Search in sources :

Example 1 with PackedCoordinateSequenceFactory

use of org.locationtech.jts.geom.impl.PackedCoordinateSequenceFactory 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)

Aggregations

Point (com.esri.core.geometry.Point)1 GeometryUtils.createJtsEmptyPoint (com.facebook.presto.geospatial.GeometryUtils.createJtsEmptyPoint)1 GeometryUtils.createJtsMultiPoint (com.facebook.presto.geospatial.GeometryUtils.createJtsMultiPoint)1 GeometryUtils.createJtsPoint (com.facebook.presto.geospatial.GeometryUtils.createJtsPoint)1 GeometrySerializationType (com.facebook.presto.geospatial.serde.GeometrySerializationType)1 PrestoException (com.facebook.presto.spi.PrestoException)1 BasicSliceInput (io.airlift.slice.BasicSliceInput)1 PackedCoordinateSequenceFactory (org.locationtech.jts.geom.impl.PackedCoordinateSequenceFactory)1