Search in sources :

Example 1 with Polyline

use of com.esri.core.geometry.Polyline in project sis by apache.

the class ESRI method createPolyline.

/**
 * Creates a polyline from the given ordinate values.
 * Each {@link Double#NaN} ordinate value start a new path.
 * The implementation returned by this method must be an instance of {@link #rootClass}.
 */
@Override
public Geometry createPolyline(final int dimension, final Vector... ordinates) {
    if (dimension != 2) {
        throw unsupported(dimension);
    }
    boolean lineTo = false;
    final Polyline path = new Polyline();
    for (final Vector v : ordinates) {
        if (v != null) {
            final int size = v.size();
            for (int i = 0; i < size; ) {
                final double x = v.doubleValue(i++);
                final double y = v.doubleValue(i++);
                if (Double.isNaN(x) || Double.isNaN(y)) {
                    lineTo = false;
                } else if (lineTo) {
                    path.lineTo(x, y);
                } else {
                    path.startPath(x, y);
                    lineTo = true;
                }
            }
        }
    }
    return path;
}
Also used : Polyline(com.esri.core.geometry.Polyline) Vector(org.apache.sis.math.Vector) Point(com.esri.core.geometry.Point)

Example 2 with Polyline

use of com.esri.core.geometry.Polyline 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);
    }
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCLineString(com.esri.core.geometry.ogc.OGCLineString) Segment(com.esri.core.geometry.Segment) OGCPolygon(com.esri.core.geometry.ogc.OGCPolygon) OGCGeometryCollection(com.esri.core.geometry.ogc.OGCGeometryCollection) OGCConcreteGeometryCollection(com.esri.core.geometry.ogc.OGCConcreteGeometryCollection) OGCPolygon(com.esri.core.geometry.ogc.OGCPolygon) Polygon(com.esri.core.geometry.Polygon) Vector(java.util.Vector) DataByteArray(org.apache.pig.data.DataByteArray) MultiPath(com.esri.core.geometry.MultiPath) DataBag(org.apache.pig.data.DataBag) ExecException(org.apache.pig.backend.executionengine.ExecException) Point(com.esri.core.geometry.Point) Point(com.esri.core.geometry.Point) IOException(java.io.IOException) ExecException(org.apache.pig.backend.executionengine.ExecException) Line(com.esri.core.geometry.Line) Polyline(com.esri.core.geometry.Polyline) Tuple(org.apache.pig.data.Tuple)

Example 3 with Polyline

use of com.esri.core.geometry.Polyline in project calcite by apache.

the class GeoFunctions method makeLine.

private static Geom makeLine(Geom... geoms) {
    final Polyline g = new Polyline();
    Point p = null;
    for (Geom geom : geoms) {
        if (geom.g() instanceof Point) {
            final Point prev = p;
            p = (Point) geom.g();
            if (prev != null) {
                final Line line = new Line();
                line.setStart(prev);
                line.setEnd(p);
                g.addSegment(line, false);
            }
        }
    }
    return new SimpleGeom(g);
}
Also used : Line(com.esri.core.geometry.Line) Polyline(com.esri.core.geometry.Polyline) Point(com.esri.core.geometry.Point)

Example 4 with Polyline

use of com.esri.core.geometry.Polyline in project sis by apache.

the class ESRI method tryMergePolylines.

/**
 * Merges a sequence of points or paths if the first instance is an implementation of this library.
 *
 * @throws ClassCastException if an element in the iterator is not a JTS geometry.
 */
@Override
final Geometry tryMergePolylines(Object next, final Iterator<?> polylines) {
    if (!(next instanceof MultiPath || next instanceof Point)) {
        return null;
    }
    final Polyline path = new Polyline();
    boolean lineTo = false;
    for (; ; next = polylines.next()) {
        if (next != null) {
            if (next instanceof Point) {
                final double x = ((Point) next).getX();
                final double y = ((Point) next).getY();
                if (Double.isNaN(x) || Double.isNaN(y)) {
                    lineTo = false;
                } else if (lineTo) {
                    path.lineTo(x, y);
                } else {
                    path.startPath(x, y);
                    lineTo = true;
                }
            } else {
                path.add((MultiPath) next, false);
                lineTo = false;
            }
        }
        if (!polylines.hasNext()) {
            // to skip this condition during the first iteration.
            break;
        }
    }
    return path;
}
Also used : MultiPath(com.esri.core.geometry.MultiPath) Polyline(com.esri.core.geometry.Polyline) Point(com.esri.core.geometry.Point)

Example 5 with Polyline

use of com.esri.core.geometry.Polyline in project sis by apache.

the class ReaderTest method verifyTrack.

/**
 * Verifies property values for the given track.
 *
 * @param  f         the track to verify.
 * @param  v11       {@code true} for GPX 1.1, or {@code false} for GPX 1.0.
 * @param  numLinks  expected number of links.
 */
@SuppressWarnings("fallthrough")
private static void verifyTrack(final AbstractFeature f, final boolean v11, final int numLinks) {
    assertEquals("name", "Track name", f.getPropertyValue("name"));
    assertEquals("cmt", "Track comment", f.getPropertyValue("cmt"));
    assertEquals("desc", "Track description", f.getPropertyValue("desc"));
    assertEquals("src", "Track source", f.getPropertyValue("src"));
    assertEquals("type", v11 ? "Track type" : null, f.getPropertyValue("type"));
    assertEquals("number", 7, f.getPropertyValue("number"));
    final List<?> links = (List<?>) f.getPropertyValue("link");
    assertEquals("links.size()", numLinks, links.size());
    switch(numLinks) {
        // Fallthrough everywhere.
        default:
        case 3:
            assertStringEquals("http://track-address3.org", links.get(2));
        case 2:
            assertStringEquals("http://track-address2.org", links.get(1));
        case 1:
            assertStringEquals("http://track-address1.org", links.get(0));
        case 0:
            break;
    }
    final List<?> segments = (List<?>) f.getPropertyValue("trkseg");
    assertEquals("segments.size()", 2, segments.size());
    final AbstractFeature seg1 = (AbstractFeature) segments.get(0);
    final AbstractFeature seg2 = (AbstractFeature) segments.get(1);
    final List<?> points = (List<?>) seg1.getPropertyValue("trkpt");
    assertEquals("points.size()", 3, points.size());
    verifyPoint((AbstractFeature) points.get(0), 0, v11);
    verifyPoint((AbstractFeature) points.get(1), 1, v11);
    verifyPoint((AbstractFeature) points.get(2), 2, v11);
    assertTrue(((Collection<?>) seg2.getPropertyValue("trkpt")).isEmpty());
    final Polyline p = (Polyline) f.getPropertyValue("sis:geometry");
    assertEquals("pointCount", 3, p.getPointCount());
    assertEquals("point(0)", new Point(15, 10), p.getPoint(0));
    assertEquals("point(1)", new Point(25, 20), p.getPoint(1));
    assertEquals("point(2)", new Point(35, 30), p.getPoint(2));
    assertEnvelopeEquals(15, 35, 10, 30, (Envelope) f.getPropertyValue("sis:envelope"));
}
Also used : Polyline(com.esri.core.geometry.Polyline) AbstractFeature(org.apache.sis.feature.AbstractFeature) List(java.util.List) Point(com.esri.core.geometry.Point)

Aggregations

Point (com.esri.core.geometry.Point)6 Polyline (com.esri.core.geometry.Polyline)6 Line (com.esri.core.geometry.Line)2 MultiPath (com.esri.core.geometry.MultiPath)2 List (java.util.List)2 Polygon (com.esri.core.geometry.Polygon)1 Segment (com.esri.core.geometry.Segment)1 OGCConcreteGeometryCollection (com.esri.core.geometry.ogc.OGCConcreteGeometryCollection)1 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)1 OGCGeometryCollection (com.esri.core.geometry.ogc.OGCGeometryCollection)1 OGCLineString (com.esri.core.geometry.ogc.OGCLineString)1 OGCPolygon (com.esri.core.geometry.ogc.OGCPolygon)1 IOException (java.io.IOException)1 Vector (java.util.Vector)1 ExecException (org.apache.pig.backend.executionengine.ExecException)1 DataBag (org.apache.pig.data.DataBag)1 DataByteArray (org.apache.pig.data.DataByteArray)1 Tuple (org.apache.pig.data.Tuple)1 AbstractFeature (org.apache.sis.feature.AbstractFeature)1 Vector (org.apache.sis.math.Vector)1