Search in sources :

Example 6 with GeometryWrapper

use of org.apache.sis.internal.feature.GeometryWrapper in project sis by apache.

the class Factory method parseWKB.

/**
 * Reads the given Well Known Binary (WKB).
 * This implementation does not change the buffer position.
 *
 * @param  data  the sequence of bytes to parse.
 * @return the geometry object for the given WKB.
 * @throws ParseException if the WKB can not be parsed.
 */
@Override
public GeometryWrapper<Geometry> parseWKB(final ByteBuffer data) throws ParseException {
    byte[] array;
    if (data.hasArray()) {
        /*
             * Try to use the underlying array without copy if possible.
             * Copy only if the position or length does not match.
             */
        array = data.array();
        int lower = data.arrayOffset();
        int upper = data.limit() + lower;
        lower += data.position();
        if (lower != 0 || upper != array.length) {
            array = Arrays.copyOfRange(array, lower, upper);
        }
    } else {
        array = new byte[data.remaining()];
        data.get(array);
    }
    // WKBReader(GeometryFactory) constructor is cheap.
    return new Wrapper(new WKBReader(factory).read(array));
}
Also used : GeometryWrapper(org.apache.sis.internal.feature.GeometryWrapper) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) WKBReader(org.locationtech.jts.io.WKBReader)

Example 7 with GeometryWrapper

use of org.apache.sis.internal.feature.GeometryWrapper in project sis by apache.

the class Factory method parseWKT.

/**
 * Parses the given Well Known Text (WKT).
 *
 * @param  wkt  the Well Known Text to parse.
 * @return the geometry object for the given WKT.
 * @throws ParseException if the WKT can not be parsed.
 */
@Override
public GeometryWrapper<Geometry> parseWKT(final String wkt) throws ParseException {
    // WKTReader(GeometryFactory) constructor is cheap.
    final WKTReader reader = new WKTReader(factory);
    reader.setIsOldJtsCoordinateSyntaxAllowed(false);
    return new Wrapper(reader.read(wkt));
}
Also used : GeometryWrapper(org.apache.sis.internal.feature.GeometryWrapper) WKTReader(org.locationtech.jts.io.WKTReader)

Example 8 with GeometryWrapper

use of org.apache.sis.internal.feature.GeometryWrapper in project sis by apache.

the class Factory method createMultiPolygon.

/**
 * Creates a multi-polygon from an array of JTS {@link Polygon} or {@link LinearRing}.
 * If some geometries are actually linear rings, they will be converted to polygons.
 *
 * @param  geometries  the polygons or linear rings to put in a multi-polygons.
 * @throws ClassCastException if an element in the array is not a JTS geometry.
 * @throws IllegalArgumentException if an element is a non-closed linear string.
 */
@Override
public GeometryWrapper<Geometry> createMultiPolygon(final Object[] geometries) {
    final Polygon[] polygons = new Polygon[geometries.length];
    boolean isFloat = true;
    for (int i = 0; i < geometries.length; i++) {
        final Object polyline = unwrap(geometries[i]);
        final Polygon polygon;
        if (polyline instanceof Polygon) {
            polygon = (Polygon) polyline;
        } else {
            final boolean fs;
            final CoordinateSequence cs;
            if (polyline instanceof LinearRing) {
                final LinearRing ring = (LinearRing) polyline;
                cs = ring.getCoordinateSequence();
                fs = isFloat(cs);
                polygon = factory(fs).createPolygon(ring);
            } else if (polyline instanceof LineString) {
                // Let JTS throws an exception with its own error message if the ring is not valid.
                cs = ((LineString) polyline).getCoordinateSequence();
                fs = isFloat(cs);
                polygon = factory(fs).createPolygon(cs);
            } else {
                throw new ClassCastException(Errors.format(Errors.Keys.IllegalArgumentClass_3, Strings.bracket("geometries", i), Polygon.class, Classes.getClass(polyline)));
            }
            JTS.copyMetadata((Geometry) polyline, polygon);
            isFloat &= fs;
        }
        polygons[i] = polygon;
    }
    return new Wrapper(factory(isFloat).createMultiPolygon(polygons));
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) GeometryWrapper(org.apache.sis.internal.feature.GeometryWrapper) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) LinearRing(org.locationtech.jts.geom.LinearRing) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint)

Example 9 with GeometryWrapper

use of org.apache.sis.internal.feature.GeometryWrapper in project sis by apache.

the class Wrapper method operationSameCRS.

/**
 * Applies a SQLMM operation on this geometry.
 *
 * @param  operation  the SQLMM operation to apply.
 * @param  other      the other geometry, or {@code null} if the operation requires only one geometry.
 * @param  argument   an operation-specific argument, or {@code null} if not applicable.
 * @return result of the specified operation.
 * @throws ClassCastException if the operation can only be executed on some specific argument types
 *         (for example geometries that are polylines) and one of the argument is not of that type.
 */
@Override
protected Object operationSameCRS(final SQLMM operation, final GeometryWrapper<Geometry> other, final Object argument) {
    /*
         * For all operation producing a geometry, the result is collected for post-processing.
         * For all other kinds of value, the result is returned directly in the switch statement.
         */
    final Geometry result;
    switch(operation) {
        case ST_IsMeasured:
            return Boolean.FALSE;
        case ST_Dimension:
            return geometry.getDimension();
        case ST_SRID:
            return geometry.getSRID();
        case ST_IsEmpty:
            return geometry.isEmpty();
        case ST_IsSimple:
            return geometry.isSimple();
        case ST_IsValid:
            return geometry.isValid();
        case ST_Envelope:
            return getEnvelope();
        case ST_Boundary:
            result = geometry.getBoundary();
            break;
        case ST_ConvexHull:
            result = geometry.convexHull();
            break;
        case ST_Buffer:
            result = geometry.buffer(((Number) argument).doubleValue());
            break;
        case ST_Intersection:
            result = geometry.intersection(((Wrapper) other).geometry);
            break;
        case ST_Union:
            result = geometry.union(((Wrapper) other).geometry);
            break;
        case ST_Difference:
            result = geometry.difference(((Wrapper) other).geometry);
            break;
        case ST_SymDifference:
            result = geometry.symDifference(((Wrapper) other).geometry);
            break;
        case ST_Distance:
            return geometry.distance(((Wrapper) other).geometry);
        case ST_Equals:
            return geometry.equalsTopo(((Wrapper) other).geometry);
        case ST_Relate:
            return geometry.relate(((Wrapper) other).geometry, argument.toString());
        case ST_Disjoint:
            return geometry.disjoint(((Wrapper) other).geometry);
        case ST_Intersects:
            return geometry.intersects(((Wrapper) other).geometry);
        case ST_Touches:
            return geometry.touches(((Wrapper) other).geometry);
        case ST_Crosses:
            return geometry.crosses(((Wrapper) other).geometry);
        case ST_Within:
            return geometry.within(((Wrapper) other).geometry);
        case ST_Contains:
            return geometry.contains(((Wrapper) other).geometry);
        case ST_Overlaps:
            return geometry.overlaps(((Wrapper) other).geometry);
        // WKTWriter() constructor is cheap.
        case ST_AsText:
            return new WKTWriter().write(geometry);
        case ST_AsBinary:
            return FilteringContext.writeWKB(geometry);
        case ST_X:
            return ((Point) geometry).getX();
        case ST_Y:
            return ((Point) geometry).getY();
        case ST_Z:
            return ((Point) geometry).getCoordinate().getZ();
        // JTS does not have curves.
        case ST_ToLineString:
            return geometry;
        case ST_NumGeometries:
            return geometry.getNumGeometries();
        case ST_NumPoints:
            return geometry.getNumPoints();
        case ST_PointN:
            result = ((LineString) geometry).getPointN(toIndex(argument));
            break;
        case ST_StartPoint:
            result = ((LineString) geometry).getStartPoint();
            break;
        case ST_EndPoint:
            result = ((LineString) geometry).getEndPoint();
            break;
        case ST_IsClosed:
            return ((LineString) geometry).isClosed();
        case ST_IsRing:
            return ((LineString) geometry).isRing();
        // Fallthrough: length is the perimeter for polygons.
        case ST_Perimeter:
        case ST_Length:
            return geometry.getLength();
        case ST_Area:
            return geometry.getArea();
        case ST_Centroid:
            result = geometry.getCentroid();
            break;
        case ST_PointOnSurface:
            result = geometry.getInteriorPoint();
            break;
        case ST_ExteriorRing:
            result = ((Polygon) geometry).getExteriorRing();
            break;
        case ST_InteriorRingN:
            result = ((Polygon) geometry).getInteriorRingN(toIndex(argument));
            break;
        case ST_NumInteriorRings:
            return ((Polygon) geometry).getNumInteriorRing();
        case ST_GeometryN:
            result = geometry.getGeometryN(toIndex(argument));
            break;
        case ST_ToPoint:
        case ST_ToPolygon:
        case ST_ToMultiPoint:
        case ST_ToMultiLine:
        case ST_ToMultiPolygon:
        case ST_ToGeomColl:
            {
                final GeometryType target = operation.getGeometryType().get();
                final Class<?> type = factory().getGeometryClass(target);
                if (type.isInstance(geometry)) {
                    return geometry;
                }
                result = convert(target);
                break;
            }
        case ST_Is3D:
            {
                final Coordinate c = geometry.getCoordinate();
                return (c != null) ? !Double.isNaN(c.z) : null;
            }
        case ST_CoordDim:
            {
                final Coordinate c = geometry.getCoordinate();
                return (c != null) ? Double.isNaN(c.z) ? 2 : 3 : null;
            }
        case ST_GeometryType:
            {
                for (int i = 0; i < TYPES.length; i++) {
                    if (TYPES[i].isInstance(geometry)) {
                        return SQLMM_NAMES[i];
                    }
                }
                return null;
            }
        case ST_ExplicitPoint:
            {
                final Coordinate c = ((Point) geometry).getCoordinate();
                if (c == null)
                    return ArraysExt.EMPTY_DOUBLE;
                final double x = c.getX();
                final double y = c.getY();
                final double z = c.getZ();
                return Double.isNaN(z) ? new double[] { x, y } : new double[] { x, y, z };
            }
        case ST_Simplify:
            {
                final double distance = ((Number) argument).doubleValue();
                result = DouglasPeuckerSimplifier.simplify(geometry, distance);
                break;
            }
        case ST_SimplifyPreserveTopology:
            {
                final double distance = ((Number) argument).doubleValue();
                result = TopologyPreservingSimplifier.simplify(geometry, distance);
                break;
            }
        default:
            return super.operationSameCRS(operation, other, argument);
    }
    JTS.copyMetadata(geometry, result);
    return result;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryWrapper(org.apache.sis.internal.feature.GeometryWrapper) WKTWriter(org.locationtech.jts.io.WKTWriter) GeometryType(org.apache.sis.internal.feature.GeometryType) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) Coordinate(org.locationtech.jts.geom.Coordinate) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon)

Aggregations

GeometryWrapper (org.apache.sis.internal.feature.GeometryWrapper)9 MultiPoint (org.locationtech.jts.geom.MultiPoint)3 Point (org.locationtech.jts.geom.Point)3 Shape (java.awt.Shape)2 Path2D (java.awt.geom.Path2D)2 Collection (java.util.Collection)2 AbstractShape (org.apache.sis.internal.referencing.j2d.AbstractShape)2 Coordinate (org.locationtech.jts.geom.Coordinate)2 Geometry (org.locationtech.jts.geom.Geometry)2 LineString (org.locationtech.jts.geom.LineString)2 MultiLineString (org.locationtech.jts.geom.MultiLineString)2 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)2 Polygon (org.locationtech.jts.geom.Polygon)2 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)2 Geometry (com.esri.core.geometry.Geometry)1 Line (com.esri.core.geometry.Line)1 MultiPath (com.esri.core.geometry.MultiPath)1 MultiPoint (com.esri.core.geometry.MultiPoint)1 Point (com.esri.core.geometry.Point)1 Polygon (com.esri.core.geometry.Polygon)1