com.esri.core.geometry.GeometryCursor Java Examples

The following examples show how to use com.esri.core.geometry.GeometryCursor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeoFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #2
Source File: OGCGeometry.java    From geometry-api-java with Apache License 2.0 6 votes vote down vote up
public static OGCGeometry createFromEsriCursor(GeometryCursor gc,
		SpatialReference sr, boolean skipEmpty) {
	ArrayList<OGCGeometry> geoms = new ArrayList<OGCGeometry>(10);
	Geometry emptyGeom = null;
	for (Geometry g = gc.next(); g != null; g = gc.next()) {
		emptyGeom = g;
		if (!skipEmpty || !g.isEmpty())
			geoms.add(createFromEsriGeometry(g, sr));
	}

	if (geoms.size() == 1) {
		return geoms.get(0);
	} else if (geoms.size() == 0)
		return createFromEsriGeometry(emptyGeom, sr);
	else
		return new OGCConcreteGeometryCollection(geoms, sr);
}
 
Example #3
Source File: OGCGeometry.java    From geometry-api-java with Apache License 2.0 6 votes vote down vote up
public OGCGeometry union(OGCGeometry another) {
	String thisType = geometryType();
	String anotherType = another.geometryType();
	if (thisType != anotherType || thisType == OGCConcreteGeometryCollection.TYPE) {
		//heterogeneous union.
		//We make a geometry collection, then process to union parts and remove overlaps.
		ArrayList<OGCGeometry> geoms = new ArrayList<OGCGeometry>();
		geoms.add(this);
		geoms.add(another);
		OGCConcreteGeometryCollection geomCol = new OGCConcreteGeometryCollection(geoms, esriSR);
		return geomCol.flattenAndRemoveOverlaps().reduceFromMulti();
	}
	
	OperatorUnion op = (OperatorUnion) OperatorFactoryLocal.getInstance()
			.getOperator(Operator.Type.Union);
	GeometryCursorAppend ap = new GeometryCursorAppend(
			getEsriGeometryCursor(), another.getEsriGeometryCursor());
	com.esri.core.geometry.GeometryCursor cursor = op.execute(ap,
			getEsriSpatialReference(), null);
	return OGCGeometry.createFromEsriCursor(cursor, esriSR);
}
 
Example #4
Source File: OGCConcreteGeometryCollection.java    From geometry-api-java with Apache License 2.0 6 votes vote down vote up
private GeometryCursor removeOverlapsHelper_(List<Geometry> geoms) {
	List<Geometry> result = new ArrayList<Geometry>();
	for (int i = 0; i < geoms.size(); ++i) {
		Geometry current = geoms.get(i);
		if (current.isEmpty())
			continue;
		
		for (int j = i + 1; j < geoms.size(); ++j) {
			Geometry subG = geoms.get(j);
			current = OperatorDifference.local().execute(current, subG, esriSR, null);
			if (current.isEmpty())
				break;
		}
		
		if (current.isEmpty())
			continue;
		
		result.add(current);
	}
	
	return new SimpleGeometryCursor(result);
}
 
Example #5
Source File: GeometryUtils.java    From presto with Apache License 2.0 6 votes vote down vote up
public static int getPointCount(OGCGeometry ogcGeometry)
{
    GeometryCursor cursor = ogcGeometry.getEsriGeometryCursor();
    int points = 0;
    while (true) {
        com.esri.core.geometry.Geometry geometry = cursor.next();
        if (geometry == null) {
            return points;
        }

        if (geometry.isEmpty()) {
            continue;
        }

        if (geometry instanceof Point) {
            points++;
        }
        else {
            points += ((MultiVertexGeometry) geometry).getPointCount();
        }
    }
}
 
Example #6
Source File: GeoFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
@Description("Returns true if the input geometry is well formed")
@ScalarFunction("ST_IsValid")
@SqlType(BOOLEAN)
public static boolean stIsValid(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    GeometryCursor cursor = deserialize(input).getEsriGeometryCursor();
    while (true) {
        com.esri.core.geometry.Geometry geometry = cursor.next();
        if (geometry == null) {
            return true;
        }

        if (!OperatorSimplifyOGC.local().isSimpleOGC(geometry, null, true, null, null)) {
            return false;
        }
    }
}
 
Example #7
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Returns the reason for why the input geometry is not valid. Returns null if the input is valid.")
@ScalarFunction("geometry_invalid_reason")
@SqlType(VARCHAR)
@SqlNullable
public static Slice invalidReason(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    GeometryCursor cursor = deserialize(input).getEsriGeometryCursor();
    NonSimpleResult result = new NonSimpleResult();
    while (true) {
        com.esri.core.geometry.Geometry geometry = cursor.next();
        if (geometry == null) {
            return null;
        }

        if (!OperatorSimplifyOGC.local().isSimpleOGC(geometry, null, true, result, null)) {
            String reasonText = NON_SIMPLE_REASONS.getOrDefault(result.m_reason, result.m_reason.name());

            if (!(geometry instanceof MultiVertexGeometry)) {
                return utf8Slice(reasonText);
            }

            MultiVertexGeometry multiVertexGeometry = (MultiVertexGeometry) geometry;
            if (result.m_vertexIndex1 >= 0 && result.m_vertexIndex2 >= 0) {
                Point point1 = multiVertexGeometry.getPoint(result.m_vertexIndex1);
                Point point2 = multiVertexGeometry.getPoint(result.m_vertexIndex2);
                return utf8Slice(format("%s at or near (%s %s) and (%s %s)", reasonText, point1.getX(), point1.getY(), point2.getX(), point2.getY()));
            }

            if (result.m_vertexIndex1 >= 0) {
                Point point = multiVertexGeometry.getPoint(result.m_vertexIndex1);
                return utf8Slice(format("%s at or near (%s %s)", reasonText, point.getX(), point.getY()));
            }

            return utf8Slice(reasonText);
        }
    }
}
 
Example #8
Source File: PagesSpatialIndexSupplier.java    From presto with Apache License 2.0 5 votes vote down vote up
private static void accelerateGeometry(OGCGeometry ogcGeometry, Operator relateOperator)
{
    // Recurse into GeometryCollections
    GeometryCursor cursor = ogcGeometry.getEsriGeometryCursor();
    while (true) {
        com.esri.core.geometry.Geometry esriGeometry = cursor.next();
        if (esriGeometry == null) {
            break;
        }
        relateOperator.accelerateGeometry(esriGeometry, null, Geometry.GeometryAccelerationDegree.enumMild);
    }
}
 
Example #9
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Converts a Geometry object to a SphericalGeography object")
@ScalarFunction("to_spherical_geography")
@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME)
public static Slice toSphericalGeography(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    // "every point in input is in range" <=> "the envelope of input is in range"
    Envelope envelope = deserializeEnvelope(input);
    if (!envelope.isEmpty()) {
        checkLatitude(envelope.getYMin());
        checkLatitude(envelope.getYMax());
        checkLongitude(envelope.getXMin());
        checkLongitude(envelope.getXMax());
    }
    OGCGeometry geometry = deserialize(input);
    if (geometry.is3D()) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Cannot convert 3D geometry to a spherical geography");
    }

    GeometryCursor cursor = geometry.getEsriGeometryCursor();
    while (true) {
        com.esri.core.geometry.Geometry subGeometry = cursor.next();
        if (subGeometry == null) {
            break;
        }

        if (!GEOMETRY_TYPES_FOR_SPHERICAL_GEOGRAPHY.contains(subGeometry.getType())) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Cannot convert geometry of this type to spherical geography: " + subGeometry.getType());
        }
    }

    return input;
}
 
Example #10
Source File: OGCGeometry.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
public OGCGeometry intersection(OGCGeometry another) {
	if (another.geometryType() == OGCConcreteGeometryCollection.TYPE) {
		return (new OGCConcreteGeometryCollection(this, esriSR)).intersection(another);
	}

	com.esri.core.geometry.OperatorIntersection op = (OperatorIntersection) OperatorFactoryLocal
			.getInstance().getOperator(Operator.Type.Intersection);
	com.esri.core.geometry.GeometryCursor cursor = op.execute(
			getEsriGeometryCursor(), another.getEsriGeometryCursor(),
			getEsriSpatialReference(), null, 7);
	return OGCGeometry.createFromEsriCursor(cursor, esriSR, true);
}
 
Example #11
Source File: OGCGeometry.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
public OGCGeometry buffer(double distance) {
	OperatorBuffer op = (OperatorBuffer) OperatorFactoryLocal.getInstance()
			.getOperator(Operator.Type.Buffer);
	if (distance == 0) {// when distance is 0, return self (maybe we should
						// create a copy instead).
		return this;
	}

	double d[] = { distance };
	com.esri.core.geometry.GeometryCursor cursor = op.execute(
			getEsriGeometryCursor(), getEsriSpatialReference(), d, true,
			null);
	return OGCGeometry.createFromEsriGeometry(cursor.next(), esriSR);
}
 
Example #12
Source File: OGCConcreteGeometryCollection.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
private static List<Geometry> toList(GeometryCursor cursor)
{
	List<Geometry> geometries = new ArrayList<Geometry>();
	for (Geometry geometry = cursor.next(); geometry != null; geometry = cursor.next()) {
		geometries.add(geometry);
	}
	return geometries;
}
 
Example #13
Source File: OGCConcreteGeometryCollection.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
@Override
public OGCGeometry envelope() {
	GeometryCursor gc = getEsriGeometryCursor();
	Envelope env = new Envelope();
	for (Geometry g = gc.next(); g != null; g = gc.next()) {
		Envelope e = new Envelope();
		g.queryEnvelope(e);
		env.merge(e);
	}

	Polygon polygon = new Polygon();
	polygon.addEnvelope(env, false);
	return new OGCPolygon(polygon, esriSR);
}
 
Example #14
Source File: OGCConcreteGeometryCollection.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
public OGCConcreteGeometryCollection(GeometryCursor geoms,
		SpatialReference sr) {
	List<OGCGeometry> ogcGeoms = new ArrayList<OGCGeometry>(10);
	for (Geometry g = geoms.next(); g != null; g = geoms.next()) {
		ogcGeoms.add(createFromEsriGeometry(g, sr));
	}
	
	geometries = ogcGeoms;
	esriSR = sr;
}
 
Example #15
Source File: GeometryUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
public static boolean contains(OGCGeometry ogcGeometry, Envelope envelope)
{
    GeometryCursor cursor = ogcGeometry.getEsriGeometryCursor();
    while (true) {
        Geometry geometry = cursor.next();
        if (geometry == null) {
            return false;
        }

        if (GeometryEngine.contains(geometry, envelope, null)) {
            return true;
        }
    }
}
 
Example #16
Source File: GeometryUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
public static boolean disjoint(Envelope envelope, OGCGeometry ogcGeometry)
{
    GeometryCursor cursor = ogcGeometry.getEsriGeometryCursor();
    while (true) {
        Geometry geometry = cursor.next();
        if (geometry == null) {
            return true;
        }

        if (!GeometryEngine.disjoint(geometry, envelope, null)) {
            return false;
        }
    }
}
 
Example #17
Source File: GeometryUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Envelope getEnvelope(OGCGeometry ogcGeometry)
{
    GeometryCursor cursor = ogcGeometry.getEsriGeometryCursor();
    Envelope overallEnvelope = new Envelope();
    while (true) {
        Geometry geometry = cursor.next();
        if (geometry == null) {
            return overallEnvelope;
        }

        Envelope envelope = new Envelope();
        geometry.queryEnvelope(envelope);
        overallEnvelope.merge(envelope);
    }
}
 
Example #18
Source File: OGCConcreteGeometryCollection.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
@Override
public GeometryCursor getEsriGeometryCursor() {
	return new GeometryCursorOGC(geometries);
}
 
Example #19
Source File: OGCConcreteGeometryCollection.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
@Override
public OGCGeometry convexHull() {
	GeometryCursor cursor = OperatorConvexHull.local().execute(
			getEsriGeometryCursor(), false, null);
	MultiPoint mp = new MultiPoint();
	Polygon polygon = new Polygon();
	VertexDescription vd = null;
	for (Geometry geom = cursor.next(); geom != null; geom = cursor.next()) {
		vd = geom.getDescription();
		if (geom.isEmpty())
			continue;

		if (geom.getType() == Geometry.Type.Polygon) {
			polygon.add((MultiPath) geom, false);
		}
		else if (geom.getType() == Geometry.Type.Polyline) {
			mp.add((MultiVertexGeometry) geom, 0, -1);
		}
		else if (geom.getType() == Geometry.Type.Point) {
			mp.add((Point) geom);
		}
		else {
			throw new GeometryException("internal error");
		}
	}

	Geometry resultGeom = null;
	if (!mp.isEmpty()) {
		resultGeom = OperatorConvexHull.local().execute(mp, null);
	}

	if (!polygon.isEmpty()) {
		if (resultGeom != null && !resultGeom.isEmpty()) {
			Geometry[] geoms = { resultGeom, polygon };
			resultGeom = OperatorConvexHull.local().execute(
					new SimpleGeometryCursor(geoms), true, null).next();
		}
		else {
			resultGeom = OperatorConvexHull.local().execute(polygon, null);
		}
	}

	if (resultGeom == null) {
		Point pt = new Point();
		if (vd != null)
			pt.assignVertexDescription(vd);

		return new OGCPoint(pt, getEsriSpatialReference());
	}

	return OGCGeometry.createFromEsriGeometry(resultGeom, getEsriSpatialReference(), false);
}
 
Example #20
Source File: OGCConcreteGeometryCollection.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
private GeometryCursor prepare_for_ops_(OGCConcreteGeometryCollection collection) {
	assert(collection != null && !collection.isEmpty());
	GeometryCursor prepared = OGCStructureInternal.prepare_for_ops_(collection.flatten().getEsriGeometryCursor(), esriSR);
	return removeOverlapsHelper_(toList(prepared));
}
 
Example #21
Source File: OGCGeometry.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
public OGCGeometry convexHull() {
	com.esri.core.geometry.GeometryCursor cursor = OperatorConvexHull.local().execute(
			getEsriGeometryCursor(), false, null);
	return OGCGeometry.createFromEsriCursor(cursor, esriSR);
}
 
Example #22
Source File: OGCGeometry.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
public GeometryCursor getEsriGeometryCursor() {
	return new SimpleGeometryCursor(getEsriGeometry());
}
 
Example #23
Source File: GeoFunctions.java    From presto with Apache License 2.0 4 votes vote down vote up
private static Slice stUnion(Iterable<Slice> slices)
{
    // The current state of Esri/geometry-api-java does not allow support for multiple dimensions being
    // fed to the union operator without dropping the lower dimensions:
    // https://github.com/Esri/geometry-api-java/issues/199
    // When operating over a collection of geometries, it is more efficient to reuse the same operator
    // for the entire operation.  Therefore, split the inputs and operators by dimension, and then union
    // each dimension's result at the end.
    ListeningGeometryCursor[] cursorsByDimension = new ListeningGeometryCursor[NUMBER_OF_DIMENSIONS];
    GeometryCursor[] operatorsByDimension = new GeometryCursor[NUMBER_OF_DIMENSIONS];

    setAll(cursorsByDimension, i -> new ListeningGeometryCursor());
    setAll(operatorsByDimension, i -> OperatorUnion.local().execute(cursorsByDimension[i], null, null));

    Iterator<Slice> slicesIterator = slices.iterator();
    if (!slicesIterator.hasNext()) {
        return null;
    }
    while (slicesIterator.hasNext()) {
        Slice slice = slicesIterator.next();
        // Ignore null inputs
        if (slice.getInput().available() == 0) {
            continue;
        }

        for (OGCGeometry geometry : flattenCollection(deserialize(slice))) {
            int dimension = geometry.dimension();
            cursorsByDimension[dimension].tick(geometry.getEsriGeometry());
            operatorsByDimension[dimension].tock();
        }
    }

    List<OGCGeometry> outputs = new ArrayList<>();
    for (GeometryCursor operator : operatorsByDimension) {
        OGCGeometry unionedGeometry = createFromEsriGeometry(operator.next(), null);
        if (unionedGeometry != null) {
            outputs.add(unionedGeometry);
        }
    }

    if (outputs.size() == 1) {
        return serialize(outputs.get(0));
    }
    return serialize(new OGCConcreteGeometryCollection(outputs, null).flattenAndRemoveOverlaps().reduceFromMulti());
}
 
Example #24
Source File: OGCConcreteGeometryCollection.java    From geometry-api-java with Apache License 2.0 3 votes vote down vote up
/**
 * Fixes topological overlaps in the GeometryCollecion.
 * This is equivalent to union of the geometry collection elements.
 *
 * TODO "flattened" collection is supposed to contain only mutli-geometries, but this method may return single geometries
 * e.g. for GEOMETRYCOLLECTION (LINESTRING (...)) it returns GEOMETRYCOLLECTION (LINESTRING (...))
 * and not GEOMETRYCOLLECTION (MULTILINESTRING (...))
 * @return A geometry collection that is flattened and has no overlapping elements.
 */
public OGCConcreteGeometryCollection flattenAndRemoveOverlaps() {

	//flatten and crack/cluster
	GeometryCursor cursor = OGCStructureInternal.prepare_for_ops_(flatten().getEsriGeometryCursor(), esriSR);

	//make sure geometries don't overlap
	return new OGCConcreteGeometryCollection(removeOverlapsHelper_(toList(cursor)), esriSR);
}
 
Example #25
Source File: OGCGeometry.java    From geometry-api-java with Apache License 2.0 2 votes vote down vote up
/**
 * Create an OGCGeometry instance from the GeometryCursor.
 * 
 * @param gc
 * @param sr
 * @return Geometry instance created from the geometry cursor.
 */
public static OGCGeometry createFromEsriCursor(GeometryCursor gc,
		SpatialReference sr) {
	return createFromEsriCursor(gc, sr, false);
}