private <P extends Position> boolean hasEmpty(Geometry<P> geometry) { if (geometry.isEmpty()) { return true; } if (geometry instanceof GeometryCollection) { for (Geometry<P> part : (GeometryCollection<P, ?>) geometry) { if (hasEmpty(part)) return true; } } return false; } }
/** * @inheritDoc */ @Override public <P extends C2D & Measured> double measureAt(final Geometry<P> geometry, final P pos, double tolerance) { if (geometry == null || pos == null) throw new IllegalArgumentException("Parameters must not be NULL"); if (geometry.isEmpty()) return Double.NaN; InterpolatingVisitor<P> visitor = new InterpolatingVisitor<P>(pos, tolerance); geometry.accept(visitor); return visitor.m(); }
protected int getGeometryType(Geometry<P> geometry) { //empty geometries have the same representation as an empty geometry collection if (geometry.isEmpty()) { return WkbGeometryType.GEOMETRY_COLLECTION.getTypeCode(); } WkbGeometryType type = WkbGeometryType.forClass(geometry.getClass()); if (type == null) { throw new UnsupportedConversionException( String.format( "Can't convert geometries of type %s", geometry.getClass().getCanonicalName() ) ); } return type.getTypeCode(); }
@Override public Double execute() { if (geometry == null) { throw new IllegalArgumentException("Operation expects a non-empty geometry"); } if (geometry.isEmpty()) { return Double.NaN; } FindExtremumMeasureVisitor<P> visitor = new FindExtremumMeasureVisitor<P>(min); geometry.getPositions().accept(visitor); return visitor.extremum; } };
@Override public Geometry<M> execute() { Geometry<M> measured = Geometry.forceToCrs(geometry, measuredVariant); if (measured.isEmpty()) return measured; if (keepBeginMeasure) { double initialValue = measured.getPositionN(0).getM(); length = (Double.isNaN(initialValue) ? 0 : initialValue); } if (measured instanceof LineString) { return measure((LineString<M>) measured); } else if (geometry instanceof MultiLineString) { return measure((MultiLineString<M>) measured); } else { throw new IllegalStateException( String.format("Requires a LineString or MultiLineString, but received %s", geometry.getClass().getName())); } }
@Override protected void encode(Geometry<?> geom, int parentShapeIndex, CountingPositionSequenceBuilder<?> coordinates, List<Figure> figures, List<Shape> shapes) { if ( geom.isEmpty() ) { shapes.add( new Shape( parentShapeIndex, -1, this.openGisType ) ); return; } int thisShapeIndex = shapes.size(); Shape thisShape = createShape( parentShapeIndex, figures ); shapes.add( thisShape ); if (! (geom instanceof GeometryCollection)) { throw new IllegalArgumentException( "Expect GeometryCollection argument." ); } GeometryCollection gc = (GeometryCollection) geom; for ( int i = 0; i < gc.getNumGeometries(); i++ ) { Geometry component = gc.getGeometryN( i ); encodeComponent( component, thisShapeIndex, coordinates, figures, shapes ); } }
@Override protected void encode(Geometry<?> geom, int parentShapeIndex, CountingPositionSequenceBuilder<?> coordinates, List<Figure> figures, List<Shape> shapes) { if ( !( geom instanceof Polygon ) ) { throw new IllegalArgumentException( "Polygon geometry expected." ); } if ( geom.isEmpty() ) { shapes.add( new Shape( parentShapeIndex, -1, OpenGisType.POLYGON ) ); return; } Polygon polygon = (Polygon) geom; int figureOffset = figures.size(); shapes.add( new Shape( parentShapeIndex, figureOffset, OpenGisType.POLYGON ) ); int pointOffset = coordinates.getNumAdded(); addExteriorRing( polygon, coordinates, figures ); addInteriorRings( polygon, coordinates, figures ); }
@Override public <P extends C2D & Measured> Geometry<P> locateBetween(final Geometry<P> geometry, final double startMeasure, final double endMeasure) { if (geometry == null) throw new IllegalArgumentException("Null geometries not allowed."); if (geometry.isEmpty()) return new Point<P>(geometry.getCoordinateReferenceSystem()); if (C2D.class.isAssignableFrom(geometry.getPositionClass()) && Measured.class.isAssignableFrom(geometry.getPositionClass())) { MeasureInterpolatingVisitor visitor = new MeasureInterpolatingVisitor(geometry, startMeasure, endMeasure); geometry.accept((GeometryVisitor<P>) visitor); return (Geometry<P>) visitor.result(); } throw new IllegalArgumentException("Requires projected coordinates"); }
@Override public <P extends C2D> boolean relates(final Geometry<P> geometry, final Geometry<P> other, final String matrix) { if (geometry.isEmpty() || other.isEmpty()) return Boolean.FALSE; checkCompatibleCRS(geometry, other); final RelateOp relateOp = new RelateOp(JTS.to(geometry), JTS.to(other)); return relateOp.getIntersectionMatrix().matches(matrix); }
@Override public <P extends C2D> boolean intersects(final Geometry<P> geometry, final Geometry<P> other) { if (geometry.isEmpty() || other.isEmpty()) return Boolean.FALSE; checkCompatibleCRS(geometry, other); if (!envelopeIntersect(geometry, other)) return Boolean.FALSE; RelateOp relateOp = new RelateOp(JTS.to(geometry), JTS.to(other)); return relateOp.getIntersectionMatrix().isIntersects(); }
@Override protected void encode(Geometry<?> geom, int parentIdx, CountingPositionSequenceBuilder<?> coordinates, List <Figure> figures, List<Shape> shapes) { if (!(geom instanceof Point)) { throw new IllegalArgumentException("Require Point geometry"); } if (geom.isEmpty()) { shapes.add(new Shape(parentIdx, -1, OpenGisType.POINT)); return; } int pntOffset = coordinates.getNumAdded(); int figureOffset = figures.size(); double[] c = new double[coordinates.getCoordinateDimension()]; coordinates.add(geom.getPositionN(0).toArray(c)); Figure figure = new Figure(FigureAttribute.Stroke, pntOffset); figures.add(figure); Shape shape = new Shape(parentIdx, figureOffset, OpenGisType.POINT); shapes.add(shape); }
@Override public <P extends C2D> Geometry<P> union(final Geometry<P> geometry, final Geometry<P> other) { checkCompatibleCRS(geometry, other); if (geometry.isEmpty()) return other; if (other.isEmpty()) return geometry; checkNotGeometryCollection(geometry); checkNotGeometryCollection(other); org.locationtech.jts.geom.Geometry union = SnapIfNeededOverlayOp.overlayOp(JTS.to(geometry), JTS.to(other), OverlayOp.UNION); return JTS.from(union, geometry.getCoordinateReferenceSystem()); }
@Override public <P extends C2D> Geometry<P> symmetricDifference(final Geometry<P> geometry, final Geometry<P> other) { checkCompatibleCRS(geometry, other); if (geometry.isEmpty()) return other; if (other.isEmpty()) return geometry; checkNotGeometryCollection(geometry); checkNotGeometryCollection(other); org.locationtech.jts.geom.Geometry symDifference = SnapIfNeededOverlayOp.overlayOp(JTS.to(geometry), JTS.to(other), OverlayOp.SYMDIFFERENCE); return JTS.from(symDifference, geometry.getCoordinateReferenceSystem()); }
@Override public <P extends C2D> boolean overlaps(final Geometry<P> geometry, final Geometry<P> other) { if (geometry.isEmpty() || other.isEmpty()) return Boolean.FALSE; checkCompatibleCRS(geometry, other); if (!envelopeIntersect(geometry, other)) return Boolean.FALSE; final RelateOp relateOp = new RelateOp(JTS.to(geometry), JTS.to(other)); return relateOp.getIntersectionMatrix().isOverlaps(geometry.getDimension(), other.getDimension()); }
@Override public <P extends C2D> boolean touches(final Geometry<P> geometry, final Geometry<P> other) { if (geometry.isEmpty() || other.isEmpty()) return Boolean.FALSE; checkCompatibleCRS(geometry, other); if (!envelopeIntersect(geometry, other)) return Boolean.FALSE; final RelateOp relateOp = new RelateOp(JTS.to(geometry), JTS.to(other)); return relateOp.getIntersectionMatrix().isTouches(geometry.getDimension(), other.getDimension()); }
@Override public <P extends C2D> boolean contains(final Geometry<P> geometry, final Geometry<P> other) { if (geometry.isEmpty() || other.isEmpty()) return Boolean.FALSE; checkCompatibleCRS(geometry, other); if (!geometry.getEnvelope().contains(other.getEnvelope())) return Boolean.FALSE; final RelateOp relateOp = new RelateOp(JTS.to(geometry), JTS.to(other)); return relateOp.getIntersectionMatrix().isContains(); }
@Override public <P extends C2D> boolean crosses(final Geometry<P> geometry, final Geometry<P> other) { if (geometry.isEmpty() || other.isEmpty()) return Boolean.FALSE; checkCompatibleCRS(geometry, other); if (!envelopeIntersect(geometry, other)) return Boolean.FALSE; final RelateOp relateOp = new RelateOp(JTS.to(geometry), JTS.to(other)); return relateOp.getIntersectionMatrix().isCrosses(geometry.getDimension(), other.getDimension()); }
@Override public <P extends C2D> Geometry<P> difference(final Geometry<P> geometry, final Geometry<P> other) { checkCompatibleCRS(geometry, other); if (geometry.isEmpty()) return new Point<P>(geometry.getCoordinateReferenceSystem()); if (other.isEmpty()) return geometry; checkNotGeometryCollection(geometry); checkNotGeometryCollection(other); org.locationtech.jts.geom.Geometry difference = SnapIfNeededOverlayOp.overlayOp(JTS.to(geometry), JTS.to(other), OverlayOp.DIFFERENCE); return JTS.from(difference, geometry.getCoordinateReferenceSystem()); }
@Override public <P extends C2D> Geometry<P> intersection(final Geometry<P> geometry, final Geometry<P> other) { checkCompatibleCRS(geometry, other); if (geometry.isEmpty() || other.isEmpty()) return new Point<P>(geometry.getCoordinateReferenceSystem()); checkNotGeometryCollection(geometry); checkNotGeometryCollection(other); org.locationtech.jts.geom.Geometry intersection = SnapIfNeededOverlayOp.overlayOp(JTS.to(geometry), JTS.to(other), OverlayOp.INTERSECTION); return JTS.from(intersection, geometry.getCoordinateReferenceSystem()); }
/** * Returns the {@code Envelope} of this instance. * @return the {@code Envelope} of this instance. */ public Envelope<P> getEnvelope() { if (isEmpty()) return new Envelope<P>(getCoordinateReferenceSystem()); PositionSequence<P> positions = getPositions(); EnvelopeVisitor<P> visitor = new EnvelopeVisitor<P>(getCoordinateReferenceSystem()); positions.accept(visitor); return visitor.result(); }