@Override public void createIndividuals() { LOG.info("Loading population from raster file {}", sourceFilename); try { File rasterFile = new File(sourceFilename); // determine file format and CRS, then load raster AbstractGridFormat format = GridFormatFinder.findFormat(rasterFile); AbstractGridCoverage2DReader reader = format.getReader(rasterFile); GridCoverage2D coverage = reader.read(null); this.coverageCRS = coverage.getCoordinateReferenceSystem(); GridGeometry2D gridGeometry = coverage.getGridGeometry(); GridEnvelope2D gridEnvelope = gridGeometry.getGridRange2D(); gridGeometry.getGridToCRS(); // because we may want to produce an empty raster rather than loading one, alongside the coverage we // store the row/col dimensions and the referenced envelope in the original coordinate reference system. this.cols = gridEnvelope.width; this.rows = gridEnvelope.height; this.createIndividuals0(); } catch (Exception ex) { throw new IllegalStateException("Error loading population from raster file: ", ex); } LOG.info("Done loading raster from file."); }
private static GridCoverage2D existingCoverage( GridCoverage2D coverage, CoordinateReferenceSystem targetCRS, GridGeometry2D targetGG) { // NoData and ROI must be handled ROI roiProp = CoverageUtilities.getROIProperty(coverage); Object nodataProp = CoverageUtilities.getNoDataProperty(coverage); boolean hasROI = (roiProp != null); boolean hasNoData = (nodataProp != null); if (hasROI || hasNoData) { return null; } while (!equivalent(coverage.getGridGeometry(), targetGG) || (!CRS.equalsIgnoreMetadata(targetCRS, coverage.getCoordinateReferenceSystem()) && !CRS.equalsIgnoreMetadata( targetCRS, coverage.getCoordinateReferenceSystem2D()))) { if (!(coverage instanceof Resampler2D)) { return null; } final List<GridCoverage> sources = coverage.getSources(); assert sources.size() == 1 : sources; coverage = (GridCoverage2D) sources.get(0); } return coverage; }
PrintStream ps = new PrintStream(output); ps.println("Grid bounds: " + coverage.getEnvelope()); ps.println("Grid CRS: " + coverage.getCoordinateReferenceSystem()); ps.println("Grid range: " + coverage.getGridGeometry().getGridRange()); ps.println("Grid to world: " + coverage.getGridGeometry().getGridToCRS());
FileUtils.copyFile(sourceFile, stream); } else { CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem(); boolean unreferenced = crs == null || crs instanceof EngineeringCRS;
static Integer findSRID(GridCoverage2D raster) throws Exception { return CRS.lookupEpsgCode(raster.getCoordinateReferenceSystem(), true); }
/** * Layer bounds generated from the grid coverage. * * @return layer bounds generated from the grid coverage. */ public ReferencedEnvelope getBounds() { if (coverage != null) { CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem(); Envelope2D bounds = coverage.getEnvelope2D(); if (bounds != null) { return new ReferencedEnvelope(bounds); } else if (crs != null) { return new ReferencedEnvelope(crs); } } return null; }
crs = coverage.getCoordinateReferenceSystem(); } else { CoordinateReferenceSystem crs1 = coverage.getCoordinateReferenceSystem();
public Coverage doOperation(final ParameterValueGroup parameters, final Hints hints) throws CoverageProcessingException { /* * Extracts the source grid coverages now as a List. The sources will be set in the ParameterBlockJAI (as RenderedImages) later. */ final Collection<GridCoverage2D> sourceCollection = new ArrayList<GridCoverage2D>(); extractSources(parameters, sourceCollection, null); // Selection of the source number int numSources = sourceCollection.size(); GridCoverage2D[] sources = new GridCoverage2D[numSources]; // Creation of an array of GridCoverage2D from the input collection sourceCollection.toArray(sources); // Selection of the CRS of the first coverage in order to check that the CRS is the same for // all the GridCoverages GridCoverage2D firstCoverage = sources[PRIMARY_SOURCE_INDEX]; CoordinateReferenceSystem crs = firstCoverage.getCoordinateReferenceSystem(); for (int i = 0; i < sources.length; i++) { final GridCoverage2D source = sources[i]; CoordinateReferenceSystem crsSource = source.getCoordinateReferenceSystem(); if (!CRS.equalsIgnoreMetadata(crs, crsSource)) { throw new CoverageProcessingException("Input Coverages have different CRS"); } } // Preparation of the input parameters and resampling of the source images final Params params = prepareParameters(parameters, sources, hints); /* * Applies the operation. This delegates the work to the chain of 'deriveXXX' methods. */ return deriveGridCoverage(sources, params); }
static ReferencedEnvelope findBounds(GridCoverage2D raster) { org.opengis.geometry.Envelope e = raster.getEnvelope(); return new ReferencedEnvelope( e.getMinimum(0), e.getMaximum(0), e.getMinimum(1), e.getMaximum(1), raster.getCoordinateReferenceSystem()); }
private GridCoverage2D cropCoverageOnRequestedEnvelope(GridCoverage2D readCoverage) { if (readCoverage == null) { return null; } try { ReferencedEnvelope requested = ReferencedEnvelope.reference(requestedGridGeometry.getEnvelope()); ReferencedEnvelope requestedNativeCRS = requested.transform(readCoverage.getCoordinateReferenceSystem(), true); ReferencedEnvelope coverageEnvelope = ReferencedEnvelope.reference(readCoverage.getEnvelope()); ReferencedEnvelope cropEnvelope = new ReferencedEnvelope( requestedNativeCRS.intersection(coverageEnvelope), readCoverage.getCoordinateReferenceSystem()); if (isNotEmpty(cropEnvelope)) { GridCoverage2D cropCoverage = cropCoverage(readCoverage, requestedNativeCRS); return cropCoverage; } else { return null; } } catch (Exception e) { LOGGER.log( Level.FINE, "Failed to crop coverage on the requested area, using the original one", e); return readCoverage; } }
@Test public void test() throws IOException, URISyntaxException, MismatchedDimensionException, NoSuchAuthorityCodeException, FactoryException { StreamingRenderer renderer = new StreamingRenderer(); MapContent map = new MapContent(); URL raster = getClass().getResource(rasterBase + ".png"); GridCoverage2D gc = readGeoReferencedImageFile(new File(raster.toURI())); map.addLayer(loadGeoReferencedImageFile(gc, "test")); renderer.setMapContent(map); BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_ARGB); Envelope env = new Envelope(1880352, 5825436, 1884352, 5828436); ReferencedEnvelope refenv = new ReferencedEnvelope(env, gc.getCoordinateReferenceSystem()); AtomicReference<Exception> error = new AtomicReference<>(); renderer.addRenderListener( new RenderListener() { @Override public void featureRenderer(SimpleFeature feature) {} @Override public void errorOccurred(Exception e) { error.set(e); } }); renderer.paint(image.createGraphics(), new Rectangle(400, 300), refenv); map.dispose(); assertNull(error.get()); }
CoordinateReferenceSystem crsA = coverageA.getCoordinateReferenceSystem(); CoordinateReferenceSystem crsB = coverageB.getCoordinateReferenceSystem(); if (!CRS.equalsIgnoreMetadata(crsA, crsB)) { MathTransform mathTransform = null;
CoordinateReferenceSystem dataCrs = dataCoverage.getCoordinateReferenceSystem(); CoordinateReferenceSystem zonesCrs = builder.getFeatureType()
} else { crs = coverage.getCoordinateReferenceSystem();
geoCRS, cov.getCoordinateReferenceSystem());
GridCoverageRenderer renderer = new GridCoverageRenderer( coverage.getCoordinateReferenceSystem(), mapExtent, screenSize, w2s);
assertTrue(property instanceof GridCoverage2D); GridCoverage2D propertyCoverage = (GridCoverage2D) property; CoordinateReferenceSystem targetCrs = propertyCoverage.getCoordinateReferenceSystem(); assertTrue(targetCrs.getName().equals(crs.getName()));
MathTransform g2w = source.getGridGeometry().getGridToCRS(); AffineTransform af = (AffineTransform) g2w; CoordinateReferenceSystem crs = source.getCoordinateReferenceSystem(); Unit<?> unit = crs.getCoordinateSystem().getAxis(0).getUnit(); boolean isMeter = unit.isCompatible(SI.METRE);
parameters.parameter("CoordinateReferenceSystem").getValue(); if (targetCRS == null) { targetCRS = source.getCoordinateReferenceSystem();
/** Returns a projected CRS for test purpose. */ private static CoordinateReferenceSystem getProjectedCRS(final GridCoverage2D coverage) { try { final GeographicCRS base = (GeographicCRS) coverage.getCoordinateReferenceSystem(); final Ellipsoid ellipsoid = base.getDatum().getEllipsoid(); final DefaultMathTransformFactory factory = new DefaultMathTransformFactory(); final ParameterValueGroup parameters = factory.getDefaultParameters("Oblique_Stereographic"); parameters.parameter("semi_major").setValue(ellipsoid.getSemiMajorAxis()); parameters.parameter("semi_minor").setValue(ellipsoid.getSemiMinorAxis()); parameters.parameter("central_meridian").setValue(5); parameters.parameter("latitude_of_origin").setValue(-5); final MathTransform mt; try { mt = factory.createParameterizedTransform(parameters); } catch (FactoryException exception) { fail(exception.getLocalizedMessage()); return null; } // create the projected crs return new DefaultProjectedCRS( java.util.Collections.singletonMap("name", "Stereographic"), base, mt, DefaultCartesianCS.PROJECTED); } catch (NoSuchIdentifierException exception) { fail(exception.getLocalizedMessage()); return null; } }