Java Code Examples for com.esri.core.geometry.ogc.OGCGeometry#isEmpty()

The following examples show how to use com.esri.core.geometry.ogc.OGCGeometry#isEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GeoFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: GeoFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
@SqlNullable
@Description("Returns the geometry that represents all points whose distance from the specified geometry is less than or equal to the specified distance")
@ScalarFunction("ST_Buffer")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stBuffer(@SqlType(GEOMETRY_TYPE_NAME) Slice input, @SqlType(DOUBLE) double distance)
{
    if (isNaN(distance)) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "distance is NaN");
    }

    if (distance < 0) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "distance is negative");
    }

    if (distance == 0) {
        return input;
    }

    OGCGeometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }
    return serialize(geometry.buffer(distance));
}
 
Example 3
Source File: GeoFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
@SqlNullable
@Description("Returns a Point interpolated along a LineString at the fraction given.")
@ScalarFunction("line_interpolate_point")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice lineInterpolatePoint(
        @SqlType(GEOMETRY_TYPE_NAME) Slice input,
        @SqlType(StandardTypes.DOUBLE) double distanceFraction)
{
    OGCGeometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }

    List<Point> interpolatedPoints = interpolatePoints(geometry, distanceFraction, false);
    return serialize(createFromEsriGeometry(interpolatedPoints.get(0), null));
}
 
Example 4
Source File: GeoFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
@SqlNullable
@Description("Returns an array of interior rings of a polygon")
@ScalarFunction("ST_InteriorRings")
@SqlType("array(" + GEOMETRY_TYPE_NAME + ")")
public static Block stInteriorRings(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_InteriorRings", geometry, EnumSet.of(POLYGON));
    if (geometry.isEmpty()) {
        return null;
    }

    OGCPolygon polygon = (OGCPolygon) geometry;
    BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, polygon.numInteriorRing());
    for (int i = 0; i < polygon.numInteriorRing(); i++) {
        GEOMETRY.writeSlice(blockBuilder, serialize(polygon.interiorRingN(i)));
    }
    return blockBuilder.build();
}
 
Example 5
Source File: GeoFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
@SqlNullable
@Description("Returns the geometry element at the specified index (indices started with 1)")
@ScalarFunction("ST_GeometryN")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stGeometryN(@SqlType(GEOMETRY_TYPE_NAME) Slice input, @SqlType(INTEGER) long index)
{
    OGCGeometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }
    GeometryType type = GeometryType.getForEsriGeometryType(geometry.geometryType());
    if (!type.isMultitype()) {
        if (index == 1) {
            return input;
        }
        return null;
    }
    OGCGeometryCollection geometryCollection = ((OGCGeometryCollection) geometry);
    if (index < 1 || index > geometryCollection.numGeometries()) {
        return null;
    }
    OGCGeometry ogcGeometry = geometryCollection.geometryN((int) index - 1);
    return serialize(ogcGeometry);
}
 
Example 6
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Returns the cardinality of the geometry collection")
@ScalarFunction("ST_NumGeometries")
@SqlType(INTEGER)
public static long stNumGeometries(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return 0;
    }
    GeometryType type = GeometryType.getForEsriGeometryType(geometry.geometryType());
    if (!type.isMultitype()) {
        return 1;
    }
    return ((OGCGeometryCollection) geometry).numGeometries();
}
 
Example 7
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns a line string representing the exterior ring of the POLYGON")
@ScalarFunction("ST_ExteriorRing")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stExteriorRing(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_ExteriorRing", geometry, EnumSet.of(POLYGON));
    if (geometry.isEmpty()) {
        return null;
    }
    return serialize(((OGCPolygon) geometry).exteriorRing());
}
 
Example 8
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns the 2-dimensional cartesian minimum distance (based on spatial ref) between two geometries in projected units")
@ScalarFunction("ST_Distance")
@SqlType(DOUBLE)
public static Double stDistance(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right)
{
    OGCGeometry leftGeometry = deserialize(left);
    OGCGeometry rightGeometry = deserialize(right);
    verifySameSpatialReference(leftGeometry, rightGeometry);
    return leftGeometry.isEmpty() || rightGeometry.isEmpty() ? null : leftGeometry.distance(rightGeometry);
}
 
Example 9
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Returns the closure of the combinatorial boundary of this Geometry")
@ScalarFunction("ST_Boundary")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stBoundary(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    if (geometry.isEmpty() && GeometryType.getForEsriGeometryType(geometry.geometryType()) == LINE_STRING) {
        // OCGGeometry#boundary crashes with NPE for LINESTRING EMPTY
        return EMPTY_MULTIPOINT;
    }
    return serialize(geometry.boundary());
}
 
Example 10
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Return the Y coordinate of the point")
@ScalarFunction("ST_Y")
@SqlType(DOUBLE)
public static Double stY(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_Y", geometry, EnumSet.of(POINT));
    if (geometry.isEmpty()) {
        return null;
    }
    return ((OGCPoint) geometry).Y();
}
 
Example 11
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Return the X coordinate of the point")
@ScalarFunction("ST_X")
@SqlType(DOUBLE)
public static Double stX(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_X", geometry, EnumSet.of(POINT));
    if (geometry.isEmpty()) {
        return null;
    }
    return ((OGCPoint) geometry).X();
}
 
Example 12
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns the last point of a LINESTRING geometry as a Point")
@ScalarFunction("ST_EndPoint")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stEndPoint(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_EndPoint", geometry, EnumSet.of(LINE_STRING));
    if (geometry.isEmpty()) {
        return null;
    }
    MultiPath lines = (MultiPath) geometry.getEsriGeometry();
    SpatialReference reference = geometry.getEsriSpatialReference();
    return serialize(createFromEsriGeometry(lines.getPoint(lines.getPointCount() - 1), reference));
}
 
Example 13
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns the first point of a LINESTRING geometry as a Point")
@ScalarFunction("ST_StartPoint")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stStartPoint(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_StartPoint", geometry, EnumSet.of(LINE_STRING));
    if (geometry.isEmpty()) {
        return null;
    }
    MultiPath lines = (MultiPath) geometry.getEsriGeometry();
    SpatialReference reference = geometry.getEsriSpatialReference();
    return serialize(createFromEsriGeometry(lines.getPoint(0), reference));
}
 
Example 14
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 15
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@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)));
    }

    // Math.abs is required here because for Polygons with a 2D area of 0
    // isExteriorRing returns false for the exterior ring
    return Math.abs(sphericalExcess * EARTH_RADIUS_M * EARTH_RADIUS_M);
}
 
Example 16
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns the great-circle length in meters of a linestring or multi-linestring on Earth's surface")
@ScalarFunction("ST_Length")
@SqlType(DOUBLE)
public static Double stSphericalLength(@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }

    validateSphericalType("ST_Length", geometry, EnumSet.of(LINE_STRING, MULTI_LINE_STRING));
    MultiPath lineString = (MultiPath) geometry.getEsriGeometry();

    double sum = 0;

    // sum up paths on (multi)linestring
    for (int path = 0; path < lineString.getPathCount(); path++) {
        if (lineString.getPathSize(path) < 2) {
            continue;
        }

        // sum up distances between adjacent points on this path
        int pathStart = lineString.getPathStart(path);
        Point previous = lineString.getPoint(pathStart);
        for (int i = pathStart + 1; i < lineString.getPathEnd(path); i++) {
            Point next = lineString.getPoint(i);
            sum += greatCircleDistance(previous.getY(), previous.getX(), next.getY(), next.getX());
            previous = next;
        }
    }

    return sum * 1000;
}
 
Example 17
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Returns TRUE if this Geometry has no anomalous geometric points, such as self intersection or self tangency")
@ScalarFunction("ST_IsSimple")
@SqlType(BOOLEAN)
public static boolean stIsSimple(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    return geometry.isEmpty() || geometry.isSimple();
}
 
Example 18
Source File: GeometryStateFactory.java    From presto with Apache License 2.0 5 votes vote down vote up
private static long getGeometryMemorySize(OGCGeometry geometry)
{
    if (geometry == null) {
        return 0;
    }
    // Due to the following issue:
    // https://github.com/Esri/geometry-api-java/issues/192
    // We must check if the geometry is empty before calculating its size.  Once the issue is resolved
    // and we bring the fix into our codebase, we can remove this check.
    if (geometry.isEmpty()) {
        return OGC_GEOMETRY_BASE_INSTANCE_SIZE;
    }
    return geometry.estimateMemorySize();
}
 
Example 19
Source File: PagesRTreeIndex.java    From presto with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an array of addresses from {@link PagesIndex#valueAddresses} corresponding
 * to rows with matching geometries.
 * <p>
 * The caller is responsible for calling {@link #isJoinPositionEligible(int, int, Page)}
 * for each of these addresses to apply additional join filters.
 */
@Override
public int[] findJoinPositions(int probePosition, Page probe, int probeGeometryChannel, Optional<Integer> probePartitionChannel)
{
    Block probeGeometryBlock = probe.getBlock(probeGeometryChannel);
    if (probeGeometryBlock.isNull(probePosition)) {
        return EMPTY_ADDRESSES;
    }

    int probePartition = probePartitionChannel.map(channel -> toIntExact(INTEGER.getLong(probe.getBlock(channel), probePosition))).orElse(-1);

    Slice slice = probeGeometryBlock.getSlice(probePosition, 0, probeGeometryBlock.getSliceLength(probePosition));
    OGCGeometry probeGeometry = deserialize(slice);
    verifyNotNull(probeGeometry);
    if (probeGeometry.isEmpty()) {
        return EMPTY_ADDRESSES;
    }

    boolean probeIsPoint = probeGeometry instanceof OGCPoint;

    IntArrayList matchingPositions = new IntArrayList();

    Envelope envelope = getEnvelope(probeGeometry);
    rtree.query(envelope, item -> {
        GeometryWithPosition geometryWithPosition = (GeometryWithPosition) item;
        OGCGeometry buildGeometry = geometryWithPosition.getGeometry();
        if (partitions.isEmpty() || (probePartition == geometryWithPosition.getPartition() && (probeIsPoint || (buildGeometry instanceof OGCPoint) || testReferencePoint(envelope, buildGeometry, probePartition)))) {
            if (radiusChannel == -1) {
                if (spatialRelationshipTest.apply(buildGeometry, probeGeometry, OptionalDouble.empty())) {
                    matchingPositions.add(geometryWithPosition.getPosition());
                }
            }
            else {
                if (spatialRelationshipTest.apply(geometryWithPosition.getGeometry(), probeGeometry, OptionalDouble.of(getRadius(geometryWithPosition.getPosition())))) {
                    matchingPositions.add(geometryWithPosition.getPosition());
                }
            }
        }
    });

    return matchingPositions.toIntArray(null);
}
 
Example 20
Source File: PagesSpatialIndexSupplier.java    From presto with Apache License 2.0 4 votes vote down vote up
private static STRtree buildRTree(LongArrayList addresses, List<List<Block>> channels, int geometryChannel, Optional<Integer> radiusChannel, Optional<Integer> partitionChannel)
{
    STRtree rtree = new STRtree();
    Operator relateOperator = OperatorFactoryLocal.getInstance().getOperator(Operator.Type.Relate);

    for (int position = 0; position < addresses.size(); position++) {
        long pageAddress = addresses.getLong(position);
        int blockIndex = decodeSliceIndex(pageAddress);
        int blockPosition = decodePosition(pageAddress);

        Block block = channels.get(geometryChannel).get(blockIndex);
        // TODO Consider pushing is-null and is-empty checks into a filter below the join
        if (block.isNull(blockPosition)) {
            continue;
        }

        Slice slice = block.getSlice(blockPosition, 0, block.getSliceLength(blockPosition));
        OGCGeometry ogcGeometry = deserialize(slice);
        verifyNotNull(ogcGeometry);
        if (ogcGeometry.isEmpty()) {
            continue;
        }

        double radius = radiusChannel.map(channel -> DOUBLE.getDouble(channels.get(channel).get(blockIndex), blockPosition)).orElse(0.0);
        if (radius < 0) {
            continue;
        }

        if (radiusChannel.isEmpty()) {
            // If radiusChannel is supplied, this is a distance query, for which our acceleration won't help.
            accelerateGeometry(ogcGeometry, relateOperator);
        }

        int partition = -1;
        if (partitionChannel.isPresent()) {
            Block partitionBlock = channels.get(partitionChannel.get()).get(blockIndex);
            partition = toIntExact(INTEGER.getLong(partitionBlock, blockPosition));
        }

        rtree.insert(getEnvelope(ogcGeometry, radius), new GeometryWithPosition(ogcGeometry, partition, position));
    }

    rtree.build();
    return rtree;
}