Search in sources :

Example 1 with GeometrySerializationType

use of com.facebook.presto.geospatial.serde.GeometrySerializationType in project presto by prestodb.

the class GeoFunctions method readPointCoordinates.

private static CoordinateSequence readPointCoordinates(Block input, String functionName, boolean forbidDuplicates) {
    PackedCoordinateSequenceFactory coordinateSequenceFactory = new PackedCoordinateSequenceFactory();
    double[] coordinates = new double[2 * input.getPositionCount()];
    double lastX = Double.NaN;
    double lastY = Double.NaN;
    for (int i = 0; i < input.getPositionCount(); i++) {
        if (input.isNull(i)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to %s: null at index %s", functionName, i + 1));
        }
        BasicSliceInput slice = new BasicSliceInput(GEOMETRY.getSlice(input, i));
        GeometrySerializationType type = GeometrySerializationType.getForCode(slice.readByte());
        if (type != GeometrySerializationType.POINT) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to %s: geometry is not a point: %s at index %s", functionName, type.toString(), i + 1));
        }
        double x = slice.readDouble();
        double y = slice.readDouble();
        if (Double.isNaN(x) || Double.isNaN(x)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to %s: empty point at index %s", functionName, i + 1));
        }
        if (forbidDuplicates && x == lastX && y == lastY) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to %s: consecutive duplicate points at index %s", functionName, i + 1));
        }
        lastX = x;
        lastY = y;
        coordinates[2 * i] = x;
        coordinates[2 * i + 1] = y;
    }
    return coordinateSequenceFactory.create(coordinates, 2);
}
Also used : GeometrySerializationType(com.facebook.presto.geospatial.serde.GeometrySerializationType) PrestoException(com.facebook.presto.spi.PrestoException) 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) PackedCoordinateSequenceFactory(org.locationtech.jts.geom.impl.PackedCoordinateSequenceFactory) BasicSliceInput(io.airlift.slice.BasicSliceInput)

Example 2 with GeometrySerializationType

use of com.facebook.presto.geospatial.serde.GeometrySerializationType 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 3 with GeometrySerializationType

use of com.facebook.presto.geospatial.serde.GeometrySerializationType in project presto by prestodb.

the class BingTileFunctions method geometryToBingTiles.

@Description("Given a geometry and a zoom level, returns the minimum set of Bing tiles of that zoom level that fully covers that geometry")
@ScalarFunction("geometry_to_bing_tiles")
@SqlType("array(" + BingTileType.NAME + ")")
public static Block geometryToBingTiles(@SqlType(GEOMETRY_TYPE_NAME) Slice input, @SqlType(StandardTypes.INTEGER) long zoomLevelInput) {
    Envelope envelope = deserializeEnvelope(input);
    if (envelope.isEmpty()) {
        return EMPTY_TILE_ARRAY;
    }
    int zoomLevel = toIntExact(zoomLevelInput);
    GeometrySerializationType type = deserializeType(input);
    List<BingTile> covering;
    if (type == GeometrySerializationType.POINT || type == GeometrySerializationType.ENVELOPE) {
        covering = findMinimalTileCovering(envelope, zoomLevel);
    } else {
        OGCGeometry ogcGeometry = deserialize(input);
        if (isPointOrRectangle(ogcGeometry, envelope)) {
            covering = findMinimalTileCovering(envelope, zoomLevel);
        } else {
            covering = findMinimalTileCovering(ogcGeometry, zoomLevel);
        }
    }
    BlockBuilder blockBuilder = BIGINT.createBlockBuilder(null, covering.size());
    for (BingTile tile : covering) {
        BIGINT.writeLong(blockBuilder, tile.encode());
    }
    return blockBuilder.build();
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GeometrySerializationType(com.facebook.presto.geospatial.serde.GeometrySerializationType) EsriGeometrySerde.deserializeEnvelope(com.facebook.presto.geospatial.serde.EsriGeometrySerde.deserializeEnvelope) BingTileUtils.tileToEnvelope(com.facebook.presto.plugin.geospatial.BingTileUtils.tileToEnvelope) Envelope(com.esri.core.geometry.Envelope) GeometryUtils.disjoint(com.facebook.presto.geospatial.GeometryUtils.disjoint) Point(com.esri.core.geometry.Point) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

Point (com.esri.core.geometry.Point)3 GeometrySerializationType (com.facebook.presto.geospatial.serde.GeometrySerializationType)3 Envelope (com.esri.core.geometry.Envelope)2 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)2 GeometryUtils.createJtsEmptyPoint (com.facebook.presto.geospatial.GeometryUtils.createJtsEmptyPoint)2 GeometryUtils.createJtsMultiPoint (com.facebook.presto.geospatial.GeometryUtils.createJtsMultiPoint)2 GeometryUtils.createJtsPoint (com.facebook.presto.geospatial.GeometryUtils.createJtsPoint)2 EsriGeometrySerde.deserializeEnvelope (com.facebook.presto.geospatial.serde.EsriGeometrySerde.deserializeEnvelope)2 Description (com.facebook.presto.spi.function.Description)2 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)2 SqlType (com.facebook.presto.spi.function.SqlType)2 Polyline (com.esri.core.geometry.Polyline)1 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 GeometryUtils.disjoint (com.facebook.presto.geospatial.GeometryUtils.disjoint)1 BingTileUtils.tileToEnvelope (com.facebook.presto.plugin.geospatial.BingTileUtils.tileToEnvelope)1 PrestoException (com.facebook.presto.spi.PrestoException)1 BasicSliceInput (io.airlift.slice.BasicSliceInput)1 PackedCoordinateSequenceFactory (org.locationtech.jts.geom.impl.PackedCoordinateSequenceFactory)1