use of io.prestosql.geospatial.GeometryType 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()));
}
use of io.prestosql.geospatial.GeometryType in project hetu-core by openlookeng.
the class GeoFunctions method lineLocatePoint.
@SqlNullable
@Description("Returns a float between 0 and 1 representing the location of the closest point on the LineString to the given Point, as a fraction of total 2d line length.")
@ScalarFunction("line_locate_point")
@SqlType(DOUBLE)
public static Double lineLocatePoint(@SqlType(GEOMETRY_TYPE_NAME) Slice lineSlice, @SqlType(GEOMETRY_TYPE_NAME) Slice pointSlice) {
Geometry line = JtsGeometrySerde.deserialize(lineSlice);
Geometry point = JtsGeometrySerde.deserialize(pointSlice);
if (line.isEmpty() || point.isEmpty()) {
return null;
}
GeometryType lineType = GeometryType.getForJtsGeometryType(line.getGeometryType());
if (lineType != GeometryType.LINE_STRING && lineType != GeometryType.MULTI_LINE_STRING) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("First argument to line_locate_point must be a LineString or a MultiLineString. Got: %s", line.getGeometryType()));
}
GeometryType pointType = GeometryType.getForJtsGeometryType(point.getGeometryType());
if (pointType != GeometryType.POINT) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Second argument to line_locate_point must be a Point. Got: %s", point.getGeometryType()));
}
return new LengthIndexedLine(line).indexOf(point.getCoordinate()) / line.getLength();
}
use of io.prestosql.geospatial.GeometryType 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.geospatial.GeometryType in project hetu-core by openlookeng.
the class GeoFunctions method stGeometries.
@SqlNullable
@Description("Returns an array of geometries in the specified collection")
@ScalarFunction("ST_Geometries")
@SqlType("array(" + GEOMETRY_TYPE_NAME + ")")
public static Block stGeometries(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
OGCGeometry geometry = deserialize(input);
if (geometry.isEmpty()) {
return null;
}
GeometryType type = GeometryType.getForEsriGeometryType(geometry.geometryType());
if (!type.isMultitype()) {
BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, 1);
GEOMETRY.writeSlice(blockBuilder, serialize(geometry));
return blockBuilder.build();
}
OGCGeometryCollection collection = (OGCGeometryCollection) geometry;
BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, collection.numGeometries());
for (int i = 0; i < collection.numGeometries(); i++) {
GEOMETRY.writeSlice(blockBuilder, serialize(collection.geometryN(i)));
}
return blockBuilder.build();
}
use of io.prestosql.geospatial.GeometryType in project hetu-core by openlookeng.
the class GeoFunctions method stNumGeometries.
@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();
}
Aggregations