Search in sources :

Example 1 with GeometryType

use of io.prestosql.geospatial.GeometryType in project hetu-core by openlookeng.

the class GeoFunctions method stCentroid.

@Description("Returns the Point value that is the mathematical centroid of a Geometry")
@ScalarFunction("ST_Centroid")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stCentroid(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = deserialize(input);
    validateType("ST_Centroid", geometry, EnumSet.of(POINT, MULTI_POINT, LINE_STRING, MULTI_LINE_STRING, POLYGON, MULTI_POLYGON));
    GeometryType geometryType = GeometryType.getForEsriGeometryType(geometry.geometryType());
    if (geometryType == GeometryType.POINT) {
        return input;
    }
    int pointCount = ((MultiVertexGeometry) geometry.getEsriGeometry()).getPointCount();
    if (pointCount == 0) {
        return serialize(createFromEsriGeometry(new Point(), geometry.getEsriSpatialReference()));
    }
    Point centroid;
    switch(geometryType) {
        case MULTI_POINT:
            centroid = computePointsCentroid((MultiVertexGeometry) geometry.getEsriGeometry());
            break;
        case LINE_STRING:
        case MULTI_LINE_STRING:
            centroid = computeLineCentroid((Polyline) geometry.getEsriGeometry());
            break;
        case POLYGON:
            centroid = computePolygonCentroid((Polygon) geometry.getEsriGeometry());
            break;
        case MULTI_POLYGON:
            centroid = computeMultiPolygonCentroid((OGCMultiPolygon) geometry);
            break;
        default:
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Unexpected geometry type: " + geometryType);
    }
    return serialize(createFromEsriGeometry(centroid, geometry.getEsriSpatialReference()));
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GeometryType(io.prestosql.geospatial.GeometryType) MultiVertexGeometry(com.esri.core.geometry.MultiVertexGeometry) OGCMultiPolygon(com.esri.core.geometry.ogc.OGCMultiPolygon) Polyline(com.esri.core.geometry.Polyline) PrestoException(io.prestosql.spi.PrestoException) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) Polygon(com.esri.core.geometry.Polygon) OGCMultiPolygon(com.esri.core.geometry.ogc.OGCMultiPolygon) OGCPolygon(com.esri.core.geometry.ogc.OGCPolygon) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Example 2 with GeometryType

use of io.prestosql.geospatial.GeometryType in project hetu-core by openlookeng.

the class GeoFunctions method lineLocatePoint.

@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();
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCGeometry.createFromEsriGeometry(com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry) Geometry(org.locationtech.jts.geom.Geometry) MultiVertexGeometry(com.esri.core.geometry.MultiVertexGeometry) GeometryType(io.prestosql.geospatial.GeometryType) LengthIndexedLine(org.locationtech.jts.linearref.LengthIndexedLine) PrestoException(io.prestosql.spi.PrestoException) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Example 3 with GeometryType

use of io.prestosql.geospatial.GeometryType in project hetu-core by openlookeng.

the class GeoFunctions method stArea.

@Description("Returns the 2D Euclidean area of a geometry")
@ScalarFunction("ST_Area")
@SqlType(DOUBLE)
public static double stArea(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = deserialize(input);
    // The Esri geometry library does not support area for geometry collections. We compute the area
    // of collections by summing the area of the individual components.
    GeometryType type = GeometryType.getForEsriGeometryType(geometry.geometryType());
    if (type == GeometryType.GEOMETRY_COLLECTION) {
        double area = 0.0;
        GeometryCursor cursor = geometry.getEsriGeometryCursor();
        while (true) {
            com.esri.core.geometry.Geometry esriGeometry = cursor.next();
            if (esriGeometry == null) {
                return area;
            }
            area += esriGeometry.calculateArea2D();
        }
    }
    return geometry.getEsriGeometry().calculateArea2D();
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GeometryType(io.prestosql.geospatial.GeometryType) GeometryCursor(com.esri.core.geometry.GeometryCursor) ListeningGeometryCursor(com.esri.core.geometry.ListeningGeometryCursor) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Example 4 with GeometryType

use of io.prestosql.geospatial.GeometryType in project hetu-core by openlookeng.

the class GeoFunctions method stGeometries.

@SqlNullable
@Description("Returns an array of geometries in the specified collection")
@ScalarFunction("ST_Geometries")
@SqlType("array(" + GEOMETRY_TYPE_NAME + ")")
public static Block stGeometries(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }
    GeometryType type = GeometryType.getForEsriGeometryType(geometry.geometryType());
    if (!type.isMultitype()) {
        BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, 1);
        GEOMETRY.writeSlice(blockBuilder, serialize(geometry));
        return blockBuilder.build();
    }
    OGCGeometryCollection collection = (OGCGeometryCollection) geometry;
    BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, collection.numGeometries());
    for (int i = 0; i < collection.numGeometries(); i++) {
        GEOMETRY.writeSlice(blockBuilder, serialize(collection.geometryN(i)));
    }
    return blockBuilder.build();
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GeometryType(io.prestosql.geospatial.GeometryType) OGCGeometryCollection(com.esri.core.geometry.ogc.OGCGeometryCollection) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) BlockBuilder(io.prestosql.spi.block.BlockBuilder) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Example 5 with GeometryType

use of io.prestosql.geospatial.GeometryType in project hetu-core by openlookeng.

the class GeoFunctions method stNumGeometries.

@Description("Returns the cardinality of the geometry collection")
@ScalarFunction("ST_NumGeometries")
@SqlType(INTEGER)
public static long stNumGeometries(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return 0;
    }
    GeometryType type = GeometryType.getForEsriGeometryType(geometry.geometryType());
    if (!type.isMultitype()) {
        return 1;
    }
    return ((OGCGeometryCollection) geometry).numGeometries();
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GeometryType(io.prestosql.geospatial.GeometryType) OGCGeometryCollection(com.esri.core.geometry.ogc.OGCGeometryCollection) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)6 GeometryType (io.prestosql.geospatial.GeometryType)6 Description (io.prestosql.spi.function.Description)6 ScalarFunction (io.prestosql.spi.function.ScalarFunction)6 SqlType (io.prestosql.spi.function.SqlType)6 OGCGeometryCollection (com.esri.core.geometry.ogc.OGCGeometryCollection)3 SqlNullable (io.prestosql.spi.function.SqlNullable)3 MultiPoint (com.esri.core.geometry.MultiPoint)2 MultiVertexGeometry (com.esri.core.geometry.MultiVertexGeometry)2 Point (com.esri.core.geometry.Point)2 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)2 PrestoException (io.prestosql.spi.PrestoException)2 GeometryCursor (com.esri.core.geometry.GeometryCursor)1 ListeningGeometryCursor (com.esri.core.geometry.ListeningGeometryCursor)1 Polygon (com.esri.core.geometry.Polygon)1 Polyline (com.esri.core.geometry.Polyline)1 OGCGeometry.createFromEsriGeometry (com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry)1 OGCMultiPolygon (com.esri.core.geometry.ogc.OGCMultiPolygon)1 OGCPolygon (com.esri.core.geometry.ogc.OGCPolygon)1 BlockBuilder (io.prestosql.spi.block.BlockBuilder)1