Search in sources :

Example 1 with Envelope

use of com.esri.core.geometry.Envelope in project presto by prestodb.

the class GeoFunctions method envelopes.

private static boolean envelopes(Slice left, Slice right, EnvelopesPredicate predicate) {
    Envelope leftEnvelope = deserializeEnvelope(left);
    Envelope rightEnvelope = deserializeEnvelope(right);
    if (leftEnvelope.isEmpty() || rightEnvelope.isEmpty()) {
        return false;
    }
    return predicate.apply(leftEnvelope, rightEnvelope);
}
Also used : EsriGeometrySerde.deserializeEnvelope(com.facebook.presto.geospatial.serde.EsriGeometrySerde.deserializeEnvelope) Envelope(com.esri.core.geometry.Envelope)

Example 2 with Envelope

use of com.esri.core.geometry.Envelope in project presto by prestodb.

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.isEmpty()) {
        return null;
    }
    BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, 2);
    org.locationtech.jts.geom.Point lowerLeftCorner = createJtsPoint(envelope.getXMin(), envelope.getYMin());
    org.locationtech.jts.geom.Point upperRightCorner = createJtsPoint(envelope.getXMax(), envelope.getYMax());
    GEOMETRY.writeSlice(blockBuilder, serialize(lowerLeftCorner));
    GEOMETRY.writeSlice(blockBuilder, serialize(upperRightCorner));
    return blockBuilder.build();
}
Also used : EsriGeometrySerde.deserializeEnvelope(com.facebook.presto.geospatial.serde.EsriGeometrySerde.deserializeEnvelope) Envelope(com.esri.core.geometry.Envelope) 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 3 with Envelope

use of com.esri.core.geometry.Envelope in project presto by prestodb.

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) {
    GeometrySerializationType leftType = deserializeType(left);
    GeometrySerializationType rightType = deserializeType(right);
    if (leftType == GeometrySerializationType.ENVELOPE && rightType == 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() || intersection.getYMin() == intersection.getYMax()) {
            if (intersection.getXMin() == intersection.getXMax() && intersection.getYMin() == intersection.getYMax()) {
                return EsriGeometrySerde.serialize(createFromEsriGeometry(new Point(intersection.getXMin(), intersection.getYMin()), null));
            }
            return EsriGeometrySerde.serialize(createFromEsriGeometry(new Polyline(new Point(intersection.getXMin(), intersection.getYMin()), new Point(intersection.getXMax(), intersection.getYMax())), null));
        }
        return EsriGeometrySerde.serialize(intersection);
    }
    // If one side is an envelope, then if it contains the other's envelope we can just return the other geometry.
    if (leftType == GeometrySerializationType.ENVELOPE && deserializeEnvelope(left).contains(deserializeEnvelope(right))) {
        return right;
    }
    if (rightType == GeometrySerializationType.ENVELOPE && deserializeEnvelope(right).contains(deserializeEnvelope(left))) {
        return left;
    }
    OGCGeometry leftGeometry = EsriGeometrySerde.deserialize(left);
    OGCGeometry rightGeometry = EsriGeometrySerde.deserialize(right);
    verifySameSpatialReference(leftGeometry, rightGeometry);
    return EsriGeometrySerde.serialize(leftGeometry.intersection(rightGeometry));
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GeometrySerializationType(com.facebook.presto.geospatial.serde.GeometrySerializationType) Polyline(com.esri.core.geometry.Polyline) 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) EsriGeometrySerde.deserializeEnvelope(com.facebook.presto.geospatial.serde.EsriGeometrySerde.deserializeEnvelope) Envelope(com.esri.core.geometry.Envelope) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 4 with Envelope

use of com.esri.core.geometry.Envelope in project presto by prestodb.

the class SphericalGeoFunctions method spatialPartitions.

@ScalarFunction
@SqlNullable
@Description("Returns an array of spatial partition IDs for a geometry representing a set of points within specified distance from the input geometry")
@SqlType("array(int)")
public static Block spatialPartitions(@SqlType(KdbTreeType.NAME) Object kdbTree, @SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) Slice geometry, @SqlType(DOUBLE) double distance) {
    if (isNaN(distance)) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "distance is NaN");
    }
    if (isInfinite(distance)) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "distance is infinite");
    }
    if (distance < 0) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "distance is negative");
    }
    Envelope envelope = deserializeEnvelope(geometry);
    if (envelope.isEmpty()) {
        return null;
    }
    Rectangle expandedEnvelope2D = new Rectangle(envelope.getXMin() - distance, envelope.getYMin() - distance, envelope.getXMax() + distance, envelope.getYMax() + distance);
    return GeoFunctions.spatialPartitions((KdbTree) kdbTree, expandedEnvelope2D);
}
Also used : Rectangle(com.facebook.presto.geospatial.Rectangle) PrestoException(com.facebook.presto.spi.PrestoException) EsriGeometrySerde.deserializeEnvelope(com.facebook.presto.geospatial.serde.EsriGeometrySerde.deserializeEnvelope) Envelope(com.esri.core.geometry.Envelope) 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 5 with Envelope

use of com.esri.core.geometry.Envelope in project presto by prestodb.

the class SphericalGeoFunctions method toSphericalGeography.

@Description("Converts a Geometry object to a SphericalGeography object")
@ScalarFunction("to_spherical_geography")
@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME)
public static Slice toSphericalGeography(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    // "every point in input is in range" <=> "the envelope of input is in range"
    Envelope envelope = deserializeEnvelope(input);
    if (!envelope.isEmpty()) {
        checkLatitude(envelope.getYMin());
        checkLatitude(envelope.getYMax());
        checkLongitude(envelope.getXMin());
        checkLongitude(envelope.getXMax());
    }
    OGCGeometry geometry = EsriGeometrySerde.deserialize(input);
    if (geometry.is3D()) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Cannot convert 3D geometry to a spherical geography");
    }
    GeometryCursor cursor = geometry.getEsriGeometryCursor();
    while (true) {
        com.esri.core.geometry.Geometry subGeometry = cursor.next();
        if (subGeometry == null) {
            break;
        }
        if (!GEOMETRY_TYPES_FOR_SPHERICAL_GEOGRAPHY.contains(subGeometry.getType())) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Cannot convert geometry of this type to spherical geography: " + subGeometry.getType());
        }
    }
    return input;
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GeometryCursor(com.esri.core.geometry.GeometryCursor) PrestoException(com.facebook.presto.spi.PrestoException) Geometry(com.esri.core.geometry.Geometry) EsriGeometrySerde.deserializeEnvelope(com.facebook.presto.geospatial.serde.EsriGeometrySerde.deserializeEnvelope) Envelope(com.esri.core.geometry.Envelope) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

Envelope (com.esri.core.geometry.Envelope)18 EsriGeometrySerde.deserializeEnvelope (com.facebook.presto.geospatial.serde.EsriGeometrySerde.deserializeEnvelope)12 SqlType (com.facebook.presto.spi.function.SqlType)7 Point (com.esri.core.geometry.Point)6 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)6 Description (com.facebook.presto.spi.function.Description)6 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 Rectangle (com.facebook.presto.geospatial.Rectangle)4 PrestoException (com.facebook.presto.spi.PrestoException)4 Geometry (com.esri.core.geometry.Geometry)3 SqlNullable (com.facebook.presto.spi.function.SqlNullable)3 Test (org.testng.annotations.Test)3 GeometryCursor (com.esri.core.geometry.GeometryCursor)2 MultiPoint (com.esri.core.geometry.MultiPoint)2 OGCMultiPoint (com.esri.core.geometry.ogc.OGCMultiPoint)2 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)2 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)2 GeometryUtils.disjoint (com.facebook.presto.geospatial.GeometryUtils.disjoint)2 GeometrySerializationType (com.facebook.presto.geospatial.serde.GeometrySerializationType)2 BingTileUtils.tileToEnvelope (com.facebook.presto.plugin.geospatial.BingTileUtils.tileToEnvelope)2