Search in sources :

Example 76 with ScalarFunction

use of io.trino.spi.function.ScalarFunction in project trino by trinodb.

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) {
    OGCGeometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }
    GeometryType type = GeometryType.getForEsriGeometryType(geometry.geometryType());
    if (!type.isMultitype()) {
        if (index == 1) {
            return input;
        }
        return null;
    }
    OGCGeometryCollection geometryCollection = ((OGCGeometryCollection) geometry);
    if (index < 1 || index > geometryCollection.numGeometries()) {
        return null;
    }
    OGCGeometry ogcGeometry = geometryCollection.geometryN((int) index - 1);
    return serialize(ogcGeometry);
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GeometryType(io.trino.geospatial.GeometryType) OGCGeometryCollection(com.esri.core.geometry.ogc.OGCGeometryCollection) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 77 with ScalarFunction

use of io.trino.spi.function.ScalarFunction in project trino by trinodb.

the class GeoFunctions method stTouches.

@SqlNullable
@Description("Returns TRUE if the geometries have at least one point in common, but their interiors do not intersect")
@ScalarFunction("ST_Touches")
@SqlType(BOOLEAN)
public static Boolean stTouches(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right) {
    if (!envelopes(left, right, Envelope::intersect)) {
        return false;
    }
    OGCGeometry leftGeometry = deserialize(left);
    OGCGeometry rightGeometry = deserialize(right);
    verifySameSpatialReference(leftGeometry, rightGeometry);
    return leftGeometry.touches(rightGeometry);
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 78 with ScalarFunction

use of io.trino.spi.function.ScalarFunction in project trino by trinodb.

the class GeoFunctions method stLineString.

@Description("Returns a LineString from an array of points")
@ScalarFunction("ST_LineString")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stLineString(@SqlType("array(" + GEOMETRY_TYPE_NAME + ")") Block input) {
    MultiPath multipath = new Polyline();
    OGCPoint previousPoint = null;
    for (int i = 0; i < input.getPositionCount(); i++) {
        Slice slice = GEOMETRY.getSlice(input, i);
        if (slice.getInput().available() == 0) {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to ST_LineString: null point at index %s", i + 1));
        }
        OGCGeometry geometry = deserialize(slice);
        if (!(geometry instanceof OGCPoint)) {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("ST_LineString takes only an array of valid points, %s was passed", geometry.geometryType()));
        }
        OGCPoint point = (OGCPoint) geometry;
        if (point.isEmpty()) {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to ST_LineString: empty point at index %s", i + 1));
        }
        if (previousPoint == null) {
            multipath.startPath(point.X(), point.Y());
        } else {
            if (point.Equals(previousPoint)) {
                throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to ST_LineString: consecutive duplicate points at index %s", i + 1));
            }
            multipath.lineTo(point.X(), point.Y());
        }
        previousPoint = point;
    }
    OGCLineString linestring = new OGCLineString(multipath, 0, null);
    return serialize(linestring);
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) MultiPath(com.esri.core.geometry.MultiPath) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) Polyline(com.esri.core.geometry.Polyline) TrinoException(io.trino.spi.TrinoException) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) OGCLineString(com.esri.core.geometry.ogc.OGCLineString) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 79 with ScalarFunction

use of io.trino.spi.function.ScalarFunction in project trino by trinodb.

the class GeoFunctions method stPointN.

@SqlNullable
@Description("Returns the vertex of a linestring at the specified index (indices started with 1) ")
@ScalarFunction("ST_PointN")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stPointN(@SqlType(GEOMETRY_TYPE_NAME) Slice input, @SqlType(INTEGER) long index) {
    OGCGeometry geometry = deserialize(input);
    validateType("ST_PointN", geometry, EnumSet.of(LINE_STRING));
    OGCLineString linestring = (OGCLineString) geometry;
    if (index < 1 || index > linestring.numPoints()) {
        return null;
    }
    return serialize(linestring.pointN(toIntExact(index) - 1));
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCLineString(com.esri.core.geometry.ogc.OGCLineString) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 80 with ScalarFunction

use of io.trino.spi.function.ScalarFunction in project trino by trinodb.

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 TrinoException(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 TrinoException(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) GeometryUtils.jsonFromJtsGeometry(io.trino.geospatial.GeometryUtils.jsonFromJtsGeometry) Geometry(org.locationtech.jts.geom.Geometry) MultiVertexGeometry(com.esri.core.geometry.MultiVertexGeometry) GeometryType(io.trino.geospatial.GeometryType) LengthIndexedLine(org.locationtech.jts.linearref.LengthIndexedLine) TrinoException(io.trino.spi.TrinoException) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Aggregations

ScalarFunction (io.trino.spi.function.ScalarFunction)109 SqlType (io.trino.spi.function.SqlType)108 Description (io.trino.spi.function.Description)91 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)46 SqlNullable (io.trino.spi.function.SqlNullable)44 LiteralParameters (io.trino.spi.function.LiteralParameters)25 BlockBuilder (io.trino.spi.block.BlockBuilder)22 Constraint (io.trino.type.Constraint)21 Slice (io.airlift.slice.Slice)18 Point (com.esri.core.geometry.Point)17 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)17 TrinoException (io.trino.spi.TrinoException)17 MultiPoint (com.esri.core.geometry.MultiPoint)15 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)10 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)9 Matcher (io.airlift.joni.Matcher)8 GeometryType (io.trino.geospatial.GeometryType)7 JsonParser (com.fasterxml.jackson.core.JsonParser)6 JsonToken (com.fasterxml.jackson.core.JsonToken)6 Envelope (com.esri.core.geometry.Envelope)5