use of org.locationtech.jts.geom.TopologyException in project presto by prestodb.
the class GeoFunctions method geometryNearestPoints.
@SqlNullable
@Description("Return the closest points on the two geometries")
@ScalarFunction("geometry_nearest_points")
@SqlType("array(" + GEOMETRY_TYPE_NAME + ")")
public static Block geometryNearestPoints(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right) {
Geometry leftGeometry = deserialize(left);
Geometry rightGeometry = deserialize(right);
if (leftGeometry.isEmpty() || rightGeometry.isEmpty()) {
return null;
}
try {
Coordinate[] nearestCoordinates = DistanceOp.nearestPoints(leftGeometry, rightGeometry);
BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, 2);
GEOMETRY.writeSlice(blockBuilder, serialize(createJtsPoint(nearestCoordinates[0])));
GEOMETRY.writeSlice(blockBuilder, serialize(createJtsPoint(nearestCoordinates[1])));
return blockBuilder.build();
} catch (TopologyException e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getMessage(), e);
}
}
use of org.locationtech.jts.geom.TopologyException in project presto by prestodb.
the class JtsGeometrySerde method deserialize.
public static Geometry deserialize(Slice shape) {
requireNonNull(shape, "shape is null");
BasicSliceInput input = shape.getInput();
verify(input.available() > 0);
GeometrySerializationType type = GeometrySerializationType.getForCode(input.readByte());
try {
return readGeometry(input, type);
} catch (TopologyException e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getMessage(), e);
}
}
use of org.locationtech.jts.geom.TopologyException in project hale by halestudio.
the class InteriorPoint method calculateInteriorPoint.
/**
* Calculate an interior point for a given geometry or object holding a
* geometry.
*
* @param geometryHolder {@link Geometry}, {@link GeometryProperty} or
* {@link Instance} holding a geometry
* @return an interior point of the geometry
* @throws TransformationException if the interior point could not be
* calculated
*/
public static GeometryProperty<?> calculateInteriorPoint(Object geometryHolder) throws TransformationException {
// depth first traverser that on cancel continues traversal but w/o the
// children of the current object
InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
GeometryFinder geoFind = new GeometryFinder(null);
traverser.traverse(geometryHolder, geoFind);
List<GeometryProperty<?>> geoms = geoFind.getGeometries();
Geometry result;
CRSDefinition oldCRS = null;
// use the first geometry encountered
int index = 0;
Geometry geom = null;
while (geom == null && index < geoms.size()) {
geom = geoms.get(index).getGeometry();
oldCRS = geoms.get(index).getCRSDefinition();
index++;
}
if (geom != null) {
try {
result = geom.getInteriorPoint();
} catch (TopologyException e) {
// calculate the point for a geometry with a small buffer to
// avoid error with polygons that have overlapping lines
result = geom.buffer(0.000001).getInteriorPoint();
if (!result.within(geom)) {
// geometry
throw new TransformationException("Could not determine interior point for geometry");
}
}
} else {
return null;
}
return new DefaultGeometryProperty<Geometry>(oldCRS, result);
}
use of org.locationtech.jts.geom.TopologyException in project presto by prestodb.
the class JtsGeometrySerde method readPolygon.
private static Geometry readPolygon(SliceInput input, boolean multitype) {
skipEsriType(input);
skipEnvelope(input);
int partCount = input.readInt();
if (partCount == 0) {
if (multitype) {
return GEOMETRY_FACTORY.createMultiPolygon();
}
return GEOMETRY_FACTORY.createPolygon();
}
int pointCount = input.readInt();
int[] startIndexes = new int[partCount];
for (int i = 0; i < partCount; i++) {
startIndexes[i] = input.readInt();
}
int[] partLengths = new int[partCount];
if (partCount > 1) {
partLengths[0] = startIndexes[1];
for (int i = 1; i < partCount - 1; i++) {
partLengths[i] = startIndexes[i + 1] - startIndexes[i];
}
}
partLengths[partCount - 1] = pointCount - startIndexes[partCount - 1];
LinearRing shell = null;
List<LinearRing> holes = new ArrayList<>();
List<Polygon> polygons = new ArrayList<>();
try {
for (int i = 0; i < partCount; i++) {
Coordinate[] coordinates = readCoordinates(input, partLengths[i]);
if (isClockwise(coordinates)) {
// next polygon has started
if (shell != null) {
polygons.add(GEOMETRY_FACTORY.createPolygon(shell, holes.toArray(new LinearRing[0])));
holes.clear();
}
shell = GEOMETRY_FACTORY.createLinearRing(coordinates);
} else {
holes.add(GEOMETRY_FACTORY.createLinearRing(coordinates));
}
}
polygons.add(GEOMETRY_FACTORY.createPolygon(shell, holes.toArray(new LinearRing[0])));
} catch (IllegalArgumentException e) {
throw new TopologyException("Error constructing Polygon: " + e.getMessage());
}
if (multitype) {
return GEOMETRY_FACTORY.createMultiPolygon(polygons.toArray(new Polygon[0]));
}
return getOnlyElement(polygons);
}
Aggregations