/** Helper method for transforming an envelope. */ private static ReferencedEnvelope transform(ReferencedEnvelope e, CoordinateReferenceSystem crs) throws TransformException, FactoryException { if (!CRS.equalsIgnoreMetadata(crs, e.getCoordinateReferenceSystem())) { return e.transform(crs, true); } return e; }
/** * Computes the geographic bounds of a {@link ResourceInfo} by reprojecting the available native * bounds * * @param rinfo * @return the geographic bounds, or null if the native bounds are not available * @throws IOException */ public ReferencedEnvelope getLatLonBounds( ReferencedEnvelope nativeBounds, CoordinateReferenceSystem declaredCRS) throws IOException { if (nativeBounds != null && declaredCRS != null) { // make sure we use the declared CRS, not the native one, the may differ if (!CRS.equalsIgnoreMetadata(DefaultGeographicCRS.WGS84, declaredCRS)) { // transform try { ReferencedEnvelope bounds = new ReferencedEnvelope(nativeBounds, CRS.getHorizontalCRS(declaredCRS)); return bounds.transform(DefaultGeographicCRS.WGS84, true); } catch (Exception e) { throw (IOException) new IOException("transform error").initCause(e); } } else { return new ReferencedEnvelope(nativeBounds, DefaultGeographicCRS.WGS84); } } return null; }
@Override public BoundingBox getBoundingBox(CoordinateReferenceSystem crs) { try { Envelope bounds = (Envelope) pedestrianIndex.getRoot().getBounds(); ReferencedEnvelope refEnv = new ReferencedEnvelope(bounds, CRS.decode("EPSG:4326", true)); return refEnv.toBounds(crs); } catch (Exception e) { LOG.error("error transforming graph bounding box to request CRS : {}", crs); return null; } }
/** * Use the CRS's defined bounds to populate the LayerGroup bounds. * * <p>If the CRS has no bounds then the layer group bounds are set to null instead * * @param crs */ public void calculateBoundsFromCRS(CoordinateReferenceSystem crs) { Envelope crsEnvelope = CRS.getEnvelope(crs); if (crsEnvelope != null) { ReferencedEnvelope refEnvelope = new ReferencedEnvelope(crsEnvelope); this.group.setBounds(refEnvelope); } else { this.group.setBounds(null); } }
@Test public void testLatLonBounds() throws Exception { ReferencedEnvelope nativeBounds = new ReferencedEnvelope(700000, 800000, 4000000, 4100000, null); CoordinateReferenceSystem crs = CRS.decode("EPSG:32632", true); CatalogBuilder cb = new CatalogBuilder(getCatalog()); ReferencedEnvelope re = cb.getLatLonBounds(nativeBounds, crs); assertEquals(DefaultGeographicCRS.WGS84, re.getCoordinateReferenceSystem()); assertEquals(11.22, re.getMinX(), 0.01); assertEquals(36.1, re.getMinY(), 0.01); }
/** @param crs */ public void calculateBounds(CoordinateReferenceSystem crs) throws Exception { List<LayerInfo> layers = allLayers(); if (layers.isEmpty()) { return; } LayerInfo l = layers.get(0); ReferencedEnvelope bounds = new ReferencedEnvelope(crs); for (int i = 0; i < layers.size(); i++) { l = layers.get(i); bounds.expandToInclude(transform(l.getResource().getLatLonBoundingBox(), crs)); } group.setBounds(bounds); }
/** * Crops the coverage to the specified bounds * * @param coverage * @param bounds */ public static GridCoverage2D crop(final GridCoverage2D coverage, final Envelope bounds) { // checks final ReferencedEnvelope cropBounds = new ReferencedEnvelope(bounds); final ReferencedEnvelope coverageBounds = new ReferencedEnvelope(coverage.getEnvelope()); if (cropBounds.contains((org.locationtech.jts.geom.Envelope) coverageBounds)) { return coverage; } Polygon polygon = JTS.toGeometry(cropBounds); Geometry roi = polygon.getFactory().createMultiPolygon(new Polygon[] {polygon}); // perform the crops final ParameterValueGroup param = PROCESSOR.getOperation("CoverageCrop").getParameters(); param.parameter("Source").setValue(coverage); param.parameter("Envelope").setValue(bounds); param.parameter("ROI").setValue(roi); return (GridCoverage2D) PROCESSOR.doOperation(param); }
public ReferencedEnvelope getBounds() { ReferencedEnvelope bounds = null; SimpleFeatureIterator i = features(); try { if (!i.hasNext()) { bounds = new ReferencedEnvelope(); bounds.setToNull(); } else { SimpleFeature first = (SimpleFeature) i.next(); bounds = new ReferencedEnvelope(first.getBounds()); } while (i.hasNext()) { SimpleFeature f = (SimpleFeature) i.next(); bounds.include(f.getBounds()); } return bounds; } finally { i.close(); } }
public ReferencedEnvelope boundingBox() throws Exception { CoordinateReferenceSystem declaredCRS = getCRS(); CoordinateReferenceSystem nativeCRS = getNativeCRS(); ProjectionPolicy php = getProjectionPolicy(); ReferencedEnvelope nativeBox = this.nativeBoundingBox; if (nativeBox == null) { // back project from lat lon try { nativeBox = getLatLonBoundingBox().transform(declaredCRS, true); } catch (Exception e) { LOGGER.log(Level.WARNING, "Failed to derive native bbox from declared one", e); return null; } } ReferencedEnvelope result; if (!CRS.equalsIgnoreMetadata(declaredCRS, nativeCRS) && php == ProjectionPolicy.REPROJECT_TO_DECLARED) { result = nativeBox.transform(declaredCRS, true); } else if (php == ProjectionPolicy.FORCE_DECLARED) { result = ReferencedEnvelope.create((Envelope) nativeBox, declaredCRS); } else { result = nativeBox; } // make sure that in no case the actual field value is returned to the client, this // is not a getter, it's a derivative, thus ModificationProxy won't do a copy on its own return ReferencedEnvelope.create(result); }
public FeatureSourceWrappingFeatureTypeInfoImpl(FeatureSource featureSource) throws IOException, TransformException, FactoryException { super(); this.featureSource = featureSource; setName(featureSource.getName().getLocalPart()); setEnabled(true); setLatLonBoundingBox( featureSource.getBounds().transform(DefaultGeographicCRS.WGS84, true)); }
@Test public void testWgs84BoundsFromCompoundCRS() throws Exception { try { MapProjection.SKIP_SANITY_CHECKS = true; CatalogBuilder cb = new CatalogBuilder(getCatalog()); ReferencedEnvelope3D bounds = new ReferencedEnvelope3D( 142892, 470783, 16, 142900, 470790, 20, CRS.decode("EPSG:7415")); // used to throw an exception here ReferencedEnvelope latLonBounds = cb.getLatLonBounds(bounds, bounds.getCoordinateReferenceSystem()); assertTrue( CRS.equalsIgnoreMetadata( CRS.decode("EPSG:4326"), latLonBounds.getCoordinateReferenceSystem())); // System.out.println(latLonBounds); } finally { MapProjection.SKIP_SANITY_CHECKS = false; } }
public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((_abstract == null) ? 0 : _abstract.hashCode()); result = prime * result + ((alias == null) ? 0 : alias.hashCode()); result = prime * result + ((description == null) ? 0 : description.hashCode()); result = prime * result + (enabled ? 1231 : 1237); result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((keywords == null) ? 0 : keywords.hashCode()); result = prime * result + ((latLonBoundingBox == null) ? 0 : latLonBoundingBox.hashCode()); result = prime * result + ((metadataLinks == null) ? 0 : metadataLinks.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((namespace == null) ? 0 : namespace.hashCode()); result = prime * result + ((nativeBoundingBox == null) ? 0 : nativeBoundingBox.hashCode()); result = prime * result + ((nativeCRS == null) ? 0 : nativeCRS.hashCode()); result = prime * result + ((nativeName == null) ? 0 : nativeName.hashCode()); result = prime * result + ((projectionPolicy == null) ? 0 : projectionPolicy.hashCode()); result = prime * result + ((srs == null) ? 0 : srs.hashCode()); result = prime * result + ((store == null) ? 0 : store.hashCode()); result = prime * result + ((title == null) ? 0 : title.hashCode()); return result; }
@Override public void createIndividuals() { try { coverageCRS = CRS.decode(crsCode, true); } catch (Exception e) { LOG.error("error decoding coordinate reference system code.", e); return; } if (boundsFromGraph) { // autowire graph service or pass in } gridEnvelope = new GridEnvelope2D(0, 0, cols, rows); refEnvelope = new ReferencedEnvelope(left, right, bottom, top, coverageCRS); gridGeometry = new GridGeometry2D(gridEnvelope, refEnvelope); super.createIndividuals0(); }
private ReferencedEnvelope aggregateEnvelopes(LayerInfo... layers) { ReferencedEnvelope eoExpected = new ReferencedEnvelope( layers[0].getResource().getNativeBoundingBox(), layers[0].getResource().getCRS()); for (int i = 1; i < layers.length; i++) { eoExpected.expandToInclude(layers[i].getResource().getNativeBoundingBox()); } return eoExpected; } }
/** Initializes a feature type object setting any info that has not been set. */ public void initFeatureType(FeatureTypeInfo featureType) throws Exception { if (featureType.getCatalog() == null) { featureType.setCatalog(catalog); } initResourceInfo(featureType); // setup the srs if missing if (featureType.getSRS() == null) { lookupSRS(featureType, true); } if (featureType.getProjectionPolicy() == null) { setupProjectionPolicy(featureType); } // deal with bounding boxes as possible CoordinateReferenceSystem crs = featureType.getCRS(); if (featureType.getLatLonBoundingBox() == null && featureType.getNativeBoundingBox() == null) { // both missing, we compute them setupBounds(featureType); } else if (featureType.getLatLonBoundingBox() == null) { // native available but geographic to be computed setupBounds(featureType); } else if (featureType.getNativeBoundingBox() == null && crs != null) { // we know the geographic and we can reproject back to native ReferencedEnvelope boundsLatLon = featureType.getLatLonBoundingBox(); featureType.setNativeBoundingBox(boundsLatLon.transform(crs, true)); } }
Envelope crsEnvelope = CRS.getEnvelope(crs); if (crsEnvelope != null) { crsReferencedEnvelope = new ReferencedEnvelope(crsEnvelope);
public void setNativeBox(Catalog catalog, String name) throws Exception { FeatureTypeInfo fti = catalog.getFeatureTypeByName(name); fti.setNativeBoundingBox(fti.getFeatureSource(null, null).getBounds()); fti.setLatLonBoundingBox( new ReferencedEnvelope(fti.getNativeBoundingBox(), DefaultGeographicCRS.WGS84)); catalog.save(fti); }
@Test public void testUseCRSBounds() throws NoSuchAuthorityCodeException, FactoryException { // this test is almost trivial since the code itself is trivial CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326"); LayerGroupHelper helper = new LayerGroupHelper(nested); helper.calculateBoundsFromCRS(targetCRS); // layer group bounds should now match target CRS bounds assertEquals(nested.getBounds(), new ReferencedEnvelope(CRS.getEnvelope(targetCRS))); // null CRS should get null bounds helper.calculateBoundsFromCRS(null); assertEquals(nested.getBounds(), null); }
public void testComposeOne() { CompositeFeatureCollection fc = new CompositeFeatureCollection( Arrays.asList(DataUtilities.collection(riverFeatures[0]))); assertEquals(1, fc.size()); assertEquals( new ReferencedEnvelope(5, 13, 3, 7, riverType.getCoordinateReferenceSystem()), fc.getBounds()); assertEquals(riverFeatures[0], DataUtilities.first(fc)); }
public void testComposeMany() { SimpleFeatureCollection roads = DataUtilities.collection(roadFeatures); SimpleFeatureCollection rivers = DataUtilities.collection(riverFeatures); SimpleFeatureCollection lakes = DataUtilities.collection(lakeFeatures); CompositeFeatureCollection fc = new CompositeFeatureCollection(Arrays.asList(roads, rivers, lakes)); // 3 roads, 2 rivers, 1 lake assertEquals(6, fc.size()); assertEquals( new ReferencedEnvelope(1, 16, 0, 10, riverType.getCoordinateReferenceSystem()), fc.getBounds()); assertEquals(roadFeatures[0], DataUtilities.first(fc)); } }