Search in sources :

Example 1 with GeometryType

use of com.facebook.presto.geospatial.GeometryType in project presto by prestodb.

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) {
    Geometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }
    GeometryType type = GeometryType.getForJtsGeometryType(geometry.getGeometryType());
    if (!type.isMultitype()) {
        BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, 1);
        GEOMETRY.writeSlice(blockBuilder, serialize(geometry));
        return blockBuilder.build();
    }
    GeometryCollection collection = (GeometryCollection) geometry;
    BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, collection.getNumGeometries());
    for (int i = 0; i < collection.getNumGeometries(); i++) {
        GEOMETRY.writeSlice(blockBuilder, serialize(collection.getGeometryN(i)));
    }
    return blockBuilder.build();
}
Also used : GeometryUtils.wktFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry) OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCGeometry.createFromEsriGeometry(com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry) Geometry(org.locationtech.jts.geom.Geometry) GeometryUtils.jsonFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) OGCConcreteGeometryCollection(com.esri.core.geometry.ogc.OGCConcreteGeometryCollection) GeometryType(com.facebook.presto.geospatial.GeometryType) GeometryUtils.createJtsMultiPoint(com.facebook.presto.geospatial.GeometryUtils.createJtsMultiPoint) Point(com.esri.core.geometry.Point) GeometryUtils.createJtsEmptyPoint(com.facebook.presto.geospatial.GeometryUtils.createJtsEmptyPoint) GeometryUtils.createJtsPoint(com.facebook.presto.geospatial.GeometryUtils.createJtsPoint) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 2 with GeometryType

use of com.facebook.presto.geospatial.GeometryType in project presto by prestodb.

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 = deserialize(lineSlice);
    Geometry point = 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 : GeometryUtils.wktFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry) OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCGeometry.createFromEsriGeometry(com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry) Geometry(org.locationtech.jts.geom.Geometry) GeometryUtils.jsonFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry) GeometryType(com.facebook.presto.geospatial.GeometryType) LengthIndexedLine(org.locationtech.jts.linearref.LengthIndexedLine) PrestoException(com.facebook.presto.spi.PrestoException) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 3 with GeometryType

use of com.facebook.presto.geospatial.GeometryType in project presto by prestodb.

the class GeoFunctions method stGeometryN.

@SqlNullable
@Description("Returns the geometry element at the specified index (indices started with 1)")
@ScalarFunction("ST_GeometryN")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stGeometryN(@SqlType(GEOMETRY_TYPE_NAME) Slice input, @SqlType(INTEGER) long index) {
    Geometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }
    GeometryType type = GeometryType.getForJtsGeometryType(geometry.getGeometryType());
    if (!type.isMultitype()) {
        if (index == 1) {
            return input;
        }
        return null;
    }
    GeometryCollection geometryCollection = ((GeometryCollection) geometry);
    if (index < 1 || index > geometryCollection.getNumGeometries()) {
        return null;
    }
    return serialize(geometryCollection.getGeometryN((int) index - 1));
}
Also used : GeometryUtils.wktFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry) OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCGeometry.createFromEsriGeometry(com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry) Geometry(org.locationtech.jts.geom.Geometry) GeometryUtils.jsonFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) OGCConcreteGeometryCollection(com.esri.core.geometry.ogc.OGCConcreteGeometryCollection) GeometryType(com.facebook.presto.geospatial.GeometryType) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 4 with GeometryType

use of com.facebook.presto.geospatial.GeometryType in project presto by prestodb.

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) {
    Geometry geometry = deserialize(input);
    validateType("ST_Centroid", geometry, EnumSet.of(POINT, MULTI_POINT, LINE_STRING, MULTI_LINE_STRING, POLYGON, MULTI_POLYGON));
    GeometryType geometryType = GeometryType.getForJtsGeometryType(geometry.getGeometryType());
    if (geometryType == GeometryType.POINT) {
        return input;
    }
    if (geometry.getNumPoints() == 0) {
        return serialize(createJtsEmptyPoint());
    }
    return serialize(geometry.getCentroid());
}
Also used : GeometryUtils.wktFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry) OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCGeometry.createFromEsriGeometry(com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry) Geometry(org.locationtech.jts.geom.Geometry) GeometryUtils.jsonFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry) GeometryType(com.facebook.presto.geospatial.GeometryType) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 5 with GeometryType

use of com.facebook.presto.geospatial.GeometryType in project presto by prestodb.

the class EsriGeometrySerde method writeGeometry.

private static void writeGeometry(DynamicSliceOutput output, OGCGeometry geometry) {
    GeometryType type = GeometryType.getForEsriGeometryType(geometry.geometryType());
    switch(type) {
        case POINT:
            writePoint(output, geometry);
            break;
        case MULTI_POINT:
            writeSimpleGeometry(output, GeometrySerializationType.MULTI_POINT, geometry);
            break;
        case LINE_STRING:
            writeSimpleGeometry(output, GeometrySerializationType.LINE_STRING, geometry);
            break;
        case MULTI_LINE_STRING:
            writeSimpleGeometry(output, GeometrySerializationType.MULTI_LINE_STRING, geometry);
            break;
        case POLYGON:
            writeSimpleGeometry(output, GeometrySerializationType.POLYGON, geometry);
            break;
        case MULTI_POLYGON:
            writeSimpleGeometry(output, GeometrySerializationType.MULTI_POLYGON, geometry);
            break;
        case GEOMETRY_COLLECTION:
            verify(geometry instanceof OGCConcreteGeometryCollection);
            writeGeometryCollection(output, (OGCConcreteGeometryCollection) geometry);
            break;
        default:
            throw new IllegalArgumentException("Unsupported geometry type: " + type);
    }
}
Also used : GeometryType(com.facebook.presto.geospatial.GeometryType) OGCConcreteGeometryCollection(com.esri.core.geometry.ogc.OGCConcreteGeometryCollection)

Aggregations

GeometryType (com.facebook.presto.geospatial.GeometryType)5 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)4 OGCGeometry.createFromEsriGeometry (com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry)4 GeometryUtils.jsonFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry)4 GeometryUtils.wktFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry)4 Description (com.facebook.presto.spi.function.Description)4 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)4 SqlType (com.facebook.presto.spi.function.SqlType)4 Geometry (org.locationtech.jts.geom.Geometry)4 OGCConcreteGeometryCollection (com.esri.core.geometry.ogc.OGCConcreteGeometryCollection)3 SqlNullable (com.facebook.presto.spi.function.SqlNullable)3 GeometryCollection (org.locationtech.jts.geom.GeometryCollection)2 Point (com.esri.core.geometry.Point)1 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 GeometryUtils.createJtsEmptyPoint (com.facebook.presto.geospatial.GeometryUtils.createJtsEmptyPoint)1 GeometryUtils.createJtsMultiPoint (com.facebook.presto.geospatial.GeometryUtils.createJtsMultiPoint)1 GeometryUtils.createJtsPoint (com.facebook.presto.geospatial.GeometryUtils.createJtsPoint)1 PrestoException (com.facebook.presto.spi.PrestoException)1 LengthIndexedLine (org.locationtech.jts.linearref.LengthIndexedLine)1