Search in sources :

Example 1 with TopologyException

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);
    }
}
Also used : GeometryUtils.wktFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry) OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCGeometry.createFromEsriGeometry(com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry) Geometry(org.locationtech.jts.geom.Geometry) GeometryUtils.jsonFromJtsGeometry(com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry) Coordinate(org.locationtech.jts.geom.Coordinate) PrestoException(com.facebook.presto.spi.PrestoException) TopologyException(org.locationtech.jts.geom.TopologyException) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 2 with TopologyException

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);
    }
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) TopologyException(org.locationtech.jts.geom.TopologyException) BasicSliceInput(io.airlift.slice.BasicSliceInput)

Example 3 with TopologyException

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);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) InstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryFinder(eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) TopologyException(org.locationtech.jts.geom.TopologyException) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)

Example 4 with TopologyException

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);
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) ArrayList(java.util.ArrayList) LinearRing(org.locationtech.jts.geom.LinearRing) Polygon(org.locationtech.jts.geom.Polygon) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) TopologyException(org.locationtech.jts.geom.TopologyException)

Aggregations

TopologyException (org.locationtech.jts.geom.TopologyException)4 PrestoException (com.facebook.presto.spi.PrestoException)2 Coordinate (org.locationtech.jts.geom.Coordinate)2 Geometry (org.locationtech.jts.geom.Geometry)2 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)1 OGCGeometry.createFromEsriGeometry (com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry)1 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 GeometryUtils.jsonFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry)1 GeometryUtils.wktFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry)1 Description (com.facebook.presto.spi.function.Description)1 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)1 SqlNullable (com.facebook.presto.spi.function.SqlNullable)1 SqlType (com.facebook.presto.spi.function.SqlType)1 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)1 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)1 GeometryFinder (eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder)1 DepthFirstInstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)1 InstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser)1 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)1 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)1