Search in sources :

Example 1 with GeometryType

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

the class Database method forGeometry.

/**
 * Returns a function for getting values from a geometry column.
 * This is a helper method for {@link #getMapping(Column)} implementations.
 *
 * @param  columnDefinition  information about the column to extract values from and expose through Java API.
 * @return converter to the corresponding java type, or {@code null} if this class can not find a mapping,
 */
protected final ValueGetter<?> forGeometry(final Column columnDefinition) {
    final GeometryType type = columnDefinition.getGeometryType();
    final Class<? extends G> geometryClass = geomLibrary.getGeometryClass(type).asSubclass(geomLibrary.rootClass);
    return new GeometryGetter<>(geomLibrary, geometryClass, columnDefinition.getDefaultCRS(), getBinaryEncoding(columnDefinition));
}
Also used : GeometryType(org.apache.sis.internal.feature.GeometryType)

Example 2 with GeometryType

use of org.apache.sis.internal.feature.GeometryType 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)

Example 3 with GeometryType

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

the class InfoStatements method configureSpatialColumns.

/**
 * Implementation of {@link #completeIntrospection(TableReference, Map)} for geometries,
 * as a separated methods for allowing sub-classes to override above-cited method.
 * May also be used for non-geometric columns such as rasters, in which case the
 * {@code typeValueKind} argument shall be {@code null}.
 *
 * @param  columnQuery    a statement prepared by {@link #prepareIntrospectionStatement(String, char, String, String)}.
 * @param  source         the table for which to get all geometry columns.
 * @param  columns        all columns for the specified table. Keys are column names.
 * @param  typeValueKind  {@code NUMERIC}, {@code TEXTUAL} or {@code null} if none.
 * @throws DataStoreContentException if a logical error occurred in processing data.
 * @throws ParseException if the WKT can not be parsed.
 * @throws SQLException if a SQL error occurred.
 *
 * @todo Follow column dependencies for columns from a view.
 *       Problem: for views, PostGIS will not provide neither SRID nor geometry type,
 *       unless user has statically defined its column to match a specific geometry type/SRID.
 *       Source: https://gis.stackexchange.com/a/376947/182809
 */
protected final void configureSpatialColumns(final PreparedStatement columnQuery, final TableReference source, final Map<String, Column> columns, final GeometryTypeEncoding typeValueKind) throws Exception {
    int p = 0;
    if (database.supportsCatalogs)
        columnQuery.setString(++p, source.catalog);
    if (database.supportsSchemas)
        columnQuery.setString(++p, source.schema);
    columnQuery.setString(++p, source.table);
    try (ResultSet result = columnQuery.executeQuery()) {
        while (result.next()) {
            final Column target = columns.get(result.getString(1));
            if (target != null) {
                final CoordinateReferenceSystem crs = fetchCRS(result.getInt(2));
                GeometryType type = null;
                if (typeValueKind != null) {
                    type = typeValueKind.parse(result, 3);
                    if (type == null) {
                        type = GeometryType.GEOMETRY;
                    }
                }
                target.makeSpatial(this, type, crs);
            }
        }
    }
}
Also used : GeometryType(org.apache.sis.internal.feature.GeometryType) ResultSet(java.sql.ResultSet) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Aggregations

GeometryType (org.apache.sis.internal.feature.GeometryType)3 ResultSet (java.sql.ResultSet)1 GeometryWrapper (org.apache.sis.internal.feature.GeometryWrapper)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 Geometry (org.locationtech.jts.geom.Geometry)1 LineString (org.locationtech.jts.geom.LineString)1 MultiLineString (org.locationtech.jts.geom.MultiLineString)1 MultiPoint (org.locationtech.jts.geom.MultiPoint)1 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)1 Point (org.locationtech.jts.geom.Point)1 Polygon (org.locationtech.jts.geom.Polygon)1 WKTWriter (org.locationtech.jts.io.WKTWriter)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1