GridCoverageReaderHelper { public List<GridCoverage2D> readCoverages(final GeneralParameterValue[] readParams, ProjectionHandler handler) throws IOException, FactoryException, TransformException { if (handler == null) { GridCoverage2D readCoverage = readCoverage(readParams); return Arrays.asList(readCoverage); } GridGeometry2D gg = new GridGeometry2D(new GridEnvelope2D(mapRasterArea), mapExtent); CoordinateReferenceSystem readerCRS = reader.getCoordinateReferenceSystem(); List<GridCoverage2D> coverages = new ArrayList<GridCoverage2D>(); PolygonExtractor polygonExtractor = new PolygonExtractor(); for (ReferencedEnvelope envelope : handler.getQueryEnvelopes()) { Polygon polygon = JTS.toGeometry(envelope); GridGeometry2D readingGridGeometry = computeReadingGeometry(gg, readerCRS, polygon, handler); if (readingGridGeometry == null) { continue; } if (paddingRequired) { readingGridGeometry = applyReadGutter(readingGridGeometry); } GridCoverage2D coverage = readSingleCoverage(readParams, readingGridGeometry); if (coverage == null) { continue; } 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()) { continue; } else if (coverageFootprint.equals(preProcessed)) { if(!readingEnvelope.contains((Envelope) coverageEnvelope)) { ReferencedEnvelope cropEnvelope = new ReferencedEnvelope( readingEnvelope.intersection(coverageEnvelope), readerCRS); if (isNotEmpty(cropEnvelope)) { GridCoverage2D cropped = cropCoverage(coverage, cropEnvelope); coverages.add(cropped); } } else { coverages.add(coverage); } } else { final List<Polygon> polygons = polygonExtractor.getPolygons(preProcessed); 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); if (isNotEmpty(cropEnvelope)) { GridCoverage2D cropped = cropCoverage(coverage, cropEnvelope); coverages.add(cropped); } } } } return coverages; } GridCoverageReaderHelper(GridCoverage2DReader reader, Rectangle mapRasterArea,
            ReferencedEnvelope mapExtent, Interpolation interpolation); static boolean isReprojectingReader(GridCoverage2DReader reader); ReferencedEnvelope getReadEnvelope(); GridCoverage2D readCoverage(final GeneralParameterValue[] params); List<GridCoverage2D> readCoverages(final GeneralParameterValue[] readParams,
            ProjectionHandler handler);  }
@Test public void testOutsideDefinitionArea() throws Exception { CoordinateReferenceSystem crs = CRS.decode("EPSG:3031", true); ReferencedEnvelope mapExtent = new ReferencedEnvelope(-1250000, 0, -13750000, -12500000, crs); GridCoverageReaderHelper helper = new GridCoverageReaderHelper(reader, new Rectangle(400, 200), mapExtent, Interpolation.getInstance(Interpolation.INTERP_NEAREST)); ProjectionHandler handler = ProjectionHandlerFinder.getHandler(mapExtent, reader.getCoordinateReferenceSystem(), true); List<GridCoverage2D> coverages = helper.readCoverages(null, handler); assertTrue(coverages.isEmpty()); }
@Test public void testFullResolutionNull() throws Exception { final GridCoverage2D coverage = new GridCoverageFactory().create("test", new float[200][100], new ReferencedEnvelope(-180, 180, -90, 90, DefaultGeographicCRS.WGS84)); GridCoverage2DReader reader = new AbstractGridCoverage2DReader() { { this.crs = DefaultGeographicCRS.WGS84; this.originalEnvelope = new GeneralEnvelope((BoundingBox) coverage.getEnvelope2D()); this.originalGridRange = coverage.getGridGeometry().getGridRange(); } @Override public Format getFormat() { return null; } @Override public GridCoverage2D read(GeneralParameterValue[] parameters) throws IllegalArgumentException, IOException { return coverage; } }; CoordinateReferenceSystem crs = CRS.decode("EPSG:3031", true); ReferencedEnvelope mapExtent = new ReferencedEnvelope(-20000000, 20000000, -20000000, 20000000, crs); GridCoverageReaderHelper helper = new GridCoverageReaderHelper(reader, new Rectangle(400, 200), mapExtent, Interpolation.getInstance(Interpolation.INTERP_NEAREST)); ProjectionHandler handler = ProjectionHandlerFinder.getHandler(mapExtent, reader.getCoordinateReferenceSystem(), true); List<GridCoverage2D> coverages = helper.readCoverages(null, handler); assertEquals(1, coverages.size()); }