protected ShortBuffer readInt16Data(InputStream stream) throws IOException { ShortBuffer result = this.bufferStream(stream, ByteBuffer.allocate(BUFFER_SIZE)).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer(); return result; }
protected ShortBuffer decodeCoverage(ImageSource imageSource) throws IOException { if (imageSource.isUrl()) { return this.decodeUrl(imageSource.asUrl()); } return this.decodeUnrecognized(imageSource); }
protected ShortBuffer decodeUrl(String urlString) throws IOException { // TODO establish a file caching service for remote resources // TODO retry absent resources, they are currently handled but suppressed entirely after the first failure // TODO configurable connect and read timeouts InputStream stream = null; try { URLConnection conn = new URL(urlString).openConnection(); conn.setConnectTimeout(3000); conn.setReadTimeout(30000); stream = new BufferedInputStream(conn.getInputStream()); String contentType = conn.getContentType(); if (contentType.equalsIgnoreCase("application/bil16")) { return this.readInt16Data(stream); } else if (contentType.equalsIgnoreCase("image/tiff")) { return this.readTiffData(stream); } else { throw new RuntimeException( Logger.logMessage(Logger.ERROR, "ElevationRetriever", "decodeUrl", "Format not supported")); } } finally { WWUtil.closeSilently(stream); } }
protected ShortBuffer readTiffData(InputStream stream) throws IOException { ByteBuffer tiffBuffer = this.bufferPool.acquire(); if (tiffBuffer == null) { tiffBuffer = ByteBuffer.allocate(BUFFER_SIZE); } tiffBuffer.clear(); ByteBuffer buffer = this.bufferStream(stream, tiffBuffer); Tiff tiff = new Tiff(buffer); Subfile subfile = tiff.getSubfiles().get(0); // check that the format of the subfile matches our supported data types if (this.isTiffFormatSupported(subfile)) { int dataSize = subfile.getDataSize(); ByteBuffer result = subfile.getData(ByteBuffer.allocate(dataSize)); result.clear(); this.bufferPool.release(tiffBuffer); return result.asShortBuffer(); } else { throw new RuntimeException( Logger.logMessage(Logger.ERROR, "ElevationRetriever", "readTiffData", "Tiff file format not supported")); } }
public TiledElevationCoverage() { this.coverageSource = new LruMemoryCache<>(200); this.coverageCache = new LruMemoryCache<>(1024 * 1024 * 8); this.coverageRetriever = new ElevationRetriever(4); this.coverageHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() { @Override public boolean handleMessage(Message msg) { return false; } }); Logger.log(Logger.INFO, String.format(Locale.US, "Coverage cache initialized %,.0f KB", this.coverageCache.getCapacity() / 1024.0)); }
@Override protected void retrieveAsync(ImageSource key, Void unused, Callback<ImageSource, Void, ShortBuffer> callback) { try { ShortBuffer buffer = this.decodeCoverage(key); if (buffer != null) { callback.retrievalSucceeded(this, key, unused, buffer); } else { callback.retrievalFailed(this, key, null); // failed but no exception } } catch (Throwable logged) { callback.retrievalFailed(this, key, logged); // failed with exception } }
protected short[] fetchTileArray(TileMatrix tileMatrix, int row, int column) { long key = tileKey(tileMatrix, row, column); ImageSource tileSource = this.coverageSource.get(key); if (tileSource == null) { tileSource = this.tileFactory.createTileSource(tileMatrix, row, column); this.coverageSource.put(key, tileSource, 1); } short[] tileArray = this.coverageCache.get(tileSource); if (tileArray == null && this.isEnableRetrieval()) { this.coverageRetriever.retrieve(tileSource, null, this); } return tileArray; }