use of com.esri.core.geometry.ogc.OGCGeometryCollection in project pigeon by aseldawy.
the class Connect method exec.
@Override
public DataByteArray exec(Tuple b) throws IOException {
try {
// Read information from input
Iterator<Tuple> firstPointIdIter = ((DataBag) b.get(0)).iterator();
Iterator<Tuple> lastPointIdIter = ((DataBag) b.get(1)).iterator();
Iterator<Tuple> shapesIter = ((DataBag) b.get(2)).iterator();
// Shapes that are created after connected line segments
Vector<OGCGeometry> createdShapes = new Vector<OGCGeometry>();
Vector<OGCLineString> linestrings = new Vector<OGCLineString>();
Vector<Long> firstPointId = new Vector<Long>();
Vector<Long> lastPointId = new Vector<Long>();
while (firstPointIdIter.hasNext() && lastPointIdIter.hasNext() && shapesIter.hasNext()) {
OGCGeometry geom = geometryParser.parseGeom(shapesIter.next().get(0));
long first_point_id = (Long) firstPointIdIter.next().get(0);
long last_point_id = (Long) lastPointIdIter.next().get(0);
if (geom.isEmpty()) {
// Skip empty geometries
} else if (geom instanceof OGCPolygon) {
// Copy to output directly. Polygons cannot be connected to other shapes.
createdShapes.add(geom);
} else if (geom instanceof OGCLineString) {
linestrings.add((OGCLineString) geom);
firstPointId.add(first_point_id);
lastPointId.add(last_point_id);
} else {
throw new GeoException("Cannot connect shapes of type " + geom.getClass());
}
}
if (firstPointIdIter.hasNext() || lastPointIdIter.hasNext() || shapesIter.hasNext()) {
throw new ExecException("All parameters should be of the same size (" + firstPointId.size() + "," + lastPointId.size() + "," + linestrings.size() + ")");
}
// Stores an ordered list of line segments in current connected block
Vector<OGCLineString> connected_lines = new Vector<OGCLineString>();
// Total number of points in all visited linestrings
int sumPoints = 0;
// Which linestrings to reverse upon connection
Vector<Boolean> reverse = new Vector<Boolean>();
long first_point_id = -1;
long last_point_id = -1;
// Reorder linestrings to form a contiguous list of connected linestrings
while (!linestrings.isEmpty()) {
// Loop invariant:
// At the beginning of each iteration, the lines in connected_lines are connected.
// In each iteration, we move one linestring from linestrings to connected_lines
// while keeping them connected
int size_before = connected_lines.size();
for (int i = 0; i < linestrings.size(); ) {
if (connected_lines.isEmpty()) {
// First linestring
first_point_id = firstPointId.remove(i);
last_point_id = lastPointId.remove(i);
reverse.add(false);
sumPoints += linestrings.get(i).numPoints();
connected_lines.add(linestrings.remove(i));
} else if (lastPointId.get(i) == first_point_id) {
// This linestring goes to the beginning of the list as-is
lastPointId.remove(i);
first_point_id = firstPointId.remove(i);
sumPoints += linestrings.get(i).numPoints();
connected_lines.add(0, linestrings.remove(i));
reverse.add(0, false);
} else if (firstPointId.get(i) == first_point_id) {
// Should go to the beginning after being reversed
firstPointId.remove(i);
first_point_id = lastPointId.remove(i);
sumPoints += linestrings.get(i).numPoints();
connected_lines.add(0, linestrings.remove(i));
reverse.add(0, true);
} else if (firstPointId.get(i) == last_point_id) {
// This linestring goes to the end of the list as-is
firstPointId.remove(i);
last_point_id = lastPointId.remove(i);
sumPoints += linestrings.get(i).numPoints();
connected_lines.add(linestrings.remove(i));
reverse.add(false);
} else if (lastPointId.get(i) == last_point_id) {
// Should go to the end after being reversed
lastPointId.remove(i);
last_point_id = firstPointId.remove(i);
sumPoints += linestrings.get(i).numPoints();
connected_lines.add(linestrings.remove(i));
reverse.add(true);
} else {
i++;
}
}
if (connected_lines.size() == size_before || linestrings.isEmpty()) {
// Cannot connect any more lines to the current block. Emit as a shape
boolean isPolygon = first_point_id == last_point_id;
Point[] points = new Point[sumPoints - connected_lines.size() + (isPolygon ? 0 : 1)];
int n = 0;
for (int i = 0; i < connected_lines.size(); i++) {
OGCLineString linestring = connected_lines.get(i);
boolean isReverse = reverse.get(i);
int last_i = (isPolygon || i < connected_lines.size() - 1) ? linestring.numPoints() - 1 : linestring.numPoints();
for (int i_point = 0; i_point < last_i; i_point++) {
points[n++] = (Point) linestring.pointN(isReverse ? linestring.numPoints() - 1 - i_point : i_point).getEsriGeometry();
}
}
MultiPath multi_path = isPolygon ? new Polygon() : new Polyline();
for (int i = 1; i < points.length; i++) {
Segment segment = new Line();
segment.setStart(points[i - 1]);
segment.setEnd(points[i]);
multi_path.addSegment(segment, false);
}
createdShapes.add(isPolygon ? new OGCPolygon((Polygon) multi_path, 0, SpatialReference.create(4326)) : new OGCLineString((Polyline) multi_path, 0, SpatialReference.create(4326)));
// Re-initialize all data structures to connect remaining lines
if (!linestrings.isEmpty()) {
connected_lines.clear();
reverse.clear();
sumPoints = 0;
}
}
}
if (createdShapes.size() == 1) {
return new DataByteArray(createdShapes.get(0).asBinary().array());
} else if (createdShapes.size() > 1) {
OGCGeometryCollection collection = new OGCConcreteGeometryCollection(createdShapes, createdShapes.get(0).getEsriSpatialReference());
return new DataByteArray(collection.asBinary().array());
} else {
throw new GeoException("No shapes to connect");
}
} catch (Exception e) {
throw new GeoException(e);
}
}
use of com.esri.core.geometry.ogc.OGCGeometryCollection in project pigeon by aseldawy.
the class Union method union.
protected static OGCGeometry union(Tuple input) throws ExecException {
DataBag values = (DataBag) input.get(0);
if (values.size() == 0)
return null;
ArrayList<OGCGeometry> all_geoms = new ArrayList<OGCGeometry>();
for (Tuple one_geom : values) {
OGCGeometry parsedGeom = geometryParser.parseGeom(one_geom.get(0));
all_geoms.add(parsedGeom);
}
// Do a union of all_geometries in the recommended way (using buffer(0))
OGCGeometryCollection geom_collection = new OGCConcreteGeometryCollection(all_geoms, all_geoms.get(0).getEsriSpatialReference());
return geom_collection.union(all_geoms.get(0));
}
use of com.esri.core.geometry.ogc.OGCGeometryCollection in project pigeon by aseldawy.
the class Extent method extent.
protected static OGCGeometry extent(Tuple input) throws ExecException {
DataBag values = (DataBag) input.get(0);
if (values.size() == 0)
return null;
ArrayList<OGCGeometry> all_geoms = new ArrayList<OGCGeometry>();
for (Tuple one_geom : values) {
OGCGeometry parsedGeom = geometryParser.parseGeom(one_geom.get(0));
all_geoms.add(parsedGeom);
}
// Do a union of all_geometries in the recommended way (using buffer(0))
OGCGeometryCollection geom_collection = new OGCConcreteGeometryCollection(all_geoms, all_geoms.get(0).getEsriSpatialReference());
return geom_collection.envelope();
}
use of com.esri.core.geometry.ogc.OGCGeometryCollection in project pigeon by aseldawy.
the class TestHelper method assertGeometryEqual.
public static void assertGeometryEqual(Object expected, Object test) {
OGCGeometry expected_geom = expected instanceof String ? OGCGeometry.fromText((String) expected) : OGCGeometry.fromBinary(ByteBuffer.wrap(((DataByteArray) expected).get()));
OGCGeometry test_geom = test instanceof String ? OGCGeometry.fromText((String) test) : OGCGeometry.fromBinary(ByteBuffer.wrap(((DataByteArray) test).get()));
if (expected_geom instanceof OGCGeometryCollection && test_geom instanceof OGCGeometryCollection) {
OGCGeometryCollection expected_coln = (OGCGeometryCollection) expected_geom;
OGCGeometryCollection test_coln = (OGCGeometryCollection) test_geom;
assertEquals(expected_coln.numGeometries(), test_coln.numGeometries());
Vector<OGCGeometry> expectedGeometries = new Vector<OGCGeometry>();
for (int i = 0; i < expected_coln.numGeometries(); i++) {
expectedGeometries.add(expected_coln.geometryN(i));
}
for (int i = 0; i < test_coln.numGeometries(); i++) {
OGCGeometry geom = test_coln.geometryN(i);
int j = 0;
while (j < expectedGeometries.size() && !geom.equals(expectedGeometries.get(j))) j++;
assertTrue(j < expectedGeometries.size());
expectedGeometries.remove(j++);
}
} else {
assertTrue("Exepcted geometry to be '" + expected + "' but found '" + test + "'", expected_geom.equals(test_geom));
}
}
use of com.esri.core.geometry.ogc.OGCGeometryCollection in project pigeon by aseldawy.
the class ConvexHull method convexHull.
protected static OGCGeometry convexHull(Tuple input) throws ExecException {
DataBag values = (DataBag) input.get(0);
if (values.size() == 0)
return null;
ArrayList<OGCGeometry> all_geoms = new ArrayList<OGCGeometry>();
for (Tuple one_geom : values) {
OGCGeometry parsedGeom = geometryParser.parseGeom(one_geom.get(0));
all_geoms.add(parsedGeom);
}
// Do a convex null of all_geometries
OGCGeometryCollection geom_collection = new OGCConcreteGeometryCollection(all_geoms, all_geoms.get(0).getEsriSpatialReference());
return geom_collection.convexHull();
}
Aggregations