Search in sources :

Example 21 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

the class GeoFunctions method stEnvelopeAsPts.

@SqlNullable
@Description("Returns the lower left and upper right corners of bounding rectangular polygon of a Geometry")
@ScalarFunction("ST_EnvelopeAsPts")
@SqlType("array(" + GEOMETRY_TYPE_NAME + ")")
public static Block stEnvelopeAsPts(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    Envelope envelope = deserializeEnvelope(input);
    if (envelope == null) {
        return null;
    }
    BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, 2);
    Point lowerLeftCorner = new Point(envelope.getXMin(), envelope.getYMin());
    Point upperRightCorner = new Point(envelope.getXMax(), envelope.getYMax());
    GEOMETRY.writeSlice(blockBuilder, serialize(createFromEsriGeometry(lowerLeftCorner, null, false)));
    GEOMETRY.writeSlice(blockBuilder, serialize(createFromEsriGeometry(upperRightCorner, null, false)));
    return blockBuilder.build();
}
Also used : MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) GeometrySerde.deserializeEnvelope(io.prestosql.geospatial.serde.GeometrySerde.deserializeEnvelope) Envelope(com.esri.core.geometry.Envelope) 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 22 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

the class GeoFunctions method stRelate.

@SqlNullable
@Description("Returns TRUE if this Geometry is spatially related to another Geometry")
@ScalarFunction("ST_Relate")
@SqlType(BOOLEAN)
public static Boolean stRelate(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right, @SqlType(VARCHAR) Slice relation) {
    OGCGeometry leftGeometry = deserialize(left);
    OGCGeometry rightGeometry = deserialize(right);
    verifySameSpatialReference(leftGeometry, rightGeometry);
    return leftGeometry.relate(rightGeometry, relation.toStringUtf8());
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) 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 23 with Description

use of io.prestosql.spi.function.Description 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 24 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

the class GeoFunctions method stSphericalDistance.

@SqlNullable
@Description("Returns the great-circle distance in meters between two SphericalGeography points.")
@ScalarFunction("ST_Distance")
@SqlType(DOUBLE)
public static Double stSphericalDistance(@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) Slice left, @SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) Slice right) {
    OGCGeometry leftGeometry = deserialize(left);
    OGCGeometry rightGeometry = deserialize(right);
    if (leftGeometry.isEmpty() || rightGeometry.isEmpty()) {
        return null;
    }
    // TODO: support more SphericalGeography types.
    validateSphericalType("ST_Distance", leftGeometry, EnumSet.of(POINT));
    validateSphericalType("ST_Distance", rightGeometry, EnumSet.of(POINT));
    Point leftPoint = (Point) leftGeometry.getEsriGeometry();
    Point rightPoint = (Point) rightGeometry.getEsriGeometry();
    // greatCircleDistance returns distance in KM.
    return greatCircleDistance(leftPoint.getY(), leftPoint.getX(), rightPoint.getY(), rightPoint.getX()) * 1000;
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) 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 25 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

the class GeoFunctions method stSphericalArea.

@SqlNullable
@Description("Returns the area of a geometry on the Earth's surface using spherical model")
@ScalarFunction("ST_Area")
@SqlType(DOUBLE)
public static Double stSphericalArea(@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }
    validateSphericalType("ST_Area", geometry, EnumSet.of(POLYGON, MULTI_POLYGON));
    Polygon polygon = (Polygon) geometry.getEsriGeometry();
    // See https://www.movable-type.co.uk/scripts/latlong.html
    // and http://osgeo-org.1560.x6.nabble.com/Area-of-a-spherical-polygon-td3841625.html
    // and https://www.element84.com/blog/determining-if-a-spherical-polygon-contains-a-pole
    // for the underlying Maths
    double sphericalExcess = 0.0;
    int numPaths = polygon.getPathCount();
    for (int i = 0; i < numPaths; i++) {
        double sign = polygon.isExteriorRing(i) ? 1.0 : -1.0;
        sphericalExcess += sign * Math.abs(computeSphericalExcess(polygon, polygon.getPathStart(i), polygon.getPathEnd(i)));
    }
    // isExteriorRing returns false for the exterior ring
    return Math.abs(sphericalExcess * EARTH_RADIUS_M * EARTH_RADIUS_M);
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) 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) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

Description (io.prestosql.spi.function.Description)76 ScalarFunction (io.prestosql.spi.function.ScalarFunction)76 SqlType (io.prestosql.spi.function.SqlType)76 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)40 SqlNullable (io.prestosql.spi.function.SqlNullable)34 Slice (io.airlift.slice.Slice)18 LiteralParameters (io.prestosql.spi.function.LiteralParameters)15 Point (com.esri.core.geometry.Point)14 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)14 Constraint (io.prestosql.type.Constraint)13 MultiPoint (com.esri.core.geometry.MultiPoint)12 BlockBuilder (io.prestosql.spi.block.BlockBuilder)11 PrestoException (io.prestosql.spi.PrestoException)10 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)7 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 GeometryType (io.prestosql.geospatial.GeometryType)6 Envelope (com.esri.core.geometry.Envelope)5 MultiPath (com.esri.core.geometry.MultiPath)5 Matcher (io.airlift.joni.Matcher)5 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)5