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); }
/** * Utility method to create a ReferencedEnvelope from an opengis Envelope class, supporting 2d * as well as 3d envelopes (returning the right class). * * @param env The opgenis Envelope object * @return ReferencedEnvelope, ReferencedEnvelope3D if it is 3d */ public static ReferencedEnvelope create(ReferencedEnvelope env, CoordinateReferenceSystem crs) { return create((org.opengis.geometry.Envelope) env, crs); } /**
/** Returns the specified bounding box as a JTS envelope. */ private static Envelope getJTSEnvelope(final BoundingBox bbox) { if (bbox == null) { throw new NullPointerException("Provided bbox envelope was null"); } if (bbox instanceof Envelope) { return (Envelope) bbox; } // safe creation if empty bounds return ReferencedEnvelope.create(bbox, bbox.getCoordinateReferenceSystem()); }
/** * Transforms the envelope from its current crs to {@link DefaultGeographicCRS#WGS84}. If the * specified envelope is already in WGS84, then it is returned unchanged. * * <p>The method {@link CRS#equalsIgnoreMetadata(Object, Object)} is used to compare the numeric * values and axis order (so {@code CRS.decode("CRS.84")} or {@code CRS.decode("4326",true)} * provide an appropriate match). * * @param envelope The envelope to transform. * @param crs The CRS the envelope is currently in. * @return The envelope transformed to be in {@link DefaultGeographicCRS#WGS84}. * @throws TransformException If at least one coordinate can't be transformed. */ public static Envelope toGeographic( final Envelope envelope, final CoordinateReferenceSystem crs) throws TransformException { if (CRS.equalsIgnoreMetadata(crs, DefaultGeographicCRS.WGS84)) { if (envelope instanceof ReferencedEnvelope) { return envelope; } return ReferencedEnvelope.create(envelope, DefaultGeographicCRS.WGS84); } ReferencedEnvelope initial = ReferencedEnvelope.create(envelope, crs); return toGeographic(initial); } /**
public BoundingBox getBounds() { Object obj = getDefaultGeometry(); if (obj instanceof Geometry) { Geometry geometry = (Geometry) obj; return ReferencedEnvelope.create( geometry.getEnvelopeInternal(), featureType.getCoordinateReferenceSystem()); } return ReferencedEnvelope.create(featureType.getCoordinateReferenceSystem()); }
@Override public BoundingBox getBounds() { Object o = getDefaultGeometry(); if (o instanceof Geometry) { CoordinateReferenceSystem crs = featureType.getCoordinateReferenceSystem(); if (crs == null) { crs = DefaultGeographicCRS.WGS84; } Envelope bounds = ReferencedEnvelope.create(crs); bounds.init(JTS.bounds((Geometry) o, crs)); return (BoundingBox) bounds; } return null; }
public MBTilesReader(Object source, Hints hints) throws IOException { sourceFile = MBTilesFormat.getFileFromSource(source); MBTilesFile file = new MBTilesFile(sourceFile); metadata = file.loadMetaData(); try { bounds = ReferencedEnvelope.create(metadata.getBounds(), WGS_84) .transform(SPHERICAL_MERCATOR, true); } catch (Exception e) { bounds = null; } originalEnvelope = new GeneralEnvelope(bounds == null ? WORLD_ENVELOPE : bounds); long maxZoom; try { maxZoom = file.maxZoom(); } catch (SQLException e) { throw new IOException(e); } long size = Math.round(Math.pow(ZOOM_LEVEL_BASE, maxZoom)) * DEFAULT_TILE_SIZE; highestRes = new double[] {WORLD_ENVELOPE.getSpan(0) / size, WORLD_ENVELOPE.getSpan(1) / size}; originalGridRange = new GridEnvelope2D(new Rectangle((int) size, (int) size)); coverageFactory = CoverageFactoryFinder.getGridCoverageFactory(this.hints); crs = SPHERICAL_MERCATOR; }
@Test public void testFactoryMethod() throws Exception { try { new ReferencedEnvelope(DefaultGeographicCRS.WGS84_3D); fail("ReferencedEnvelope should not be able to represent 3D CRS such as GDA94"); } catch (Exception expected) { } ReferencedEnvelope bounds2 = ReferencedEnvelope.create(DefaultGeographicCRS.WGS84_3D); assertNotNull(bounds2); }
ResultSet rs = null; ReferencedEnvelope bounds = ReferencedEnvelope.create(featureType.getCoordinateReferenceSystem()); try {
public BoundingBox getBounds() { // TODO: cache this value CoordinateReferenceSystem crs = featureType.getCoordinateReferenceSystem(); Envelope bounds = ReferencedEnvelope.create(crs); for (Object o : values) { if (o instanceof Geometry) { Geometry g = (Geometry) o; // TODO: check userData for crs... and ensure its of the same // crs as the feature type if (bounds.isNull()) { bounds.init(JTS.bounds(g, crs)); } else { bounds.expandToInclude(JTS.bounds(g, crs)); } } } return (BoundingBox) bounds; }
public Object visit(BBOX filter, Object extraData) { // if no srs is specified we can't transform anyways String srs = filter.getSRS(); if (srs != null && !"".equals(srs.trim())) return super.visit(filter, extraData); if (defaultCrs == null || filter.getBounds() == null || defaultCrs.getCoordinateSystem().getDimension() == filter.getBounds().getDimension()) { return getFactory(extraData) .bbox( filter.getExpression1(), ReferencedEnvelope.create(filter.getBounds(), defaultCrs)); } else { try { SingleCRS horizontalCRS = CRS.getHorizontalCRS(defaultCrs); ReferencedEnvelope bounds = ReferencedEnvelope.create(filter.getBounds(), horizontalCRS); return getFactory(extraData).bbox(filter.getExpression1(), bounds); } catch (Exception e) { throw new RuntimeException("Could not decode srs '" + srs + "'", e); } } }
final MathTransform transform = CRS.findMathTransform(coverageCRS, coverageCRS2D); final GeneralEnvelope bbox = CRS.transform(transform, coverageEnvelope); coverageBBox = ReferencedEnvelope.create(bbox, coverageCRS2D); } else { ReferencedEnvelope.create( coverageEnvelope, coverageEnvelope.getCoordinateReferenceSystem());
return result; } else { return ReferencedEnvelope.create(getSchema().getCoordinateReferenceSystem());
/** * Calculate bounds from features * * @return */ protected ReferencedEnvelope calculateBounds() { ReferencedEnvelope extent = ReferencedEnvelope.create(getSchema().getCoordinateReferenceSystem()); for (SimpleFeature feature : list) { if (feature == null) { continue; } ReferencedEnvelope bbox = ReferencedEnvelope.reference(feature.getBounds()); if (bbox == null || bbox.isEmpty() || bbox.isNull()) { continue; } extent.expandToInclude(bbox); } return extent; }
@Override protected ReferencedEnvelope getBoundsInternal(Query query) throws IOException { if (query.getFilter() == Filter.INCLUDE) { // filtering not implemented ReferencedEnvelope bounds = ReferencedEnvelope.create(getSchema().getCoordinateReferenceSystem()); FeatureReader<SimpleFeatureType, SimpleFeature> featureReader = getReaderInternal(query); try { while (featureReader.hasNext()) { SimpleFeature feature = featureReader.next(); bounds.include(feature.getBounds()); } } finally { featureReader.close(); } return bounds; } return null; // feature by feature scan required to count records }
@Override protected ReferencedEnvelope getBoundsInternal(Query query) throws IOException { if (query.getFilter() == Filter.INCLUDE) { // filtering not implemented FeatureReader<SimpleFeatureType, SimpleFeature> featureReader = getReaderInternal(query); CoordinateReferenceSystem crs = featureReader.getFeatureType().getCoordinateReferenceSystem(); ReferencedEnvelope bounds = ReferencedEnvelope.create(crs); try { while (featureReader.hasNext()) { SimpleFeature feature = featureReader.next(); bounds.include(feature.getBounds()); } } finally { featureReader.close(); } return bounds; } return null; // feature by feature scan required to count records }
/** * Tests that the conversion of different bound types to ReferencedEnvelope does not lose the * emptiness property * * @throws Exception */ @Test public void testEmptyEnvelopeConversion() throws Exception { // conversion of an empty OGC envelope should stay empty GeneralEnvelope ge = new GeneralEnvelope(new double[] {0, 0}, new double[] {-1, -1}); assertTrue(ge.isEmpty()); assertTrue(ReferencedEnvelope.create(ge, ge.getCoordinateReferenceSystem()).isEmpty()); assertTrue(ReferencedEnvelope.reference(ge).isEmpty()); // conversion of an empty Java Rectangle 2D should stay empty Rectangle2D r2d = new Rectangle2D.Double(0, 0, -1, -1); assertTrue(r2d.isEmpty()); assertTrue(ReferencedEnvelope.create(r2d, null).isEmpty()); // conversion of an empty ReferencedEnvelope should stay empty ReferencedEnvelope re = new ReferencedEnvelope(); assertTrue(re.isEmpty()); assertTrue(ReferencedEnvelope.create(re).isEmpty()); assertTrue(ReferencedEnvelope.create(re, re.getCoordinateReferenceSystem()).isEmpty()); assertTrue(ReferencedEnvelope.reference(re).isEmpty()); } }
ReferencedEnvelope.create(getSchema().getCoordinateReferenceSystem()); if (state.hasListener()) {
try { requestedEnvelope = ReferencedEnvelope.create( gg.getEnvelope(), gg.getCoordinateReferenceSystem()) .transform(crs, true);
ReferencedEnvelope.create(getSchema().getCoordinateReferenceSystem()); if (state.hasListener()) {