@Test public void testZoomlevel_0() throws IOException { TPKReader reader = new TPKReader(getClass().getResource("sample_v2.tpkx"), null); GeneralParameterValue[] parameters = new GeneralParameterValue[1]; GridGeometry2D gg = new GridGeometry2D( new GridEnvelope2D(new Rectangle(200, 200)), new ReferencedEnvelope(-180, 180.0, -85.0, 85.0, TPKReader.WGS_84)); parameters[0] = new Parameter<>(AbstractGridFormat.READ_GRIDGEOMETRY2D, gg); GridCoverage2D gc = reader.read(parameters); RenderedImage img = gc.getRenderedImage(); assertEquals(-20037508.34, gc.getEnvelope().getMinimum(0), 0.01); assertEquals(-20037508.34, gc.getEnvelope().getMinimum(1), 0.01); assertEquals(20037508.34, gc.getEnvelope().getMaximum(0), 0.01); assertEquals(20037508.34, gc.getEnvelope().getMaximum(1), 0.01); assertEquals(256, img.getWidth()); assertEquals(256, img.getHeight()); }
@Override public GridCoverage2D read(GeneralParameterValue[] parameters) throws IllegalArgumentException { long startRead = System.currentTimeMillis(); TPKFile file = new TPKFile(sourceFile, zoomLevelMap, bounds, imageFormat); ReferencedEnvelope requestedEnvelope = null; Rectangle dim = null; if (parameters != null) { for (int i = 0; i < parameters.length; i++) { final ParameterValue param = (ParameterValue) parameters[i]; final ReferenceIdentifier name = param.getDescriptor().getName(); if (name.equals(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName())) { final GridGeometry2D gg = (GridGeometry2D) param.getValue(); try { requestedEnvelope = ReferencedEnvelope.create( gg.getEnvelope(), gg.getCoordinateReferenceSystem()) .transform(SPHERICAL_MERCATOR, true); } catch (Exception e) { requestedEnvelope = null; } dim = gg.getGridRange2D().getBounds(); } } } if (requestedEnvelope == null) { requestedEnvelope = bounds; } long zoomLevel = 0; long leftTile, topTile, rightTile, bottomTile; if (requestedEnvelope != null && dim != null) { double ratioWidth = requestedEnvelope.getSpan(0) / WORLD_ENVELOPE.getSpan( 0); double propWidth = dim.getWidth() / ratioWidth; zoomLevel = Math.round(Math.log(propWidth / DEFAULT_TILE_SIZE) / Math.log(ZOOM_LEVEL_BASE)); } zoomLevel = file.getClosestZoom(zoomLevel); long numberOfTiles = Math.round( Math.pow( ZOOM_LEVEL_BASE, zoomLevel)); double resX = WORLD_ENVELOPE.getSpan(0) / numberOfTiles; double resY = WORLD_ENVELOPE.getSpan(1) / numberOfTiles; double offsetX = WORLD_ENVELOPE.getMinimum(0); double offsetY = WORLD_ENVELOPE.getMinimum(1); leftTile = file.getMinColumn(zoomLevel); rightTile = file.getMaxColumn(zoomLevel); bottomTile = file.getMinRow(zoomLevel); topTile = file.getMaxRow(zoomLevel); if (requestedEnvelope != null) { leftTile = Math.max( leftTile, Math.round( Math.floor( (requestedEnvelope.getMinimum(0) - offsetX) / resX))); bottomTile = Math.max( bottomTile, Math.round( Math.floor( (requestedEnvelope.getMinimum(1) - offsetY) / resY))); rightTile = Math.max( leftTile, Math.min( rightTile, Math.round( Math.floor( (requestedEnvelope.getMaximum(0) - offsetX) / resX)))); topTile = Math.max( bottomTile, Math.min( topTile, Math.round( Math.floor( (requestedEnvelope.getMaximum(1) - offsetY) / resY)))); } int width = (int) (rightTile - leftTile + 1) * DEFAULT_TILE_SIZE; int height = (int) (topTile - bottomTile + 1) * DEFAULT_TILE_SIZE; ReferencedEnvelope resultEnvelope = new ReferencedEnvelope( offsetX + leftTile * resX, offsetX + (rightTile + 1) * resX, offsetY + bottomTile * resY, offsetY + (topTile + 1) * resY, SPHERICAL_MERCATOR); String imageFormat = file.getImageFormat(); List<TPKTile> tiles = file.getTiles(zoomLevel, topTile, bottomTile, leftTile, rightTile, imageFormat); BufferedImage image = getStartImage(BufferedImage.TYPE_INT_ARGB, width, height); final Graphics graphics = image.getGraphics(); final long originLeft = leftTile; final long originTop = topTile; tiles.parallelStream() .map(TileImage::new) .forEach( tileImage -> { if (tileImage.image != null) { int posx = (int) (tileImage.col - originLeft) * DEFAULT_TILE_SIZE; int posy = (int) (originTop - tileImage.row) * DEFAULT_TILE_SIZE; graphics.drawImage(tileImage.image, posx, posy, null); } }); file.close(); String msg = String.format( "At zoom level %d TPK read completed in %d milliseconds", zoomLevel, System.currentTimeMillis() - startRead); LOGGER.fine(msg); return coverageFactory.create("unnamed", image, resultEnvelope); }
TPKReader extends AbstractGridCoverage2DReader { @Override public GridCoverage2D read(GeneralParameterValue[] parameters) throws IllegalArgumentException { long startRead = System.currentTimeMillis(); TPKFile file = new TPKFile(sourceFile, zoomLevelMap, bounds, imageFormat); ReferencedEnvelope requestedEnvelope = null; Rectangle dim = null; if (parameters != null) { for (int i = 0; i < parameters.length; i++) { final ParameterValue param = (ParameterValue) parameters[i]; final ReferenceIdentifier name = param.getDescriptor().getName(); if (name.equals(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName())) { final GridGeometry2D gg = (GridGeometry2D) param.getValue(); try { requestedEnvelope = ReferencedEnvelope.create( gg.getEnvelope(), gg.getCoordinateReferenceSystem()) .transform(SPHERICAL_MERCATOR, true); } catch (Exception e) { requestedEnvelope = null; } dim = gg.getGridRange2D().getBounds(); } } } if (requestedEnvelope == null) { requestedEnvelope = bounds; } long zoomLevel = 0; long leftTile, topTile, rightTile, bottomTile; if (requestedEnvelope != null && dim != null) { double ratioWidth = requestedEnvelope.getSpan(0) / WORLD_ENVELOPE.getSpan( 0); double propWidth = dim.getWidth() / ratioWidth; zoomLevel = Math.round(Math.log(propWidth / DEFAULT_TILE_SIZE) / Math.log(ZOOM_LEVEL_BASE)); } zoomLevel = file.getClosestZoom(zoomLevel); long numberOfTiles = Math.round( Math.pow( ZOOM_LEVEL_BASE, zoomLevel)); double resX = WORLD_ENVELOPE.getSpan(0) / numberOfTiles; double resY = WORLD_ENVELOPE.getSpan(1) / numberOfTiles; double offsetX = WORLD_ENVELOPE.getMinimum(0); double offsetY = WORLD_ENVELOPE.getMinimum(1); leftTile = file.getMinColumn(zoomLevel); rightTile = file.getMaxColumn(zoomLevel); bottomTile = file.getMinRow(zoomLevel); topTile = file.getMaxRow(zoomLevel); if (requestedEnvelope != null) { leftTile = Math.max( leftTile, Math.round( Math.floor( (requestedEnvelope.getMinimum(0) - offsetX) / resX))); bottomTile = Math.max( bottomTile, Math.round( Math.floor( (requestedEnvelope.getMinimum(1) - offsetY) / resY))); rightTile = Math.max( leftTile, Math.min( rightTile, Math.round( Math.floor( (requestedEnvelope.getMaximum(0) - offsetX) / resX)))); topTile = Math.max( bottomTile, Math.min( topTile, Math.round( Math.floor( (requestedEnvelope.getMaximum(1) - offsetY) / resY)))); } int width = (int) (rightTile - leftTile + 1) * DEFAULT_TILE_SIZE; int height = (int) (topTile - bottomTile + 1) * DEFAULT_TILE_SIZE; ReferencedEnvelope resultEnvelope = new ReferencedEnvelope( offsetX + leftTile * resX, offsetX + (rightTile + 1) * resX, offsetY + bottomTile * resY, offsetY + (topTile + 1) * resY, SPHERICAL_MERCATOR); String imageFormat = file.getImageFormat(); List<TPKTile> tiles = file.getTiles(zoomLevel, topTile, bottomTile, leftTile, rightTile, imageFormat); BufferedImage image = getStartImage(BufferedImage.TYPE_INT_ARGB, width, height); final Graphics graphics = image.getGraphics(); final long originLeft = leftTile; final long originTop = topTile; tiles.parallelStream() .map(TileImage::new) .forEach( tileImage -> { if (tileImage.image != null) { int posx = (int) (tileImage.col - originLeft) * DEFAULT_TILE_SIZE; int posy = (int) (originTop - tileImage.row) * DEFAULT_TILE_SIZE; graphics.drawImage(tileImage.image, posx, posy, null); } }); file.close(); String msg = String.format( "At zoom level %d TPK read completed in %d milliseconds", zoomLevel, System.currentTimeMillis() - startRead); LOGGER.fine(msg); return coverageFactory.create("unnamed", image, resultEnvelope); } }
TPKReader extends AbstractGridCoverage2DReader { @Override public GridCoverage2D read(GeneralParameterValue[] parameters) throws IllegalArgumentException { long startRead = System.currentTimeMillis(); TPKFile file = new TPKFile(sourceFile, zoomLevelMap, bounds, imageFormat); ReferencedEnvelope requestedEnvelope = null; Rectangle dim = null; if (parameters != null) { for (int i = 0; i < parameters.length; i++) { final ParameterValue param = (ParameterValue) parameters[i]; final ReferenceIdentifier name = param.getDescriptor().getName(); if (name.equals(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName())) { final GridGeometry2D gg = (GridGeometry2D) param.getValue(); try { requestedEnvelope = ReferencedEnvelope.create( gg.getEnvelope(), gg.getCoordinateReferenceSystem()) .transform(SPHERICAL_MERCATOR, true); } catch (Exception e) { requestedEnvelope = null; } dim = gg.getGridRange2D().getBounds(); } } } if (requestedEnvelope == null) { requestedEnvelope = bounds; } long zoomLevel = 0; long leftTile, topTile, rightTile, bottomTile; if (requestedEnvelope != null && dim != null) { double ratioWidth = requestedEnvelope.getSpan(0) / WORLD_ENVELOPE.getSpan( 0); double propWidth = dim.getWidth() / ratioWidth; zoomLevel = Math.round(Math.log(propWidth / DEFAULT_TILE_SIZE) / Math.log(ZOOM_LEVEL_BASE)); } zoomLevel = file.getClosestZoom(zoomLevel); long numberOfTiles = Math.round( Math.pow( ZOOM_LEVEL_BASE, zoomLevel)); double resX = WORLD_ENVELOPE.getSpan(0) / numberOfTiles; double resY = WORLD_ENVELOPE.getSpan(1) / numberOfTiles; double offsetX = WORLD_ENVELOPE.getMinimum(0); double offsetY = WORLD_ENVELOPE.getMinimum(1); leftTile = file.getMinColumn(zoomLevel); rightTile = file.getMaxColumn(zoomLevel); bottomTile = file.getMinRow(zoomLevel); topTile = file.getMaxRow(zoomLevel); if (requestedEnvelope != null) { leftTile = Math.max( leftTile, Math.round( Math.floor( (requestedEnvelope.getMinimum(0) - offsetX) / resX))); bottomTile = Math.max( bottomTile, Math.round( Math.floor( (requestedEnvelope.getMinimum(1) - offsetY) / resY))); rightTile = Math.max( leftTile, Math.min( rightTile, Math.round( Math.floor( (requestedEnvelope.getMaximum(0) - offsetX) / resX)))); topTile = Math.max( bottomTile, Math.min( topTile, Math.round( Math.floor( (requestedEnvelope.getMaximum(1) - offsetY) / resY)))); } int width = (int) (rightTile - leftTile + 1) * DEFAULT_TILE_SIZE; int height = (int) (topTile - bottomTile + 1) * DEFAULT_TILE_SIZE; ReferencedEnvelope resultEnvelope = new ReferencedEnvelope( offsetX + leftTile * resX, offsetX + (rightTile + 1) * resX, offsetY + bottomTile * resY, offsetY + (topTile + 1) * resY, SPHERICAL_MERCATOR); String imageFormat = file.getImageFormat(); List<TPKTile> tiles = file.getTiles(zoomLevel, topTile, bottomTile, leftTile, rightTile, imageFormat); BufferedImage image = getStartImage(BufferedImage.TYPE_INT_ARGB, width, height); final Graphics graphics = image.getGraphics(); final long originLeft = leftTile; final long originTop = topTile; tiles.parallelStream() .map(TileImage::new) .forEach( tileImage -> { if (tileImage.image != null) { int posx = (int) (tileImage.col - originLeft) * DEFAULT_TILE_SIZE; int posy = (int) (originTop - tileImage.row) * DEFAULT_TILE_SIZE; graphics.drawImage(tileImage.image, posx, posy, null); } }); file.close(); String msg = String.format( "At zoom level %d TPK read completed in %d milliseconds", zoomLevel, System.currentTimeMillis() - startRead); LOGGER.fine(msg); return coverageFactory.create("unnamed", image, resultEnvelope); } TPKReader(Object source, Hints hints); }
TPKReader extends AbstractGridCoverage2DReader { @Override public GridCoverage2D read(GeneralParameterValue[] parameters) throws IllegalArgumentException { long startRead = System.currentTimeMillis(); TPKFile file = new TPKFile(sourceFile, zoomLevelMap, bounds, imageFormat); ReferencedEnvelope requestedEnvelope = null; Rectangle dim = null; if (parameters != null) { for (int i = 0; i < parameters.length; i++) { final ParameterValue param = (ParameterValue) parameters[i]; final ReferenceIdentifier name = param.getDescriptor().getName(); if (name.equals(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName())) { final GridGeometry2D gg = (GridGeometry2D) param.getValue(); try { requestedEnvelope = ReferencedEnvelope.create( gg.getEnvelope(), gg.getCoordinateReferenceSystem()) .transform(SPHERICAL_MERCATOR, true); } catch (Exception e) { requestedEnvelope = null; } dim = gg.getGridRange2D().getBounds(); } } } if (requestedEnvelope == null) { requestedEnvelope = bounds; } long zoomLevel = 0; long leftTile, topTile, rightTile, bottomTile; if (requestedEnvelope != null && dim != null) { double ratioWidth = requestedEnvelope.getSpan(0) / WORLD_ENVELOPE.getSpan( 0); double propWidth = dim.getWidth() / ratioWidth; zoomLevel = Math.round(Math.log(propWidth / DEFAULT_TILE_SIZE) / Math.log(ZOOM_LEVEL_BASE)); } zoomLevel = file.getClosestZoom(zoomLevel); long numberOfTiles = Math.round( Math.pow( ZOOM_LEVEL_BASE, zoomLevel)); double resX = WORLD_ENVELOPE.getSpan(0) / numberOfTiles; double resY = WORLD_ENVELOPE.getSpan(1) / numberOfTiles; double offsetX = WORLD_ENVELOPE.getMinimum(0); double offsetY = WORLD_ENVELOPE.getMinimum(1); leftTile = file.getMinColumn(zoomLevel); rightTile = file.getMaxColumn(zoomLevel); bottomTile = file.getMinRow(zoomLevel); topTile = file.getMaxRow(zoomLevel); if (requestedEnvelope != null) { leftTile = Math.max( leftTile, Math.round( Math.floor( (requestedEnvelope.getMinimum(0) - offsetX) / resX))); bottomTile = Math.max( bottomTile, Math.round( Math.floor( (requestedEnvelope.getMinimum(1) - offsetY) / resY))); rightTile = Math.max( leftTile, Math.min( rightTile, Math.round( Math.floor( (requestedEnvelope.getMaximum(0) - offsetX) / resX)))); topTile = Math.max( bottomTile, Math.min( topTile, Math.round( Math.floor( (requestedEnvelope.getMaximum(1) - offsetY) / resY)))); } int width = (int) (rightTile - leftTile + 1) * DEFAULT_TILE_SIZE; int height = (int) (topTile - bottomTile + 1) * DEFAULT_TILE_SIZE; ReferencedEnvelope resultEnvelope = new ReferencedEnvelope( offsetX + leftTile * resX, offsetX + (rightTile + 1) * resX, offsetY + bottomTile * resY, offsetY + (topTile + 1) * resY, SPHERICAL_MERCATOR); String imageFormat = file.getImageFormat(); List<TPKTile> tiles = file.getTiles(zoomLevel, topTile, bottomTile, leftTile, rightTile, imageFormat); BufferedImage image = getStartImage(BufferedImage.TYPE_INT_ARGB, width, height); final Graphics graphics = image.getGraphics(); final long originLeft = leftTile; final long originTop = topTile; tiles.parallelStream() .map(TileImage::new) .forEach( tileImage -> { if (tileImage.image != null) { int posx = (int) (tileImage.col - originLeft) * DEFAULT_TILE_SIZE; int posy = (int) (originTop - tileImage.row) * DEFAULT_TILE_SIZE; graphics.drawImage(tileImage.image, posx, posy, null); } }); file.close(); String msg = String.format( "At zoom level %d TPK read completed in %d milliseconds", zoomLevel, System.currentTimeMillis() - startRead); LOGGER.fine(msg); return coverageFactory.create("unnamed", image, resultEnvelope); } TPKReader(Object source, Hints hints); @Override Format getFormat(); @Override GridCoverage2D read(GeneralParameterValue[] parameters); }
TPKReader extends AbstractGridCoverage2DReader { @Override public GridCoverage2D read(GeneralParameterValue[] parameters) throws IllegalArgumentException { long startRead = System.currentTimeMillis(); TPKFile file = new TPKFile(sourceFile, zoomLevelMap, bounds, imageFormat); ReferencedEnvelope requestedEnvelope = null; Rectangle dim = null; if (parameters != null) { for (int i = 0; i < parameters.length; i++) { final ParameterValue param = (ParameterValue) parameters[i]; final ReferenceIdentifier name = param.getDescriptor().getName(); if (name.equals(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName())) { final GridGeometry2D gg = (GridGeometry2D) param.getValue(); try { requestedEnvelope = ReferencedEnvelope.create( gg.getEnvelope(), gg.getCoordinateReferenceSystem()) .transform(SPHERICAL_MERCATOR, true); } catch (Exception e) { requestedEnvelope = null; } dim = gg.getGridRange2D().getBounds(); } } } if (requestedEnvelope == null) { requestedEnvelope = bounds; } long zoomLevel = 0; long leftTile, topTile, rightTile, bottomTile; if (requestedEnvelope != null && dim != null) { double ratioWidth = requestedEnvelope.getSpan(0) / WORLD_ENVELOPE.getSpan( 0); double propWidth = dim.getWidth() / ratioWidth; zoomLevel = Math.round(Math.log(propWidth / DEFAULT_TILE_SIZE) / Math.log(ZOOM_LEVEL_BASE)); } zoomLevel = file.getClosestZoom(zoomLevel); long numberOfTiles = Math.round( Math.pow( ZOOM_LEVEL_BASE, zoomLevel)); double resX = WORLD_ENVELOPE.getSpan(0) / numberOfTiles; double resY = WORLD_ENVELOPE.getSpan(1) / numberOfTiles; double offsetX = WORLD_ENVELOPE.getMinimum(0); double offsetY = WORLD_ENVELOPE.getMinimum(1); leftTile = file.getMinColumn(zoomLevel); rightTile = file.getMaxColumn(zoomLevel); bottomTile = file.getMinRow(zoomLevel); topTile = file.getMaxRow(zoomLevel); if (requestedEnvelope != null) { leftTile = Math.max( leftTile, Math.round( Math.floor( (requestedEnvelope.getMinimum(0) - offsetX) / resX))); bottomTile = Math.max( bottomTile, Math.round( Math.floor( (requestedEnvelope.getMinimum(1) - offsetY) / resY))); rightTile = Math.max( leftTile, Math.min( rightTile, Math.round( Math.floor( (requestedEnvelope.getMaximum(0) - offsetX) / resX)))); topTile = Math.max( bottomTile, Math.min( topTile, Math.round( Math.floor( (requestedEnvelope.getMaximum(1) - offsetY) / resY)))); } int width = (int) (rightTile - leftTile + 1) * DEFAULT_TILE_SIZE; int height = (int) (topTile - bottomTile + 1) * DEFAULT_TILE_SIZE; ReferencedEnvelope resultEnvelope = new ReferencedEnvelope( offsetX + leftTile * resX, offsetX + (rightTile + 1) * resX, offsetY + bottomTile * resY, offsetY + (topTile + 1) * resY, SPHERICAL_MERCATOR); String imageFormat = file.getImageFormat(); List<TPKTile> tiles = file.getTiles(zoomLevel, topTile, bottomTile, leftTile, rightTile, imageFormat); BufferedImage image = getStartImage(BufferedImage.TYPE_INT_ARGB, width, height); final Graphics graphics = image.getGraphics(); final long originLeft = leftTile; final long originTop = topTile; tiles.parallelStream() .map(TileImage::new) .forEach( tileImage -> { if (tileImage.image != null) { int posx = (int) (tileImage.col - originLeft) * DEFAULT_TILE_SIZE; int posy = (int) (originTop - tileImage.row) * DEFAULT_TILE_SIZE; graphics.drawImage(tileImage.image, posx, posy, null); } }); file.close(); String msg = String.format( "At zoom level %d TPK read completed in %d milliseconds", zoomLevel, System.currentTimeMillis() - startRead); LOGGER.fine(msg); return coverageFactory.create("unnamed", image, resultEnvelope); } TPKReader(Object source, Hints hints); @Override Format getFormat(); @Override GridCoverage2D read(GeneralParameterValue[] parameters);  }