GridCoverageUtil { public static GridCoverage2D scale(GridCoverage2D coverage, float width, float height) { final RenderedImage renderedImage = coverage.getRenderedImage(); final Raster renderedGrid = renderedImage.getData(); float yScale = width/renderedGrid.getWidth(); float xScale = height/renderedGrid.getHeight(); final Operations ops = new Operations(null); return (GridCoverage2D) ops.scale(coverage, xScale, yScale, 0, 0); }  static GridCoverage2D scale(GridCoverage2D coverage, float width, float height); static GridCoverage2D crop(GridCoverage2D coverage, Envelope envelope);  }
@Test public void testExactUpScale() { float[][] grid = new float[][] {{1,2},{3,4}}; final GridCoverageFactory coverageFactory = CoverageFactoryFinder.getGridCoverageFactory(GeoTools.getDefaultHints()); final GridCoverage2D coverage = coverageFactory.create("geohashGridAgg", grid, new ReferencedEnvelope(0,1,0,1,null)); GridCoverage2D scaled = GridCoverageUtil.scale(coverage, 4, 4); final RenderedImage renderedImage = scaled.getRenderedImage(); assertEquals(4, renderedImage.getWidth()); assertEquals(4, renderedImage.getHeight()); float[][] expected = new float[][] {{1,1,2,2},{1,1,2,2},{3,3,4,4},{3,3,4,4}}; IntStream.range(0,4).forEach(i->IntStream.range(0, 4).forEach(j -> { float actual = scaled.evaluate(new GridCoordinates2D(j,i), new float[1])[0]; assertEquals(expected[i][j], actual, 1e-10); })); }
@Test public void testExactDownScale() { float[][] grid = new float[][] {{1,1,2,2},{1,1,2,2},{3,3,4,4},{3,3,4,4}}; final GridCoverageFactory coverageFactory = CoverageFactoryFinder.getGridCoverageFactory(GeoTools.getDefaultHints()); final GridCoverage2D coverage = coverageFactory.create("geohashGridAgg", grid, new ReferencedEnvelope(0,1,0,1,null)); GridCoverage2D scaled = GridCoverageUtil.scale(coverage, 2,2); final RenderedImage renderedImage = scaled.getRenderedImage(); assertEquals(2, renderedImage.getWidth()); assertEquals(2, renderedImage.getHeight()); float[][] expected = new float[][] {{1,2},{3,4}}; IntStream.range(0,2).forEach(i->IntStream.range(0, 2).forEach(j -> { float actual = scaled.evaluate(new GridCoordinates2D(j,i), new float[1])[0]; assertEquals(expected[i][j], actual, 1e-10); })); }
@Test public void testInexactScale() { float[][] grid = new float[][] {{1,2},{3,4}}; final GridCoverageFactory coverageFactory = CoverageFactoryFinder.getGridCoverageFactory(GeoTools.getDefaultHints()); final GridCoverage2D coverage = coverageFactory.create("geohashGridAgg", grid, new ReferencedEnvelope(0,1,0,1,null)); GridCoverage2D scaled = GridCoverageUtil.scale(coverage, 3, 7); final RenderedImage renderedImage = scaled.getRenderedImage(); assertEquals(7, renderedImage.getWidth()); assertEquals(3, renderedImage.getHeight()); }
@Test public void testSmallScale() { float[][] grid = new float[1500][1500]; final GridCoverageFactory coverageFactory = CoverageFactoryFinder.getGridCoverageFactory(GeoTools.getDefaultHints()); final GridCoverage2D coverage = coverageFactory.create("geohashGridAgg", grid, new ReferencedEnvelope(0,1,0,1,null)); GridCoverage2D scaled = GridCoverageUtil.scale(coverage, 1501, 1499); final RenderedImage renderedImage = scaled.getRenderedImage(); assertEquals(1499, renderedImage.getWidth()); assertEquals(1501, renderedImage.getHeight()); }
@Test public void testLargeScale() { float[][] grid = new float[2][2]; final GridCoverageFactory coverageFactory = CoverageFactoryFinder.getGridCoverageFactory(GeoTools.getDefaultHints()); final GridCoverage2D coverage = coverageFactory.create("geohashGridAgg", grid, new ReferencedEnvelope(0,1,0,1,null)); GridCoverage2D scaled = GridCoverageUtil.scale(coverage, 1501, 1499); final RenderedImage renderedImage = scaled.getRenderedImage(); assertEquals(1499, renderedImage.getWidth()); assertEquals(1501, renderedImage.getHeight()); }