use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.
the class GeoFunctions method stEnvelopeAsPts.
@SqlNullable
@Description("Returns the lower left and upper right corners of bounding rectangular polygon of a Geometry")
@ScalarFunction("ST_EnvelopeAsPts")
@SqlType("array(" + GEOMETRY_TYPE_NAME + ")")
public static Block stEnvelopeAsPts(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
Envelope envelope = deserializeEnvelope(input);
if (envelope == null) {
return null;
}
BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, 2);
Point lowerLeftCorner = new Point(envelope.getXMin(), envelope.getYMin());
Point upperRightCorner = new Point(envelope.getXMax(), envelope.getYMax());
GEOMETRY.writeSlice(blockBuilder, serialize(createFromEsriGeometry(lowerLeftCorner, null, false)));
GEOMETRY.writeSlice(blockBuilder, serialize(createFromEsriGeometry(upperRightCorner, null, false)));
return blockBuilder.build();
}
use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.
the class GeoFunctions method stRelate.
@SqlNullable
@Description("Returns TRUE if this Geometry is spatially related to another Geometry")
@ScalarFunction("ST_Relate")
@SqlType(BOOLEAN)
public static Boolean stRelate(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right, @SqlType(VARCHAR) Slice relation) {
OGCGeometry leftGeometry = deserialize(left);
OGCGeometry rightGeometry = deserialize(right);
verifySameSpatialReference(leftGeometry, rightGeometry);
return leftGeometry.relate(rightGeometry, relation.toStringUtf8());
}
use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.
the class GeoFunctions method stArea.
@Description("Returns the 2D Euclidean area of a geometry")
@ScalarFunction("ST_Area")
@SqlType(DOUBLE)
public static double stArea(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
OGCGeometry geometry = deserialize(input);
// The Esri geometry library does not support area for geometry collections. We compute the area
// of collections by summing the area of the individual components.
GeometryType type = GeometryType.getForEsriGeometryType(geometry.geometryType());
if (type == GeometryType.GEOMETRY_COLLECTION) {
double area = 0.0;
GeometryCursor cursor = geometry.getEsriGeometryCursor();
while (true) {
com.esri.core.geometry.Geometry esriGeometry = cursor.next();
if (esriGeometry == null) {
return area;
}
area += esriGeometry.calculateArea2D();
}
}
return geometry.getEsriGeometry().calculateArea2D();
}
use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.
the class GeoFunctions method stSphericalDistance.
@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;
}
use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.
the class GeoFunctions method stSphericalArea.
@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)));
}
// isExteriorRing returns false for the exterior ring
return Math.abs(sphericalExcess * EARTH_RADIUS_M * EARTH_RADIUS_M);
}
Aggregations