Search in sources :

Example 26 with ScalarFunction

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

the class GeoFunctions method stPoints.

@SqlNullable
@Description("Returns an array of points in a geometry")
@ScalarFunction("ST_Points")
@SqlType("array(" + GEOMETRY_TYPE_NAME + ")")
public static Block stPoints(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    Geometry geometry = JtsGeometrySerde.deserialize(input);
    validateType("ST_Points", geometry, VALID_TYPES_FOR_ST_POINTS);
    if (geometry.isEmpty()) {
        return null;
    }
    int pointCount = geometry.getNumPoints();
    BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, pointCount);
    buildPointsBlock(geometry, blockBuilder);
    return blockBuilder.build();
}
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) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) BlockBuilder(io.trino.spi.block.BlockBuilder) 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 27 with ScalarFunction

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

the class GeoFunctions method geometryFromHadoopShape.

@Description("Returns a Geometry type object from Spatial Framework for Hadoop representation")
@ScalarFunction("geometry_from_hadoop_shape")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice geometryFromHadoopShape(@SqlType(VARBINARY) Slice input) {
    requireNonNull(input, "input is null");
    try {
        OGCGeometry geometry = OGCGeometry.fromEsriShape(getShapeByteBuffer(input));
        String wkt = geometryToWkt(geometry.getEsriGeometry(), getWktExportFlags(input));
        return serialize(OGCGeometry.fromText(wkt));
    } catch (IndexOutOfBoundsException | UnsupportedOperationException | IllegalArgumentException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Invalid Hadoop shape", e);
    }
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) TrinoException(io.trino.spi.TrinoException) 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 28 with ScalarFunction

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

the class GeoFunctions method stOverlaps.

@SqlNullable
@Description("Returns TRUE if the Geometries share space, are of the same dimension, but are not completely contained by each other")
@ScalarFunction("ST_Overlaps")
@SqlType(BOOLEAN)
public static Boolean stOverlaps(@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.overlaps(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 29 with ScalarFunction

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

the class GeoFunctions method stIntersection.

@Description("Returns the Geometry value that represents the point set intersection of two Geometries")
@ScalarFunction("ST_Intersection")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stIntersection(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right) {
    if (deserializeType(left) == GeometrySerializationType.ENVELOPE && deserializeType(right) == GeometrySerializationType.ENVELOPE) {
        Envelope leftEnvelope = deserializeEnvelope(left);
        Envelope rightEnvelope = deserializeEnvelope(right);
        // Envelope#intersect updates leftEnvelope to the intersection of the two envelopes
        if (!leftEnvelope.intersect(rightEnvelope)) {
            return EMPTY_POLYGON;
        }
        Envelope intersection = leftEnvelope;
        if (intersection.getXMin() == intersection.getXMax()) {
            if (intersection.getYMin() == intersection.getYMax()) {
                return serialize(createFromEsriGeometry(new Point(intersection.getXMin(), intersection.getXMax()), null));
            }
            return serialize(createFromEsriGeometry(new Polyline(new Point(intersection.getXMin(), intersection.getYMin()), new Point(intersection.getXMin(), intersection.getYMax())), null));
        }
        if (intersection.getYMin() == intersection.getYMax()) {
            return serialize(createFromEsriGeometry(new Polyline(new Point(intersection.getXMin(), intersection.getYMin()), new Point(intersection.getXMax(), intersection.getYMin())), null));
        }
        return serialize(intersection);
    }
    OGCGeometry leftGeometry = deserialize(left);
    OGCGeometry rightGeometry = deserialize(right);
    verifySameSpatialReference(leftGeometry, rightGeometry);
    return serialize(leftGeometry.intersection(rightGeometry));
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) Polyline(com.esri.core.geometry.Polyline) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) GeometrySerde.deserializeEnvelope(io.trino.geospatial.serde.GeometrySerde.deserializeEnvelope) Envelope(com.esri.core.geometry.Envelope) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 30 with ScalarFunction

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

the class GeoFunctions method stNumInteriorRings.

@SqlNullable
@Description("Returns the cardinality of the collection of interior rings of a polygon")
@ScalarFunction("ST_NumInteriorRing")
@SqlType(BIGINT)
public static Long stNumInteriorRings(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = deserialize(input);
    validateType("ST_NumInteriorRing", geometry, EnumSet.of(POLYGON));
    if (geometry.isEmpty()) {
        return null;
    }
    return Long.valueOf(((OGCPolygon) geometry).numInteriorRing());
}
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)

Aggregations

ScalarFunction (io.trino.spi.function.ScalarFunction)108 SqlType (io.trino.spi.function.SqlType)107 Description (io.trino.spi.function.Description)90 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)21 Constraint (io.trino.type.Constraint)21 Point (com.esri.core.geometry.Point)17 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)17 Slice (io.airlift.slice.Slice)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