private byte[] getByteArrayFromFile(double lat, double lon) throws InterruptedException, IOException { String zippedURL = baseUrl + getDownloadURL(lat, lon); File file = new File(cacheDir, new File(zippedURL).getName()); InputStream is; // get zip file if not already in cacheDir if (!file.exists()) for (int i = 0; i < 3; i++) { try { downloader.downloadFile(zippedURL, file.getAbsolutePath()); break; } catch (SocketTimeoutException ex) { // just try again after a little nap Thread.sleep(2000); } catch (FileNotFoundException ex) { if (zippedURL.contains(".hgt.zip")) { zippedURL = zippedURL.replace(".hgt.zip", "hgt.zip"); } else { throw ex; } } } return readFile(file); }
int intKey = calcIntKey(lat, lon); HeightTile demProvider = cacheData.get(intKey); if (demProvider == null) { cacheDir.mkdirs(); int minLat = down(lat); int minLon = down(lon); String fileName = getFileName(lat, lon); if (fileName == null) return 0; DataAccess heights = getDirectory().find("dem" + intKey); boolean loadExisting = false; try { updateHeightsFromFile(lat, lon, heights); } catch (FileNotFoundException ex) { demProvider = new HeightTile(minLat, minLon, DEFAULT_WIDTH, DEFAULT_WIDTH, precision, DEGREE, DEGREE);
private void updateHeightsFromFile(double lat, double lon, DataAccess heights) throws FileNotFoundException { try { byte[] bytes = getByteArrayFromFile(lat, lon); heights.create(bytes.length); for (int bytePos = 0; bytePos < bytes.length; bytePos += 2) { short val = BIT_UTIL.toShort(bytes, bytePos); if (val < -1000 || val > 12000) val = Short.MIN_VALUE; heights.setShort(bytePos, val); } heights.setHeader(WIDTH_BYTE_INDEX, bytes.length / 2); heights.flush(); } catch (FileNotFoundException ex) { logger.warn("File not found for the coordinates for " + lat + "," + lon); throw ex; } catch (Exception ex) { throw new RuntimeException("There was an issue looking up the coordinates for " + lat + "," + lon, ex); } }
int intKey = calcIntKey(lat, lon); HeightTile demProvider = cacheData.get(intKey); if (demProvider == null) { cacheDir.mkdirs(); int minLat = down(lat); int minLon = down(lon); String fileName = getFileName(lat, lon); if (fileName == null) return 0; DataAccess heights = getDirectory().find("dem" + intKey); boolean loadExisting = false; try { updateHeightsFromFile(lat, lon, heights); } catch (FileNotFoundException ex) { demProvider = new HeightTile(minLat, minLon, DEFAULT_WIDTH, DEFAULT_WIDTH, precision, DEGREE, DEGREE);
private void updateHeightsFromFile(double lat, double lon, DataAccess heights) throws FileNotFoundException { try { byte[] bytes = getByteArrayFromFile(lat, lon); heights.create(bytes.length); for (int bytePos = 0; bytePos < bytes.length; bytePos += 2) { short val = BIT_UTIL.toShort(bytes, bytePos); if (val < -1000 || val > 12000) val = Short.MIN_VALUE; heights.setShort(bytePos, val); } heights.setHeader(WIDTH_BYTE_INDEX, bytes.length / 2); heights.flush(); } catch (FileNotFoundException ex) { logger.warn("File not found for the coordinates for " + lat + "," + lon); throw ex; } catch (Exception ex) { throw new RuntimeException("There was an issue looking up the coordinates for " + lat + "," + lon, ex); } }
private byte[] getByteArrayFromFile(double lat, double lon) throws InterruptedException, IOException { String zippedURL = baseUrl + getDownloadURL(lat, lon); File file = new File(cacheDir, new File(zippedURL).getName()); InputStream is; // get zip file if not already in cacheDir if (!file.exists()) for (int i = 0; i < 3; i++) { try { downloader.downloadFile(zippedURL, file.getAbsolutePath()); break; } catch (SocketTimeoutException ex) { // just try again after a little nap Thread.sleep(2000); } catch (FileNotFoundException ex) { if (zippedURL.contains(".hgt.zip")) { zippedURL = zippedURL.replace(".hgt.zip", "hgt.zip"); } else { throw ex; } } } return readFile(file); }