use of com.facebook.presto.geospatial.GeometryType in project presto by prestodb.
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) {
Geometry geometry = deserialize(input);
if (geometry.isEmpty()) {
return null;
}
GeometryType type = GeometryType.getForJtsGeometryType(geometry.getGeometryType());
if (!type.isMultitype()) {
BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, 1);
GEOMETRY.writeSlice(blockBuilder, serialize(geometry));
return blockBuilder.build();
}
GeometryCollection collection = (GeometryCollection) geometry;
BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, collection.getNumGeometries());
for (int i = 0; i < collection.getNumGeometries(); i++) {
GEOMETRY.writeSlice(blockBuilder, serialize(collection.getGeometryN(i)));
}
return blockBuilder.build();
}
use of com.facebook.presto.geospatial.GeometryType in project presto by prestodb.
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 = deserialize(lineSlice);
Geometry point = 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 com.facebook.presto.geospatial.GeometryType in project presto by prestodb.
the class GeoFunctions method stGeometryN.
@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) {
Geometry geometry = deserialize(input);
if (geometry.isEmpty()) {
return null;
}
GeometryType type = GeometryType.getForJtsGeometryType(geometry.getGeometryType());
if (!type.isMultitype()) {
if (index == 1) {
return input;
}
return null;
}
GeometryCollection geometryCollection = ((GeometryCollection) geometry);
if (index < 1 || index > geometryCollection.getNumGeometries()) {
return null;
}
return serialize(geometryCollection.getGeometryN((int) index - 1));
}
use of com.facebook.presto.geospatial.GeometryType in project presto by prestodb.
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) {
Geometry geometry = deserialize(input);
validateType("ST_Centroid", geometry, EnumSet.of(POINT, MULTI_POINT, LINE_STRING, MULTI_LINE_STRING, POLYGON, MULTI_POLYGON));
GeometryType geometryType = GeometryType.getForJtsGeometryType(geometry.getGeometryType());
if (geometryType == GeometryType.POINT) {
return input;
}
if (geometry.getNumPoints() == 0) {
return serialize(createJtsEmptyPoint());
}
return serialize(geometry.getCentroid());
}
use of com.facebook.presto.geospatial.GeometryType in project presto by prestodb.
the class EsriGeometrySerde method writeGeometry.
private static void writeGeometry(DynamicSliceOutput output, OGCGeometry geometry) {
GeometryType type = GeometryType.getForEsriGeometryType(geometry.geometryType());
switch(type) {
case POINT:
writePoint(output, geometry);
break;
case MULTI_POINT:
writeSimpleGeometry(output, GeometrySerializationType.MULTI_POINT, geometry);
break;
case LINE_STRING:
writeSimpleGeometry(output, GeometrySerializationType.LINE_STRING, geometry);
break;
case MULTI_LINE_STRING:
writeSimpleGeometry(output, GeometrySerializationType.MULTI_LINE_STRING, geometry);
break;
case POLYGON:
writeSimpleGeometry(output, GeometrySerializationType.POLYGON, geometry);
break;
case MULTI_POLYGON:
writeSimpleGeometry(output, GeometrySerializationType.MULTI_POLYGON, geometry);
break;
case GEOMETRY_COLLECTION:
verify(geometry instanceof OGCConcreteGeometryCollection);
writeGeometryCollection(output, (OGCConcreteGeometryCollection) geometry);
break;
default:
throw new IllegalArgumentException("Unsupported geometry type: " + type);
}
}
Aggregations