Search in sources :

Example 1 with OGCMultiPolygon

use of com.esri.core.geometry.ogc.OGCMultiPolygon in project hetu-core by openlookeng.

the class GeoFunctions method stCentroid.

@Description("Returns the Point value that is the mathematical centroid of a Geometry")
@ScalarFunction("ST_Centroid")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stCentroid(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = deserialize(input);
    validateType("ST_Centroid", geometry, EnumSet.of(POINT, MULTI_POINT, LINE_STRING, MULTI_LINE_STRING, POLYGON, MULTI_POLYGON));
    GeometryType geometryType = GeometryType.getForEsriGeometryType(geometry.geometryType());
    if (geometryType == GeometryType.POINT) {
        return input;
    }
    int pointCount = ((MultiVertexGeometry) geometry.getEsriGeometry()).getPointCount();
    if (pointCount == 0) {
        return serialize(createFromEsriGeometry(new Point(), geometry.getEsriSpatialReference()));
    }
    Point centroid;
    switch(geometryType) {
        case MULTI_POINT:
            centroid = computePointsCentroid((MultiVertexGeometry) geometry.getEsriGeometry());
            break;
        case LINE_STRING:
        case MULTI_LINE_STRING:
            centroid = computeLineCentroid((Polyline) geometry.getEsriGeometry());
            break;
        case POLYGON:
            centroid = computePolygonCentroid((Polygon) geometry.getEsriGeometry());
            break;
        case MULTI_POLYGON:
            centroid = computeMultiPolygonCentroid((OGCMultiPolygon) geometry);
            break;
        default:
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Unexpected geometry type: " + geometryType);
    }
    return serialize(createFromEsriGeometry(centroid, geometry.getEsriSpatialReference()));
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GeometryType(io.prestosql.geospatial.GeometryType) MultiVertexGeometry(com.esri.core.geometry.MultiVertexGeometry) OGCMultiPolygon(com.esri.core.geometry.ogc.OGCMultiPolygon) Polyline(com.esri.core.geometry.Polyline) PrestoException(io.prestosql.spi.PrestoException) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) Polygon(com.esri.core.geometry.Polygon) OGCMultiPolygon(com.esri.core.geometry.ogc.OGCMultiPolygon) OGCPolygon(com.esri.core.geometry.ogc.OGCPolygon) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Example 2 with OGCMultiPolygon

use of com.esri.core.geometry.ogc.OGCMultiPolygon in project hetu-core by openlookeng.

the class GeoFunctions method computeMultiPolygonCentroid.

// MultiPolygon centroid is weighted mean of each polygon, weight in terms of polygon area
private static Point computeMultiPolygonCentroid(OGCMultiPolygon multiPolygon) {
    double xSum = 0;
    double ySum = 0;
    double weightSum = 0;
    for (int i = 0; i < multiPolygon.numGeometries(); i++) {
        Point centroid = computePolygonCentroid((Polygon) multiPolygon.geometryN(i).getEsriGeometry());
        Polygon polygon = (Polygon) multiPolygon.geometryN(i).getEsriGeometry();
        double weight = polygon.calculateArea2D();
        weightSum += weight;
        xSum += centroid.getX() * weight;
        ySum += centroid.getY() * weight;
    }
    return new Point(xSum / weightSum, ySum / weightSum);
}
Also used : MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) Polygon(com.esri.core.geometry.Polygon) OGCMultiPolygon(com.esri.core.geometry.ogc.OGCMultiPolygon) OGCPolygon(com.esri.core.geometry.ogc.OGCPolygon) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint)

Example 3 with OGCMultiPolygon

use of com.esri.core.geometry.ogc.OGCMultiPolygon in project trino by trinodb.

the class GeoFunctions method computeMultiPolygonCentroid.

// MultiPolygon centroid is weighted mean of each polygon, weight in terms of polygon area
private static Point computeMultiPolygonCentroid(OGCMultiPolygon multiPolygon) {
    double xSum = 0;
    double ySum = 0;
    double weightSum = 0;
    for (int i = 0; i < multiPolygon.numGeometries(); i++) {
        Point centroid = computePolygonCentroid((Polygon) multiPolygon.geometryN(i).getEsriGeometry());
        Polygon polygon = (Polygon) multiPolygon.geometryN(i).getEsriGeometry();
        double weight = polygon.calculateArea2D();
        weightSum += weight;
        xSum += centroid.getX() * weight;
        ySum += centroid.getY() * weight;
    }
    return new Point(xSum / weightSum, ySum / weightSum);
}
Also used : MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) Polygon(com.esri.core.geometry.Polygon) OGCMultiPolygon(com.esri.core.geometry.ogc.OGCMultiPolygon) OGCPolygon(com.esri.core.geometry.ogc.OGCPolygon) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint)

Example 4 with OGCMultiPolygon

use of com.esri.core.geometry.ogc.OGCMultiPolygon in project trino by trinodb.

the class GeoFunctions method stCentroid.

@Description("Returns the Point value that is the mathematical centroid of a Geometry")
@ScalarFunction("ST_Centroid")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stCentroid(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    OGCGeometry geometry = deserialize(input);
    validateType("ST_Centroid", geometry, EnumSet.of(POINT, MULTI_POINT, LINE_STRING, MULTI_LINE_STRING, POLYGON, MULTI_POLYGON));
    GeometryType geometryType = GeometryType.getForEsriGeometryType(geometry.geometryType());
    if (geometryType == GeometryType.POINT) {
        return input;
    }
    int pointCount = ((MultiVertexGeometry) geometry.getEsriGeometry()).getPointCount();
    if (pointCount == 0) {
        return serialize(createFromEsriGeometry(new Point(), geometry.getEsriSpatialReference()));
    }
    Point centroid;
    switch(geometryType) {
        case MULTI_POINT:
            centroid = computePointsCentroid((MultiVertexGeometry) geometry.getEsriGeometry());
            break;
        case LINE_STRING:
        case MULTI_LINE_STRING:
            centroid = computeLineCentroid((Polyline) geometry.getEsriGeometry());
            break;
        case POLYGON:
            centroid = computePolygonCentroid((Polygon) geometry.getEsriGeometry());
            break;
        case MULTI_POLYGON:
            centroid = computeMultiPolygonCentroid((OGCMultiPolygon) geometry);
            break;
        default:
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Unexpected geometry type: " + geometryType);
    }
    return serialize(createFromEsriGeometry(centroid, geometry.getEsriSpatialReference()));
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GeometryType(io.trino.geospatial.GeometryType) MultiVertexGeometry(com.esri.core.geometry.MultiVertexGeometry) OGCMultiPolygon(com.esri.core.geometry.ogc.OGCMultiPolygon) Polyline(com.esri.core.geometry.Polyline) TrinoException(io.trino.spi.TrinoException) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) Polygon(com.esri.core.geometry.Polygon) OGCMultiPolygon(com.esri.core.geometry.ogc.OGCMultiPolygon) OGCPolygon(com.esri.core.geometry.ogc.OGCPolygon) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Aggregations

MultiPoint (com.esri.core.geometry.MultiPoint)4 Point (com.esri.core.geometry.Point)4 Polygon (com.esri.core.geometry.Polygon)4 OGCMultiPolygon (com.esri.core.geometry.ogc.OGCMultiPolygon)4 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)4 OGCPolygon (com.esri.core.geometry.ogc.OGCPolygon)4 MultiVertexGeometry (com.esri.core.geometry.MultiVertexGeometry)2 Polyline (com.esri.core.geometry.Polyline)2 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)2 GeometryType (io.prestosql.geospatial.GeometryType)1 PrestoException (io.prestosql.spi.PrestoException)1 Description (io.prestosql.spi.function.Description)1 ScalarFunction (io.prestosql.spi.function.ScalarFunction)1 SqlType (io.prestosql.spi.function.SqlType)1 GeometryType (io.trino.geospatial.GeometryType)1 TrinoException (io.trino.spi.TrinoException)1 Description (io.trino.spi.function.Description)1 ScalarFunction (io.trino.spi.function.ScalarFunction)1 SqlType (io.trino.spi.function.SqlType)1