@Override public <X> X unwrap(Geometry value, Class<X> type, WrapperOptions options) { if ( value == null ) { return null; } if ( Geometry.class.isAssignableFrom( type ) ) { return (X) value; } if ( org.geolatte.geom.Geometry.class.isAssignableFrom( type ) ) { return (X) JTS.from( value ); } if ( String.class.isAssignableFrom( type ) ) { return (X) toString( value ); } throw unknownUnwrap( type ); }
@Override public <X> Geometry wrap(X value, WrapperOptions options) { if ( value == null ) { return null; } if ( Geometry.class.isInstance( value ) ) { return (Geometry) value; } if ( String.class.isInstance( value ) ) { return fromString( (String) value ); } if ( org.locationtech.jts.geom.Geometry.class.isInstance( value ) ) { return JTS.from( (org.locationtech.jts.geom.Geometry) value ); } throw unknownWrap( value.getClass() ); } }
return from((Point) jtsGeometry, crs); } else if (jtsGeometry instanceof LineString) { return from((LineString) jtsGeometry, crs); } else if (jtsGeometry instanceof Polygon) { return from((Polygon) jtsGeometry, crs); } else if (jtsGeometry instanceof MultiPoint) { return from((MultiPoint) jtsGeometry, crs); } else if (jtsGeometry instanceof MultiLineString) { return from((MultiLineString) jtsGeometry, crs); } else if (jtsGeometry instanceof MultiPolygon) { return from((MultiPolygon) jtsGeometry, crs); } else if (jtsGeometry instanceof GeometryCollection) { return from((GeometryCollection) jtsGeometry, crs); } else { throw new JTSConversionException();
@Override @SuppressWarnings("unchecked") public <P extends C2D, G extends Geometry<P> & Polygonal<P>> Point<P> centroid(final G geometry) { return (Point<P>) JTS.from(JTS.to(geometry).getCentroid()); }
@Override public org.geolatte.geom.Geometry<?> convertObject(Object value) { if (isEmpty(value)){ return getDefaultValue(); }else if (value instanceof org.geolatte.geom.Geometry){ return (org.geolatte.geom.Geometry<?>)value; }else if (value instanceof String){ return Wkt.fromWkt((String)value); }else if (value instanceof com.vividsolutions.jts.geom.Geometry){ return JTS.from((com.vividsolutions.jts.geom.Geometry)value); } throw new IllegalArgumentException("value="+value); }
@Override public <P extends C2D> Geometry<P> boundary(final Geometry<P> geometry) { final BoundaryOp boundaryOp = new BoundaryOp(JTS.to(geometry)); final CoordinateReferenceSystem<P> crs = geometry.getCoordinateReferenceSystem(); return JTS.from(boundaryOp.getBoundary(), crs); }
@Override public <P extends C2D> Geometry<P> buffer(final Geometry<P> geometry, final double distance) { final BufferOp op = new BufferOp(JTS.to(geometry)); return JTS.from(op.getResultGeometry(distance), geometry.getCoordinateReferenceSystem()); }
@Override public <P extends C2D> Geometry<P> convexHull(final Geometry<P> geometry) { final ConvexHull convexHull = new ConvexHull(JTS.to(geometry)); return JTS.from(convexHull.getConvexHull(), geometry.getCoordinateReferenceSystem()); }
@SuppressWarnings("unchecked") private static <P extends Position> org.geolatte.geom.MultiLineString<P> from(MultiLineString jtsGeometry, CoordinateReferenceSystem<P> crs) { if (jtsGeometry.getNumGeometries() == 0) return new org.geolatte.geom.MultiLineString<P>(crs); org.geolatte.geom.LineString<P>[] linestrings = new org.geolatte.geom.LineString[jtsGeometry.getNumGeometries ()]; for (int i = 0; i < linestrings.length; i++) { linestrings[i] = from((LineString) jtsGeometry.getGeometryN(i), crs); } return new org.geolatte.geom.MultiLineString<P>(linestrings); }
/** * Primary Factory method that converts a JTS geometry into an equivalent geolatte geometry * * @param jtsGeometry the jts geometry to convert * @return an equivalent geolatte geometry * @throws IllegalArgumentException when a null object is passed */ public static org.geolatte.geom.Geometry<?> from(org.locationtech.jts.geom.Geometry jtsGeometry) { if (jtsGeometry == null) { throw new IllegalArgumentException("Null object passed."); } Coordinate testCo = jtsGeometry.getCoordinate(); boolean is3D = !(testCo == null || Double.isNaN(testCo.z)); CoordinateReferenceSystem<?> crs = CrsRegistry.ifAbsentReturnProjected2D(jtsGeometry.getSRID()); if (is3D) { crs = CoordinateReferenceSystems.addVerticalSystem(crs, LinearUnit.METER); } // to translate measure, add Measure as LinearSystem boolean hasM = isMeasuredCoordinate(testCo) && !Double.isNaN(testCo.getM()); if (hasM) { crs = CoordinateReferenceSystems.addLinearSystem(crs, LinearUnit.METER); } return from(jtsGeometry, crs); }
@SuppressWarnings("unchecked") private static <P extends Position> org.geolatte.geom.MultiPoint<P> from(MultiPoint jtsMultiPoint, CoordinateReferenceSystem<P> crs) { if (jtsMultiPoint == null || jtsMultiPoint.getNumGeometries() == 0) return new org.geolatte.geom.MultiPoint<P>(crs); org.geolatte.geom.Point<P>[] points = new org.geolatte.geom.Point[jtsMultiPoint.getNumGeometries()]; for (int i = 0; i < points.length; i++) { points[i] = from((Point) jtsMultiPoint.getGeometryN(i), crs); } return new org.geolatte.geom.MultiPoint<P>(points); }
@SuppressWarnings("unchecked") private static <P extends Position> org.geolatte.geom.Polygon<P> from(Polygon jtsGeometry, CoordinateReferenceSystem<P> crs) { if (jtsGeometry.isEmpty()) { return new org.geolatte.geom.Polygon<P>(crs); } org.geolatte.geom.LinearRing<P>[] rings = new org.geolatte.geom.LinearRing[jtsGeometry.getNumInteriorRing() + 1]; org.geolatte.geom.LineString<P> extRing = from(jtsGeometry.getExteriorRing(), crs); rings[0] = new org.geolatte.geom.LinearRing(extRing.getPositions(), extRing.getCoordinateReferenceSystem()); for (int i = 1; i < rings.length; i++) { org.geolatte.geom.LineString intRing = from(jtsGeometry.getInteriorRingN(i - 1), crs); rings[i] = new org.geolatte.geom.LinearRing(intRing); } return new org.geolatte.geom.Polygon(rings); }
@SuppressWarnings("unchecked") private static <P extends Position> org.geolatte.geom.MultiPolygon<P> from(MultiPolygon jtsGeometry, CoordinateReferenceSystem<P> crs) { if (jtsGeometry.getNumGeometries() == 0) return new org.geolatte.geom.MultiPolygon<P>(crs); org.geolatte.geom.Polygon<P>[] polygons = (org.geolatte.geom.Polygon<P>[]) new org.geolatte.geom .Polygon[jtsGeometry.getNumGeometries()]; for (int i = 0; i < jtsGeometry.getNumGeometries(); i++) { polygons[i] = from((Polygon) jtsGeometry.getGeometryN(i), crs); } return new org.geolatte.geom.MultiPolygon<P>(polygons); }
@SuppressWarnings("unchecked") private static <P extends Position> org.geolatte.geom.GeometryCollection<P, org.geolatte.geom.Geometry<P>> from (GeometryCollection jtsGeometry, CoordinateReferenceSystem<P> crs) { if (jtsGeometry.getNumGeometries() == 0) return new org.geolatte.geom.GeometryCollection<P, org.geolatte.geom.Geometry<P>>(crs); org.geolatte.geom.Geometry<P>[] geoms = new org.geolatte.geom.Geometry[jtsGeometry.getNumGeometries()]; for (int i = 0; i < jtsGeometry.getNumGeometries(); i++) { geoms[i] = from(jtsGeometry.getGeometryN(i), crs); } return new org.geolatte.geom.GeometryCollection<P, org.geolatte.geom.Geometry<P>>(geoms); }
@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> 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()); }