@Test public void testGoogleWorld() throws Exception { File world = TestData.copy(this, "geotiff/world.tiff"); RenderedImage image = ImageIO.read(world); final CoordinateReferenceSystem wgs84 = CRS.decode("EPSG:4326", true); Envelope2D envelope = new Envelope2D(wgs84, -180, -90, 360, 180); GridCoverage2D gcFullWorld = new GridCoverageFactory().create("world", image, envelope); // crop, we cannot reproject it fully to the google projection final Envelope2D cropEnvelope = new Envelope2D(wgs84, -180, -80, 360, 160); GridCoverage2D gcCropWorld = (GridCoverage2D) Operations.DEFAULT.crop(gcFullWorld, cropEnvelope); // resample Hints.putSystemDefault(Hints.RESAMPLE_TOLERANCE, 0d); GridCoverage2D gcResampled = (GridCoverage2D) Operations.DEFAULT.resample( gcCropWorld, CRS.decode("EPSG:3857"), null, Interpolation.getInstance(Interpolation.INTERP_BILINEAR)); File expected = new File("src/test/resources/org/geotools/image/test-data/google-reproject.png"); // allow one row of difference ImageAssert.assertEquals(expected, gcResampled.getRenderedImage(), 600); }
/** * Tests the "Resample" operation with a stereographic coordinate system. * * @throws FactoryException * @throws NoSuchAuthorityCodeException */ @Test public void testReproject() throws NoSuchAuthorityCodeException, FactoryException { // do it again, make sure the image does not turn black since GridCoverage2D coverage_ = project(ushortCoverage, CRS.parseWKT(GOOGLE_MERCATOR_WKT), null, "nearest", null, true); // reproject the ushort and check that things did not go bad, that is it turned black coverage_ = (GridCoverage2D) Operations.DEFAULT.extrema(coverage_); Object minimums = coverage_.getProperty(Extrema.GT_SYNTHETIC_PROPERTY_MINIMUM); Assert.assertTrue(minimums instanceof double[]); final double[] mins = (double[]) minimums; Object maximums = coverage_.getProperty(Extrema.GT_SYNTHETIC_PROPERTY_MAXIMUM); Assert.assertTrue(maximums instanceof double[]); final double[] max = (double[]) maximums; boolean fail = true; for (int i = 0; i < mins.length; i++) if (mins[i] != max[i] && max[i] > 0) fail = false; Assert.assertFalse("Reprojection failed", fail); // exception in case the target crs does not comply with the target gg crs try { // we supplied both crs and target gg in different crs, we get an exception backS assertEquals( "Warp", showProjected( coverage, CRS.parseWKT(GOOGLE_MERCATOR_WKT), coverage.getGridGeometry(), null, true)); Assert.assertTrue( "We should not be allowed to set different crs for target crs and target gg", false); } catch (Exception e) { // ok! } }
/** * Performs a translation using the "Resample" operation. * * @param grid the {@link GridCoverage2D} to apply the translation on. * @throws NoninvertibleTransformException If a "grid to CRS" transform is not invertible. */ private void doTranslation(GridCoverage2D grid) throws NoninvertibleTransformException { final int transX = -253; final int transY = -456; final double scaleX = 0.04; final double scaleY = -0.04; final ParameterBlock block = new ParameterBlock() .addSource(grid.getRenderedImage()) .add((float) transX) .add((float) transY); RenderedImage image = JAI.create("Translate", block); assertEquals("Incorrect X translation", transX, image.getMinX()); assertEquals("Incorrect Y translation", transY, image.getMinY()); /* * Create a grid coverage from the translated image but with the same envelope. * Consequently, the 'gridToCoordinateSystem' should be translated by the same * amount, with the opposite sign. */ AffineTransform expected = getAffineTransform(grid); assertNotNull(expected); expected = new AffineTransform(expected); // Get a mutable instance. final GridCoverageFactory factory = CoverageFactoryFinder.getGridCoverageFactory(null); grid = factory.create( "Translated", image, grid.getEnvelope(), grid.getSampleDimensions(), new GridCoverage2D[] {grid}, grid.getProperties()); expected.translate(-transX, -transY); assertTransformEquals(expected, getAffineTransform(grid)); /* * Apply the "Resample" operation with a specific 'gridToCoordinateSystem' transform. * The envelope is left unchanged. The "Resample" operation should compute automatically * new image bounds. */ final AffineTransform at = AffineTransform.getScaleInstance(scaleX, scaleY); final MathTransform tr = ProjectiveTransform.create(at); // account for the half pixel correction between the two spaces since we are talking raster here // but the resample will talk model! final MathTransform correctedTransform = PixelTranslation.translate(tr, PixelInCell.CELL_CORNER, PixelInCell.CELL_CENTER); final GridGeometry2D geometry = new GridGeometry2D(null, correctedTransform, null); final GridCoverage2D newGrid = (GridCoverage2D) Operations.DEFAULT.resample(grid, grid.getCoordinateReferenceSystem(), geometry, null); assertEquals(correctedTransform, getAffineTransform(newGrid)); image = newGrid.getRenderedImage(); expected.preConcatenate(at.createInverse()); final Point point = new Point(transX, transY); assertSame(point, expected.transform(point, point)); // Round toward neareast integer }
/** Tests that flipping axis on a coverage whose origin is not (0,0) works as expected */ @Test public void testFlipTranslated() throws Exception { // build a translated image SampleModel sm = RasterFactory.createPixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, 256, 256, 3); ColorModel cm = PlanarImage.createColorModel(sm); TiledImage ti = new TiledImage(-10, -10, 5, 5, 0, 0, sm, cm); Graphics2D g = ti.createGraphics(); g.setColor(Color.GREEN); g.fillRect(-10, -10, 5, 5); g.dispose(); // build a coverage around it CoordinateReferenceSystem wgs84LatLon = CRS.decode("EPSG:4326"); final GridCoverageFactory factory = CoverageFactoryFinder.getGridCoverageFactory(null); GridCoverage2D coverage = factory.create("translated", ti, new Envelope2D(wgs84LatLon, 3, 5, 6, 8)); // verify we're good int[] pixel = new int[3]; coverage.evaluate((DirectPosition) new DirectPosition2D(4, 6), pixel); assertEquals(0, pixel[0]); assertEquals(255, pixel[1]); assertEquals(0, pixel[2]); // now reproject flipping the axis CoordinateReferenceSystem wgs84LonLat = CRS.decode("EPSG:4326", true); GridGeometry gg = new GridGeometry2D( new GridEnvelope2D(-10, -10, 5, 5), (Envelope) new Envelope2D(wgs84LonLat, 5, 3, 8, 6)); GridCoverage2D flipped = (GridCoverage2D) Operations.DEFAULT.resample( coverage, wgs84LonLat, gg, Interpolation.getInstance(Interpolation.INTERP_NEAREST)); // before the fix the pixel would have been black flipped.evaluate((DirectPosition) new DirectPosition2D(6, 4), pixel); assertEquals(0, pixel[0]); assertEquals(255, pixel[1]); assertEquals(0, pixel[2]); }
/** * Tests the "Resample" operation with a stereographic coordinate system on a paletted image * * @throws FactoryException * @throws NoSuchAuthorityCodeException */ @Test public void testReprojectPalette() throws NoSuchAuthorityCodeException, FactoryException { // do it again, make sure the image does not turn black since GridCoverage2D input = ushortCoverage; // Create a Palette image from the input coverage RenderedImage src = input.getRenderedImage(); ImageWorker iw = new ImageWorker(src).rescaleToBytes().forceIndexColorModel(false); src = iw.getRenderedOperation(); // Setting Force ReplaceIndexColorModel and CoverageProcessingView as SAME Hints hints = GeoTools.getDefaultHints().clone(); hints.put(JAI.KEY_REPLACE_INDEX_COLOR_MODEL, true); hints.put(Hints.COVERAGE_PROCESSING_VIEW, ViewType.SAME); // Create a new GridCoverage GridCoverageFactory factory = new GridCoverageFactory(hints); GridCoverage2D palette = factory.create("test", src, input.getEnvelope()); CoordinateReferenceSystem targetCRS = CRS.parseWKT(GOOGLE_MERCATOR_WKT); GridCoverage2D coverage_ = project(palette, targetCRS, null, "bilinear", hints, true); // reproject the ushort and check that things did not go bad, that is it turned black coverage_ = (GridCoverage2D) Operations.DEFAULT.extrema(coverage_); Object minimums = coverage_.getProperty(Extrema.GT_SYNTHETIC_PROPERTY_MINIMUM); Assert.assertTrue(minimums instanceof double[]); final double[] mins = (double[]) minimums; Object maximums = coverage_.getProperty(Extrema.GT_SYNTHETIC_PROPERTY_MAXIMUM); Assert.assertTrue(maximums instanceof double[]); final double[] max = (double[]) maximums; boolean fail = true; for (int i = 0; i < mins.length; i++) if (mins[i] != max[i] && max[i] > 0) fail = false; Assert.assertFalse("Reprojection failed", fail); // Ensure the CRS is correct CoordinateReferenceSystem targetCoverageCRS = coverage_.getCoordinateReferenceSystem(); Assert.assertTrue(CRS.equalsIgnoreMetadata(targetCRS, targetCoverageCRS)); }