Search in sources :

Example 1 with OGCPoint

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

the class SphericalGeoFunctions method stSphericalCentroid.

@SqlNullable
@Description("Returns the Point value that is the mathematical centroid of a Spherical Geography")
@ScalarFunction("ST_Centroid")
@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME)
public static Slice stSphericalCentroid(@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = EsriGeometrySerde.deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }
    // TODO: add support for other types e.g. POLYGON
    validateSphericalType("ST_Centroid", geometry, EnumSet.of(POINT, MULTI_POINT));
    if (geometry instanceof OGCPoint) {
        return input;
    }
    OGCGeometryCollection geometryCollection = (OGCGeometryCollection) geometry;
    for (int i = 0; i < geometryCollection.numGeometries(); i++) {
        OGCGeometry g = geometryCollection.geometryN(i);
        validateSphericalType("ST_Centroid", g, EnumSet.of(POINT));
        Point p = (Point) g.getEsriGeometry();
        checkLongitude(p.getX());
        checkLatitude(p.getY());
    }
    Point centroid;
    if (geometryCollection.numGeometries() == 1) {
        centroid = (Point) geometryCollection.geometryN(0).getEsriGeometry();
    } else {
        double x3DTotal = 0;
        double y3DTotal = 0;
        double z3DTotal = 0;
        for (int i = 0; i < geometryCollection.numGeometries(); i++) {
            CartesianPoint cp = new CartesianPoint((Point) geometryCollection.geometryN(i).getEsriGeometry());
            x3DTotal += cp.getX();
            y3DTotal += cp.getY();
            z3DTotal += cp.getZ();
        }
        double centroidVectorLength = Math.sqrt(x3DTotal * x3DTotal + y3DTotal * y3DTotal + z3DTotal * z3DTotal);
        if (centroidVectorLength == 0.0) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Unexpected error. Average vector length adds to zero (%f, %f, %f)", x3DTotal, y3DTotal, z3DTotal));
        }
        centroid = new CartesianPoint(x3DTotal / centroidVectorLength, y3DTotal / centroidVectorLength, z3DTotal / centroidVectorLength).asSphericalPoint();
    }
    return EsriGeometrySerde.serialize(new OGCPoint(centroid, geometryCollection.getEsriSpatialReference()));
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) OGCGeometryCollection(com.esri.core.geometry.ogc.OGCGeometryCollection) CartesianPoint(com.facebook.presto.geospatial.SphericalGeographyUtils.CartesianPoint) PrestoException(com.facebook.presto.spi.PrestoException) CartesianPoint(com.facebook.presto.geospatial.SphericalGeographyUtils.CartesianPoint) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) Point(com.esri.core.geometry.Point) CartesianPoint(com.facebook.presto.geospatial.SphericalGeographyUtils.CartesianPoint) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) Point(com.esri.core.geometry.Point) 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 2 with OGCPoint

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

the class EsriGeometrySerde method readPoint.

private static OGCPoint readPoint(BasicSliceInput input) {
    double x = input.readDouble();
    double y = input.readDouble();
    Point point;
    if (isNaN(x) || isNaN(y)) {
        point = new Point();
    } else {
        point = new Point(x, y);
    }
    return new OGCPoint(point, null);
}
Also used : OGCPoint(com.esri.core.geometry.ogc.OGCPoint) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) MultiPoint(com.esri.core.geometry.MultiPoint) OGCMultiPoint(com.esri.core.geometry.ogc.OGCMultiPoint) Point(com.esri.core.geometry.Point)

Example 3 with OGCPoint

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

the class GeometryUtils method isPointOrRectangle.

public static boolean isPointOrRectangle(OGCGeometry ogcGeometry, Envelope envelope) {
    if (ogcGeometry instanceof OGCPoint) {
        return true;
    }
    if (!(ogcGeometry instanceof OGCPolygon)) {
        return false;
    }
    Polygon polygon = (Polygon) ogcGeometry.getEsriGeometry();
    if (polygon.getPathCount() > 1) {
        return false;
    }
    if (polygon.getPointCount() != 4) {
        return false;
    }
    Set<Point> corners = new HashSet<>();
    corners.add(new Point(envelope.getXMin(), envelope.getYMin()));
    corners.add(new Point(envelope.getXMin(), envelope.getYMax()));
    corners.add(new Point(envelope.getXMax(), envelope.getYMin()));
    corners.add(new Point(envelope.getXMax(), envelope.getYMax()));
    for (int i = 0; i < 4; i++) {
        Point point = polygon.getPoint(i);
        if (!corners.contains(point)) {
            return false;
        }
    }
    return true;
}
Also used : OGCPoint(com.esri.core.geometry.ogc.OGCPoint) OGCPolygon(com.esri.core.geometry.ogc.OGCPolygon) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) Point(com.esri.core.geometry.Point) OGCPolygon(com.esri.core.geometry.ogc.OGCPolygon) Polygon(com.esri.core.geometry.Polygon) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) Point(com.esri.core.geometry.Point) HashSet(java.util.HashSet)

Example 4 with OGCPoint

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

the class TestGeoFunctions method assertApproximateCentroid.

private void assertApproximateCentroid(String wkt, Point expectedCentroid, double epsilon) {
    OGCPoint actualCentroid = (OGCPoint) EsriGeometrySerde.deserialize(stCentroid(EsriGeometrySerde.serialize(OGCGeometry.fromText(wkt))));
    assertEquals(actualCentroid.X(), expectedCentroid.getX(), epsilon);
    assertEquals(actualCentroid.Y(), expectedCentroid.getY(), epsilon);
}
Also used : OGCPoint(com.esri.core.geometry.ogc.OGCPoint)

Example 5 with OGCPoint

use of com.esri.core.geometry.ogc.OGCPoint in project pigeon by aseldawy.

the class MakePoint method exec.

@Override
public DataByteArray exec(Tuple input) throws IOException {
    if (input.size() != 2)
        throw new GeoException("MakePoint takes two numerical arguments");
    double x = ESRIGeometryParser.parseDouble(input.get(0));
    double y = ESRIGeometryParser.parseDouble(input.get(1));
    Point point = new Point(x, y);
    OGCPoint ogc_point = new OGCPoint(point, SpatialReference.create(4326));
    return new DataByteArray(ogc_point.asBinary().array());
}
Also used : OGCPoint(com.esri.core.geometry.ogc.OGCPoint) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) Point(com.esri.core.geometry.Point) DataByteArray(org.apache.pig.data.DataByteArray)

Aggregations

OGCPoint (com.esri.core.geometry.ogc.OGCPoint)5 Point (com.esri.core.geometry.Point)4 MultiPoint (com.esri.core.geometry.MultiPoint)1 Polygon (com.esri.core.geometry.Polygon)1 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)1 OGCGeometryCollection (com.esri.core.geometry.ogc.OGCGeometryCollection)1 OGCMultiPoint (com.esri.core.geometry.ogc.OGCMultiPoint)1 OGCPolygon (com.esri.core.geometry.ogc.OGCPolygon)1 CartesianPoint (com.facebook.presto.geospatial.SphericalGeographyUtils.CartesianPoint)1 PrestoException (com.facebook.presto.spi.PrestoException)1 Description (com.facebook.presto.spi.function.Description)1 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)1 SqlNullable (com.facebook.presto.spi.function.SqlNullable)1 SqlType (com.facebook.presto.spi.function.SqlType)1 HashSet (java.util.HashSet)1 DataByteArray (org.apache.pig.data.DataByteArray)1