/** * Test if this geometry envelope intersects with the other geometry * envelope. * * @param r the other geometry * @return true if the two overlap */ public boolean intersectsBoundingBox(ValueGeometry r) { // the Geometry object caches the envelope return getGeometryNoCopy().getEnvelopeInternal().intersects( r.getGeometryNoCopy().getEnvelopeInternal()); }
@SqlNullable @Description("Returns a float between 0 and 1 representing the location of the closest point on the LineString to the given Point, as a fraction of total 2d line length.") @ScalarFunction("line_locate_point") @SqlType(DOUBLE) public static Double lineLocatePoint(@SqlType(GEOMETRY_TYPE_NAME) Slice lineSlice, @SqlType(GEOMETRY_TYPE_NAME) Slice pointSlice) { Geometry line = JtsGeometrySerde.deserialize(lineSlice); Geometry point = JtsGeometrySerde.deserialize(pointSlice); if (line.isEmpty() || point.isEmpty()) { return null; } GeometryType lineType = GeometryType.getForJtsGeometryType(line.getGeometryType()); if (lineType != GeometryType.LINE_STRING && lineType != GeometryType.MULTI_LINE_STRING) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("First argument to line_locate_point must be a LineString or a MultiLineString. Got: %s", line.getGeometryType())); } GeometryType pointType = GeometryType.getForJtsGeometryType(point.getGeometryType()); if (pointType != GeometryType.POINT) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Second argument to line_locate_point must be a Point. Got: %s", point.getGeometryType())); } return new LengthIndexedLine(line).indexOf(point.getCoordinate()) / line.getLength(); }
private static void writePolyline(Geometry geometry, SliceOutput output, boolean multitype) { int numParts; int numPoints = geometry.getNumPoints(); if (multitype) { numParts = geometry.getNumGeometries(); output.writeByte(GeometrySerializationType.MULTI_LINE_STRING.code()); } else { numParts = numPoints > 0 ? 1 : 0; output.writeByte(GeometrySerializationType.LINE_STRING.code()); } output.writeInt(EsriShapeType.POLYLINE.code); writeEnvelope(geometry, output); output.writeInt(numParts); output.writeInt(numPoints); int partIndex = 0; for (int i = 0; i < numParts; i++) { output.writeInt(partIndex); partIndex += geometry.getGeometryN(i).getNumPoints(); } writeCoordinates(geometry.getCoordinates(), output); }
private boolean testVerticesEquality(Geometry geom1, Geometry geom2) { if ( geom1.getNumPoints() != geom2.getNumPoints() ) { return false; } for ( int i = 0; i < geom1.getNumPoints(); i++ ) { Coordinate cn1 = geom1.getCoordinates()[i]; Coordinate cn2 = geom2.getCoordinates()[i]; if ( !cn1.equals2D( cn2 ) ) { return false; } } return true; } }
protected boolean testTypeAndVertexEquality(Geometry geom1, Geometry geom2) { if ( !geom1.getGeometryType().equals( geom2.getGeometryType() ) ) { return false; } if ( geom1.getNumGeometries() != geom2.getNumGeometries() ) { return false; } if ( geom1.getNumPoints() != geom2.getNumPoints() ) { return false; } Coordinate[] coordinates1 = geom1.getCoordinates(); Coordinate[] coordinates2 = geom2.getCoordinates(); for ( int i = 0; i < coordinates1.length; i++ ) { Coordinate c1 = coordinates1[i]; Coordinate c2 = coordinates2[i]; if ( !testCoordinateEquality( c1, c2 ) ) { return false; } } return true; }
private Geometry clipToWorldFeatureTypeGeometry(Geometry geom) { // Oracle cannot deal with filters using geometries that span beyond the whole world if (isFeatureTypeGeometryGeodetic() && !WORLD.contains(geom.getEnvelopeInternal())) { Geometry result = geom.intersection(JTS.toGeometry(WORLD)); if (result != null && !result.isEmpty()) { if (result instanceof GeometryCollection) { result = distillSameTypeGeometries((GeometryCollection) result, geom); } return result; } } return geom; }
@Test public void smoothLinearRing() { Coordinate[] coords = getPolyCoords(); LineString line = factory.createLinearRing(coords); Geometry smoothed = JTS.smooth(line, 0); assertTrue(smoothed instanceof LinearRing); CoordList list = new CoordList(smoothed.getCoordinates()); assertTrue(list.containsAll(coords)); Envelope lineEnv = line.getEnvelopeInternal(); Envelope smoothEnv = smoothed.getEnvelopeInternal(); assertTrue(smoothEnv.covers(lineEnv)); }
private static void writeGeometryCollection(Geometry collection, DynamicSliceOutput output) { output.appendByte(GeometrySerializationType.GEOMETRY_COLLECTION.code()); for (int geometryIndex = 0; geometryIndex < collection.getNumGeometries(); geometryIndex++) { Geometry geometry = collection.getGeometryN(geometryIndex); int startPosition = output.size(); // leave 4 bytes for the shape length output.appendInt(0); writeGeometry(geometry, output); int endPosition = output.size(); int length = endPosition - startPosition - Integer.BYTES; output.getUnderlyingSlice().setInt(startPosition, length); } }
clipped = clipper.clip(geom, true); } else { if (geom.getEnvelopeInternal().intersects(clip.getEnvelopeInternal())) { clipped = clip.intersection(geom); new GeometryDimensionCollector(geom.getDimension()); clipped.apply(collector); Geometry result = collector.collect(); if (result == null) { if (preserveZ && !geom.equalsExact(clipped)) { if (result.getDimension() == 2 || result.getDimension() == 0) { result.apply(new IDWElevationInterpolator(geom, crs)); } else if (result.getDimension() == 1) { result.apply(new LinearElevationInterpolator(geom, crs));
/** * This "pages" through standard geo boundaries offset by multiples of 360 * longitudinally that intersect geom, and the intersecting results of a page * and the geom are shifted into the standard -180 to +180 and added to a new * geometry that is returned. */ private static Geometry cutUnwrappedGeomInto360(Geometry geom) { Envelope geomEnv = geom.getEnvelopeInternal(); if (geomEnv.getMinX() >= -180 && geomEnv.getMaxX() <= 180) return geom; assert geom.isValid() : "geom"; //TODO opt: support geom's that start at negative pages -- // ... will avoid need to previously shift in unwrapDateline(geom). List<Geometry> geomList = new ArrayList<Geometry>(); //page 0 is the standard -180 to 180 range for (int page = 0; true; page++) { double minX = -180 + page * 360; if (geomEnv.getMaxX() <= minX) break; Geometry rect = geom.getFactory().toGeometry(new Envelope(minX, minX + 360, -90, 90)); assert rect.isValid() : "rect"; Geometry pageGeom = rect.intersection(geom);//JTS is doing some hard work assert pageGeom.isValid() : "pageGeom"; shiftGeomByX(pageGeom, page * -360); geomList.add(pageGeom); } return UnaryUnionOp.union(geomList); }
private static void writeEnvelope(Geometry geometry, SliceOutput output) { if (geometry.isEmpty()) { for (int i = 0; i < 4; i++) { output.writeDouble(NaN); } return; } Envelope envelope = geometry.getEnvelopeInternal(); output.writeDouble(envelope.getMinX()); output.writeDouble(envelope.getMinY()); output.writeDouble(envelope.getMaxX()); output.writeDouble(envelope.getMaxY()); }
public static ReferencedEnvelope reprojectEnvelope( ReferencedEnvelope sourceEnvelope, CoordinateReferenceSystem targetCRS, ReferencedEnvelope targetReferenceEnvelope) throws FactoryException, TransformException { Geometry reprojected = Utils.reprojectEnvelopeToGeometry( sourceEnvelope, targetCRS, targetReferenceEnvelope); if (reprojected == null) { return new ReferencedEnvelope(targetCRS); } else { if (reprojected.getNumGeometries() > 1) { return new ReferencedEnvelope( reprojected.getGeometryN(0).getEnvelopeInternal(), targetCRS); } else { return new ReferencedEnvelope(reprojected.getEnvelopeInternal(), targetCRS); } } }
@Test public void smoothMultiLineString() { LineString[] lines = new LineString[3]; lines[0] = factory.createLineString(getLineCoords(0)); lines[1] = factory.createLineString(getLineCoords(10)); lines[2] = factory.createLineString(getLineCoords(20)); MultiLineString mls = factory.createMultiLineString(lines); Geometry smoothed = JTS.smooth(mls, 0); assertTrue(smoothed instanceof MultiLineString); assertEquals(3, smoothed.getNumGeometries()); Envelope mlsEnv = mls.getEnvelopeInternal(); Envelope smoothEnv = smoothed.getEnvelopeInternal(); assertTrue(smoothEnv.covers(mlsEnv)); }
/** * Tests if a specified {@link Point2D}is inside the boundary of the <code>Shape</code>. * * @param p a specified <code>Point2D</code> * @return <code>true</code> if the specified <code>Point2D</code> is inside the boundary of the * <code>Shape</code>; <code>false</code> otherwise. */ public boolean contains(Point2D p) { Coordinate coord = new Coordinate(p.getX(), p.getY()); Geometry point = geometry.getFactory().createPoint(coord); return geometry.contains(point); }
private void initCRS(Geometry g) { // see if we have a native CRS in the mix if (crs == null && g.getUserData() instanceof CoordinateReferenceSystem) { crs = (CoordinateReferenceSystem) g.getUserData(); } if (srid == -1 && g.getSRID() > 0) { srid = g.getSRID(); } } }
@Override protected void visitLiteralGeometry(Literal expression) throws IOException { Geometry g = (Geometry) evaluateLiteral(expression, Geometry.class); if (g instanceof LinearRing) { // WKT does not support linear rings g = g.getFactory().createLineString(((LinearRing) g).getCoordinateSequence()); } out.write("ST_GeomFromText('" + g.toText() + "', " + currentSRID + ")"); }
@Override public void filter(Geometry gmtr) { if (MultiPolygon.class.isAssignableFrom(binding)) { if (gmtr.getArea() != 0.0d && gmtr.getGeometryType().equals("Polygon")) { collection.add(gmtr); } } if (MultiLineString.class.isAssignableFrom(binding)) { if (gmtr.getLength() != 0.0d && gmtr.getGeometryType().equals("LineString")) { collection.add(gmtr); } } if (MultiPoint.class.isAssignableFrom(binding)) { if (gmtr.getNumGeometries() > 0 && gmtr.getGeometryType().equals("Point")) { collection.add(gmtr); } } if (Point.class.isAssignableFrom(binding)) { if (gmtr.getGeometryType().equals("Point")) { collection.add(gmtr); } } }
@Override public ROI intersect(ROI roi) { final Geometry geom = getGeometry(roi); // is it a rectangle? if (geom != null && geom.equalsExact(geom.getEnvelope())) { GeometryClipper clipper = new GeometryClipper(geom.getEnvelopeInternal()); Geometry intersect = clipper.clip(getAsGeometry(), true); return new ROIGeometry(intersect); } else { return super.intersect(roi); } }
private static int getDimensionCount(Geometry geometry) { ZVisitor finder = new ZVisitor(); geometry.apply(finder); return finder.isFoundZ() ? 3 : 2; }