@Test public void testReadOffDatelineBothSides() throws Exception { coverageFile = URLs.urlToFile(getClass().getResource("test-data/off_dateline.tif")); assertTrue(coverageFile.exists()); GeoTiffReader reader = new GeoTiffReader(coverageFile); try { reader = new GeoTiffReader(coverageFile); ReferencedEnvelope mapExtent = new ReferencedEnvelope(-180, 0, -90, 90, DefaultGeographicCRS.WGS84); GridCoverageReaderHelper helper = new GridCoverageReaderHelper( reader, new Rectangle(1024, 512), mapExtent, Interpolation.getInstance(Interpolation.INTERP_NEAREST)); ProjectionHandler handler = ProjectionHandlerFinder.getHandler( new ReferencedEnvelope(DefaultGeographicCRS.WGS84), DefaultGeographicCRS.WGS84, true); List<GridCoverage2D> coverages = helper.readCoverageInEnvelope(mapExtent, null, handler, true); assertEquals(1, coverages.size()); GridCoverage2D coverage = coverages.get(0); Envelope2D envelope = coverage.getEnvelope2D(); final double EPS = 0.2; assertEquals(-180.4, envelope.getMinX(), EPS); assertEquals(2, envelope.getMaxX(), EPS); assertEquals(-90, envelope.getMinY(), EPS); assertEquals(90, envelope.getMaxY(), EPS); } finally { if (reader != null) { reader.dispose(); } } }
List<GridCoverage2D> readCoverageInEnvelope( ReferencedEnvelope envelope, GeneralParameterValue[] readParams, ProjectionHandler handler, boolean paddingRequired) throws TransformException, FactoryException, IOException { Polygon polygon = JTS.toGeometry(envelope); CoordinateReferenceSystem readerCRS = reader.getCoordinateReferenceSystem(); GridGeometry2D gg = new GridGeometry2D(new GridEnvelope2D(mapRasterArea), mapExtent); GridGeometry2D readingGridGeometry = computeReadingGeometry(gg, readerCRS, polygon, handler, readParams); if (readingGridGeometry == null) { return null; } if (paddingRequired) { readingGridGeometry = applyReadGutter(readingGridGeometry); } GridCoverage2D coverage = readSingleCoverage(readParams, readingGridGeometry); if (coverage == null) { return null; } ReferencedEnvelope readingEnvelope = ReferencedEnvelope.reference(readingGridGeometry.getEnvelope2D()); ReferencedEnvelope coverageEnvelope = ReferencedEnvelope.reference(coverage.getEnvelope2D()); Polygon coverageFootprint = JTS.toGeometry(coverageEnvelope); Geometry preProcessed = handler.preProcess(coverageFootprint); if (preProcessed != null && !preProcessed.isEmpty()) { if (coverageFootprint.equals(preProcessed)) { if (!readingEnvelope.contains((Envelope) coverageEnvelope)) { ReferencedEnvelope cropEnvelope = new ReferencedEnvelope( readingEnvelope.intersection(coverageEnvelope), readerCRS); GridCoverage2D cropped = cropCoverage(coverage, cropEnvelope); return singleton(cropped); } else { return singleton(coverage); } } else { final List<Polygon> polygons = PolygonExtractor.INSTANCE.getPolygons(preProcessed); final List<GridCoverage2D> coverages = new ArrayList<>(); for (Polygon p : polygons) { ReferencedEnvelope cropEnvelope = new ReferencedEnvelope(p.getEnvelopeInternal(), readerCRS); cropEnvelope = new ReferencedEnvelope( cropEnvelope.intersection(coverageEnvelope), readerCRS); cropEnvelope = new ReferencedEnvelope( cropEnvelope.intersection(readingEnvelope), readerCRS); GridCoverage2D cropped = cropCoverage(coverage, cropEnvelope); if (cropped != null) { coverages.add(cropped); } } return coverages; } } return null; }
GridCoverageReaderHelper { List<GridCoverage2D> readCoverageInEnvelope( ReferencedEnvelope envelope, GeneralParameterValue[] readParams, ProjectionHandler handler, boolean paddingRequired) throws TransformException, FactoryException, IOException { Polygon polygon = JTS.toGeometry(envelope); CoordinateReferenceSystem readerCRS = reader.getCoordinateReferenceSystem(); GridGeometry2D gg = new GridGeometry2D(new GridEnvelope2D(mapRasterArea), mapExtent); GridGeometry2D readingGridGeometry = computeReadingGeometry(gg, readerCRS, polygon, handler, readParams); if (readingGridGeometry == null) { return null; } if (paddingRequired) { readingGridGeometry = applyReadGutter(readingGridGeometry); } GridCoverage2D coverage = readSingleCoverage(readParams, readingGridGeometry); if (coverage == null) { return null; } ReferencedEnvelope readingEnvelope = ReferencedEnvelope.reference(readingGridGeometry.getEnvelope2D()); ReferencedEnvelope coverageEnvelope = ReferencedEnvelope.reference(coverage.getEnvelope2D()); Polygon coverageFootprint = JTS.toGeometry(coverageEnvelope); Geometry preProcessed = handler.preProcess(coverageFootprint); if (preProcessed != null && !preProcessed.isEmpty()) { if (coverageFootprint.equals(preProcessed)) { if (!readingEnvelope.contains((Envelope) coverageEnvelope)) { ReferencedEnvelope cropEnvelope = new ReferencedEnvelope( readingEnvelope.intersection(coverageEnvelope), readerCRS); GridCoverage2D cropped = cropCoverage(coverage, cropEnvelope); return singleton(cropped); } else { return singleton(coverage); } } else { final List<Polygon> polygons = PolygonExtractor.INSTANCE.getPolygons(preProcessed); final List<GridCoverage2D> coverages = new ArrayList<>(); for (Polygon p : polygons) { ReferencedEnvelope cropEnvelope = new ReferencedEnvelope(p.getEnvelopeInternal(), readerCRS); cropEnvelope = new ReferencedEnvelope( cropEnvelope.intersection(coverageEnvelope), readerCRS); cropEnvelope = new ReferencedEnvelope( cropEnvelope.intersection(readingEnvelope), readerCRS); GridCoverage2D cropped = cropCoverage(coverage, cropEnvelope); if (cropped != null) { coverages.add(cropped); } } return coverages; } } return null; } }
GridCoverageReaderHelper { List<GridCoverage2D> readCoverageInEnvelope( ReferencedEnvelope envelope, GeneralParameterValue[] readParams, ProjectionHandler handler, boolean paddingRequired) throws TransformException, FactoryException, IOException { Polygon polygon = JTS.toGeometry(envelope); CoordinateReferenceSystem readerCRS = reader.getCoordinateReferenceSystem(); GridGeometry2D gg = new GridGeometry2D(new GridEnvelope2D(mapRasterArea), mapExtent); GridGeometry2D readingGridGeometry = computeReadingGeometry(gg, readerCRS, polygon, handler, readParams); if (readingGridGeometry == null) { return null; } if (paddingRequired) { readingGridGeometry = applyReadGutter(readingGridGeometry); } GridCoverage2D coverage = readSingleCoverage(readParams, readingGridGeometry); if (coverage == null) { return null; } ReferencedEnvelope readingEnvelope = ReferencedEnvelope.reference(readingGridGeometry.getEnvelope2D()); ReferencedEnvelope coverageEnvelope = ReferencedEnvelope.reference(coverage.getEnvelope2D()); Polygon coverageFootprint = JTS.toGeometry(coverageEnvelope); Geometry preProcessed = handler.preProcess(coverageFootprint); if (preProcessed != null && !preProcessed.isEmpty()) { if (coverageFootprint.equals(preProcessed)) { if (!readingEnvelope.contains((Envelope) coverageEnvelope)) { ReferencedEnvelope cropEnvelope = new ReferencedEnvelope( readingEnvelope.intersection(coverageEnvelope), readerCRS); GridCoverage2D cropped = cropCoverage(coverage, cropEnvelope); return singleton(cropped); } else { return singleton(coverage); } } else { final List<Polygon> polygons = PolygonExtractor.INSTANCE.getPolygons(preProcessed); final List<GridCoverage2D> coverages = new ArrayList<>(); for (Polygon p : polygons) { ReferencedEnvelope cropEnvelope = new ReferencedEnvelope(p.getEnvelopeInternal(), readerCRS); cropEnvelope = new ReferencedEnvelope( cropEnvelope.intersection(coverageEnvelope), readerCRS); cropEnvelope = new ReferencedEnvelope( cropEnvelope.intersection(readingEnvelope), readerCRS); GridCoverage2D cropped = cropCoverage(coverage, cropEnvelope); if (cropped != null) { coverages.add(cropped); } } return coverages; } } return null; } GridCoverageReaderHelper(
            GridCoverage2DReader reader,
            Rectangle mapRasterArea,
            ReferencedEnvelope mapExtent,
            Interpolation interpolation);  GridCoverageReaderHelper(
            GridCoverage2DReader reader,
            Rectangle mapRasterArea,
            ReferencedEnvelope mapExtent,
            Interpolation interpolation,
            Hints hints); }
GridCoverageReaderHelper { List<GridCoverage2D> readCoverageInEnvelope( ReferencedEnvelope envelope, GeneralParameterValue[] readParams, ProjectionHandler handler, boolean paddingRequired) throws TransformException, FactoryException, IOException { Polygon polygon = JTS.toGeometry(envelope); CoordinateReferenceSystem readerCRS = reader.getCoordinateReferenceSystem(); GridGeometry2D gg = new GridGeometry2D(new GridEnvelope2D(mapRasterArea), mapExtent); GridGeometry2D readingGridGeometry = computeReadingGeometry(gg, readerCRS, polygon, handler, readParams); if (readingGridGeometry == null) { return null; } if (paddingRequired) { readingGridGeometry = applyReadGutter(readingGridGeometry); } GridCoverage2D coverage = readSingleCoverage(readParams, readingGridGeometry); if (coverage == null) { return null; } ReferencedEnvelope readingEnvelope = ReferencedEnvelope.reference(readingGridGeometry.getEnvelope2D()); ReferencedEnvelope coverageEnvelope = ReferencedEnvelope.reference(coverage.getEnvelope2D()); Polygon coverageFootprint = JTS.toGeometry(coverageEnvelope); Geometry preProcessed = handler.preProcess(coverageFootprint); if (preProcessed != null && !preProcessed.isEmpty()) { if (coverageFootprint.equals(preProcessed)) { if (!readingEnvelope.contains((Envelope) coverageEnvelope)) { ReferencedEnvelope cropEnvelope = new ReferencedEnvelope( readingEnvelope.intersection(coverageEnvelope), readerCRS); GridCoverage2D cropped = cropCoverage(coverage, cropEnvelope); return singleton(cropped); } else { return singleton(coverage); } } else { final List<Polygon> polygons = PolygonExtractor.INSTANCE.getPolygons(preProcessed); final List<GridCoverage2D> coverages = new ArrayList<>(); for (Polygon p : polygons) { ReferencedEnvelope cropEnvelope = new ReferencedEnvelope(p.getEnvelopeInternal(), readerCRS); cropEnvelope = new ReferencedEnvelope( cropEnvelope.intersection(coverageEnvelope), readerCRS); cropEnvelope = new ReferencedEnvelope( cropEnvelope.intersection(readingEnvelope), readerCRS); GridCoverage2D cropped = cropCoverage(coverage, cropEnvelope); if (cropped != null) { coverages.add(cropped); } } return coverages; } } return null; } GridCoverageReaderHelper(
            GridCoverage2DReader reader,
            Rectangle mapRasterArea,
            ReferencedEnvelope mapExtent,
            Interpolation interpolation);  GridCoverageReaderHelper(
            GridCoverage2DReader reader,
            Rectangle mapRasterArea,
            ReferencedEnvelope mapExtent,
            Interpolation interpolation,
            Hints hints); static boolean isReprojectingReader(GridCoverage2DReader reader); ReferencedEnvelope getReadEnvelope(); GridCoverage2D readCoverage(final GeneralParameterValue[] params); List<GridCoverage2D> readCoverages(
            final GeneralParameterValue[] readParams, ProjectionHandler handler); List<GridCoverage2D> readCoverages(
            final GeneralParameterValue[] readParams,
            ProjectionHandler handler,
            GridCoverageFactory gridCoverageFactory); }
GridCoverageReaderHelper { List<GridCoverage2D> readCoverageInEnvelope( ReferencedEnvelope envelope, GeneralParameterValue[] readParams, ProjectionHandler handler, boolean paddingRequired) throws TransformException, FactoryException, IOException { Polygon polygon = JTS.toGeometry(envelope); CoordinateReferenceSystem readerCRS = reader.getCoordinateReferenceSystem(); GridGeometry2D gg = new GridGeometry2D(new GridEnvelope2D(mapRasterArea), mapExtent); GridGeometry2D readingGridGeometry = computeReadingGeometry(gg, readerCRS, polygon, handler, readParams); if (readingGridGeometry == null) { return null; } if (paddingRequired) { readingGridGeometry = applyReadGutter(readingGridGeometry); } GridCoverage2D coverage = readSingleCoverage(readParams, readingGridGeometry); if (coverage == null) { return null; } ReferencedEnvelope readingEnvelope = ReferencedEnvelope.reference(readingGridGeometry.getEnvelope2D()); ReferencedEnvelope coverageEnvelope = ReferencedEnvelope.reference(coverage.getEnvelope2D()); Polygon coverageFootprint = JTS.toGeometry(coverageEnvelope); Geometry preProcessed = handler.preProcess(coverageFootprint); if (preProcessed != null && !preProcessed.isEmpty()) { if (coverageFootprint.equals(preProcessed)) { if (!readingEnvelope.contains((Envelope) coverageEnvelope)) { ReferencedEnvelope cropEnvelope = new ReferencedEnvelope( readingEnvelope.intersection(coverageEnvelope), readerCRS); GridCoverage2D cropped = cropCoverage(coverage, cropEnvelope); return singleton(cropped); } else { return singleton(coverage); } } else { final List<Polygon> polygons = PolygonExtractor.INSTANCE.getPolygons(preProcessed); final List<GridCoverage2D> coverages = new ArrayList<>(); for (Polygon p : polygons) { ReferencedEnvelope cropEnvelope = new ReferencedEnvelope(p.getEnvelopeInternal(), readerCRS); cropEnvelope = new ReferencedEnvelope( cropEnvelope.intersection(coverageEnvelope), readerCRS); cropEnvelope = new ReferencedEnvelope( cropEnvelope.intersection(readingEnvelope), readerCRS); GridCoverage2D cropped = cropCoverage(coverage, cropEnvelope); if (cropped != null) { coverages.add(cropped); } } return coverages; } } return null; } GridCoverageReaderHelper(
            GridCoverage2DReader reader,
            Rectangle mapRasterArea,
            ReferencedEnvelope mapExtent,
            Interpolation interpolation);  GridCoverageReaderHelper(
            GridCoverage2DReader reader,
            Rectangle mapRasterArea,
            ReferencedEnvelope mapExtent,
            Interpolation interpolation,
            Hints hints); static boolean isReprojectingReader(GridCoverage2DReader reader); ReferencedEnvelope getReadEnvelope(); GridCoverage2D readCoverage(final GeneralParameterValue[] params); List<GridCoverage2D> readCoverages(
            final GeneralParameterValue[] readParams, ProjectionHandler handler); List<GridCoverage2D> readCoverages(
            final GeneralParameterValue[] readParams,
            ProjectionHandler handler,
            GridCoverageFactory gridCoverageFactory);  }