public void testPolygon() throws Exception {
    Polygon polygon = new Polygon();

    {
      LineString lineString = new LineString();
      lineString.addPoint(new Point(1, 2));
      lineString.addPoint(new Point(3, 4));
      lineString.addPoint(new Point(5, 6));
      lineString.addPoint(new Point(1, 2));

      polygon.addLinearRing(lineString);
    }

    StringWriter sw = new StringWriter();
    JSONWriter jsonWriter = new JSONWriter(sw);

    GeoJsonGeometryWriter geoWriter = new GeoJsonGeometryWriter();

    geoWriter.writeGeometry(jsonWriter, polygon);

    if (false
        == "{\"type\":\"Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[1,2]]]}"
            .equals(sw.toString())) {
      fail("Unexpected output");
    }
  }
  public void testMultiLineString() throws Exception {
    MultiLineString multiLineString = new MultiLineString();

    {
      LineString lineString = new LineString();
      lineString.addPoint(new Point(1, 2));
      lineString.addPoint(new Point(3, 4));

      multiLineString.addLineString(lineString);
    }

    {
      LineString lineString = new LineString();
      lineString.addPoint(new Point(5, 6));
      lineString.addPoint(new Point(7, 8));

      multiLineString.addLineString(lineString);
    }

    StringWriter sw = new StringWriter();
    JSONWriter jsonWriter = new JSONWriter(sw);

    GeoJsonGeometryWriter geoWriter = new GeoJsonGeometryWriter();

    geoWriter.writeGeometry(jsonWriter, multiLineString);

    if (false
        == "{\"type\":\"MultiLineString\",\"coordinates\":[[[1,2],[3,4]],[[5,6],[7,8]]]}"
            .equals(sw.toString())) {
      fail("Unexpected output");
    }
  }
  /**
   * Sets the Agent up to proceed along an Edge
   *
   * @param edge the GeomPlanarGraphEdge to traverse next
   */
  void setupEdge(GeomPlanarGraphEdge edge) {

    // clean up on old edge
    if (currentEdge != null) {
      ArrayList<AgentCopy> traffic = world.edgeTraffic.get(currentEdge);
      traffic.remove(this);
    }
    currentEdge = edge;

    // update new edge traffic
    if (world.edgeTraffic.get(currentEdge) == null) {
      world.edgeTraffic.put(currentEdge, new ArrayList<AgentCopy>());
    }
    world.edgeTraffic.get(currentEdge).add(this);

    // set up the new segment and index info
    LineString line = edge.getLine();
    segment = new LengthIndexedLine(line);
    startIndex = segment.getStartIndex();
    endIndex = segment.getEndIndex();
    linkDirection = 1;

    // check to ensure that Agent is moving in the right direction
    double distanceToStart = line.getStartPoint().distance(location.geometry),
        distanceToEnd = line.getEndPoint().distance(location.geometry);
    if (distanceToStart <= distanceToEnd) { // closer to start
      currentIndex = startIndex;
      linkDirection = 1;
    } else if (distanceToEnd < distanceToStart) { // closer to end
      currentIndex = endIndex;
      linkDirection = -1;
    }
  }
 @Test
 public void testLineString() throws SQLException {
   logger.trace("void testLineString()");
   logger.info(lng_str);
   LineString lng = new LineString(lng_str);
   logger.info(lng.toString());
 }
 private void init(Geometry geom) {
   List lines = LinearComponentExtracter.getLines(geom);
   for (Iterator i = lines.iterator(); i.hasNext(); ) {
     LineString line = (LineString) i.next();
     Coordinate[] pts = line.getCoordinates();
     addLine(pts);
   }
 }
Example #6
0
  public static String toWkt(LineString ls) {
    StringBuffer result;
    if (ls.isValid() == false) {
      result = new StringBuffer("LINESTRING EMPTY");
    } else {
      result = new StringBuffer("LINESTRING");
      result.append(createPointString(ls.getPoints()));
    }

    return result.toString();
  }
 @Test
 public void createComplexGeometryCollectionTest() throws Exception {
   Coordinate ptc = new Coordinate(10, 20);
   Point point = geometryFactory.createPoint(ptc);
   point.setSRID(4326);
   Coordinate[] lsc = new Coordinate[8];
   lsc[0] = new Coordinate(5.0d, 5.0d);
   lsc[1] = new Coordinate(6.0d, 5.0d);
   lsc[2] = new Coordinate(6.0d, 6.0d);
   lsc[3] = new Coordinate(7.0d, 6.0d);
   lsc[4] = new Coordinate(7.0d, 7.0d);
   lsc[5] = new Coordinate(8.0d, 7.0d);
   lsc[6] = new Coordinate(8.0d, 8.0d);
   lsc[7] = new Coordinate(9.0d, 9.0d);
   LineString lineString = geometryFactory.createLineString(lsc);
   lineString.setSRID(4326);
   Coordinate[] lrc = new Coordinate[10];
   lrc[0] = new Coordinate(7, 7);
   lrc[1] = new Coordinate(6, 9);
   lrc[2] = new Coordinate(6, 11);
   lrc[3] = new Coordinate(7, 12);
   lrc[4] = new Coordinate(9, 11);
   lrc[5] = new Coordinate(11, 12);
   lrc[6] = new Coordinate(13, 11);
   lrc[7] = new Coordinate(13, 9);
   lrc[8] = new Coordinate(11, 7);
   lrc[9] = new Coordinate(7, 7);
   LinearRing linearRing = geometryFactory.createLinearRing(lrc);
   linearRing.setSRID(4326);
   Geometry polygon =
       reader.read(
           "POLYGON ((35 10, 10 20, 15 40," + " 45 45, 35 10), (20 30, 35 35, 30 20, 20 30))");
   polygon.setSRID(4326);
   Geometry multiPoint = reader.read("MULTIPOINT ((10 40), (40 30), " + "(20 20), (30 10))");
   multiPoint.setSRID(4326);
   Geometry multiPolygon =
       reader.read(
           "MULTIPOLYGON (((40 40, 20 45,"
               + " 45 30, 40 40)), ((20 35, 45 20, 30 5, "
               + "10 10, 10 30, 20 35), (30 20, 20 25, 20 15, 30 20)))");
   multiPolygon.setSRID(4326);
   GeometryCollection geometryCollection =
       new GeometryCollection(
           new Geometry[] {point, linearRing, lineString, polygon, multiPoint, multiPolygon},
           geometryFactory);
   String geometryCollectionGeoJsonString = mapper.writeValueAsString(geometryCollection);
   logger.info(
       ":::::::::::::::::::::::GEO_JSON_GEOMETRY_COLLECTION : \n{}\n",
       geometryCollectionGeoJsonString);
   org.geojson.GeometryCollection p =
       mapper.readValue(geometryCollectionGeoJsonString, org.geojson.GeometryCollection.class);
   mapper.writeValue(new File("./target/GeometryCollectionComplex.json"), p);
 }
Example #8
0
 public void testFiveZeros() {
   LineString ls =
       new GeometryFactory()
           .createLineString(
               new Coordinate[] {
                 new Coordinate(0, 0),
                 new Coordinate(0, 0),
                 new Coordinate(0, 0),
                 new Coordinate(0, 0),
                 new Coordinate(0, 0)
               });
   assertTrue(ls.isClosed());
 }
Example #9
0
  public void testIsClosed() throws Exception {
    LineString l = (LineString) reader.read("LINESTRING EMPTY");
    assertTrue(l.isEmpty());
    assertTrue(!l.isClosed());

    LinearRing r = geometryFactory.createLinearRing((CoordinateSequence) null);
    assertTrue(r.isEmpty());
    assertTrue(r.isClosed());

    MultiLineString m = geometryFactory.createMultiLineString(new LineString[] {l, r});
    assertTrue(!m.isClosed());

    MultiLineString m2 = geometryFactory.createMultiLineString(new LineString[] {r});
    assertTrue(!m2.isClosed());
  }
  public void testLineString() throws Exception {
    LineString lineString = new LineString();
    lineString.addPoint(new Point(1, 2));
    lineString.addPoint(new Point(3, 4));

    StringWriter sw = new StringWriter();
    JSONWriter jsonWriter = new JSONWriter(sw);

    GeoJsonGeometryWriter geoWriter = new GeoJsonGeometryWriter();

    geoWriter.writeGeometry(jsonWriter, lineString);

    if (false == "{\"type\":\"LineString\",\"coordinates\":[[1,2],[3,4]]}".equals(sw.toString())) {
      fail("Unexpected output");
    }
  }
Example #11
0
 private void init() {
   Coordinate[] pts = parentLine.getCoordinates();
   segs = new TaggedLineSegment[pts.length - 1];
   for (int i = 0; i < pts.length - 1; i++) {
     TaggedLineSegment seg = new TaggedLineSegment(pts[i], pts[i + 1], parentLine, i);
     segs[i] = seg;
   }
 }
Example #12
0
  public Geometry densify(double segLength) {
    newCoords = new CoordinateList();

    CoordinateSequence seq = inputLine.getCoordinateSequence();

    Coordinate p0 = new Coordinate();
    Coordinate p1 = new Coordinate();
    seq.getCoordinate(0, p0);
    newCoords.add(new Coordinate(p0));

    for (int i = 0; i < seq.size() - 1; i++) {
      seq.getCoordinate(i, p0);
      seq.getCoordinate(i + 1, p1);
      densify(p0, p1, segLength);
    }
    Coordinate[] newPts = newCoords.toCoordinateArray();
    return inputLine.getFactory().createLineString(newPts);
  }
 @Test
 public void lineStringSerializerTest() throws Exception {
   Coordinate[] lsc = new Coordinate[8];
   lsc[0] = new Coordinate(5.0d, 5.0d);
   lsc[1] = new Coordinate(6.0d, 5.0d);
   lsc[2] = new Coordinate(6.0d, 6.0d);
   lsc[3] = new Coordinate(7.0d, 6.0d);
   lsc[4] = new Coordinate(7.0d, 7.0d);
   lsc[5] = new Coordinate(8.0d, 7.0d);
   lsc[6] = new Coordinate(8.0d, 8.0d);
   lsc[7] = new Coordinate(9.0d, 9.0d);
   LineString lineString = geometryFactory.createLineString(lsc);
   lineString.setSRID(4326);
   String lineStringGeoJsonString = mapper.writeValueAsString(lineString);
   logger.info(":::::::::::::::::::::::GEO_JSON_LINE_STRING : \n{}\n", lineStringGeoJsonString);
   org.geojson.LineString l =
       mapper.readValue(lineStringGeoJsonString, org.geojson.LineString.class);
   mapper.writeValue(new File("./target/LineString.json"), l);
 }
Example #14
0
  /** Add a ring to a builder */
  private void addRing(LineString r, Geobuf.Data.Geometry.Builder builder) {
    // skip last point, same as first
    builder.addLengths(r.getNumPoints() - 1);

    long x, y, prevX = 0, prevY = 0;

    // last point is same as first, skip
    for (int i = 0; i < r.getNumPoints() - 1; i++) {
      // delta code
      Coordinate coord = r.getCoordinateN(i);
      // note that roundoff errors do not accumulate
      x = (long) (coord.x * precisionMultiplier);
      y = (long) (coord.y * precisionMultiplier);
      builder.addCoords(x - prevX);
      builder.addCoords(y - prevY);
      prevX = x;
      prevY = y;
    }
  }
Example #15
0
  /** Moves the iterator ahead to the next vertex and (possibly) linear component. */
  public void next() {
    if (!hasNext()) return;

    vertexIndex++;
    if (vertexIndex >= currentLine.getNumPoints()) {
      componentIndex++;
      loadCurrentLine();
      vertexIndex = 0;
    }
  }
Example #16
0
  public void findClosestPoint(String wktA, String wktB) {
    System.out.println("-------------------------------------");
    try {
      Geometry A = wktRdr.read(wktA);
      Geometry B = wktRdr.read(wktB);
      System.out.println("Geometry A: " + A);
      System.out.println("Geometry B: " + B);
      DistanceOp distOp = new DistanceOp(A, B);

      double distance = distOp.distance();
      System.out.println("Distance = " + distance);

      Coordinate[] closestPt = distOp.nearestPoints();
      LineString closestPtLine = fact.createLineString(closestPt);
      System.out.println(
          "Closest points: " + closestPtLine + " (distance = " + closestPtLine.getLength() + ")");
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  public void testMultiPolygon() throws Exception {
    MultiPolygon multiPolygon = new MultiPolygon();

    {
      Polygon polygon = new Polygon();

      LineString linearRing = new LineString();
      linearRing.addPoint(new Point(0, 0));
      linearRing.addPoint(new Point(0, 10));
      linearRing.addPoint(new Point(10, 10));
      linearRing.addPoint(new Point(0, 0));
      polygon.addLinearRing(linearRing);

      multiPolygon.addPolygon(polygon);
    }

    {
      Polygon polygon = new Polygon();

      LineString linearRing = new LineString();
      linearRing.addPoint(new Point(100, 0));
      linearRing.addPoint(new Point(100, 10));
      linearRing.addPoint(new Point(110, 10));
      linearRing.addPoint(new Point(100, 0));
      polygon.addLinearRing(linearRing);

      multiPolygon.addPolygon(polygon);
    }

    StringWriter sw = new StringWriter();
    JSONWriter jsonWriter = new JSONWriter(sw);

    GeoJsonGeometryWriter geoWriter = new GeoJsonGeometryWriter();

    geoWriter.writeGeometry(jsonWriter, multiPolygon);

    if (false
        == "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0,0],[0,10],[10,10],[0,0]]],[[[100,0],[100,10],[110,10],[100,0]]]]}"
            .equals(sw.toString())) {
      fail("Unexpected output");
    }
  }
Example #18
0
 /**
  * Gets the first {@link Coordinate} of the current segment. (the coordinate of the current
  * vertex).
  *
  * @return a {@link Coordinate}
  */
 public Coordinate getSegmentStart() {
   return currentLine.getCoordinateN(vertexIndex);
 }
Example #19
0
 public LinearRing asLinearRing() {
   return parentLine.getFactory().createLinearRing(extractCoordinates(resultSegs));
 }
Example #20
0
 /**
  * Gets the second {@link Coordinate} of the current segment. (the coordinate of the next vertex).
  * If the iterator is at the end of a line, <code>null</code> is returned.
  *
  * @return a {@link Coordinate} or <code>null</code>
  */
 public Coordinate getSegmentEnd() {
   if (vertexIndex < getLine().getNumPoints() - 1)
     return currentLine.getCoordinateN(vertexIndex + 1);
   return null;
 }
Example #21
0
 public Coordinate[] getParentCoordinates() {
   return parentLine.getCoordinates();
 }
Example #22
0
 /**
  * Transforms a {@link LineString} geometry.
  *
  * @param geom
  * @param parent
  * @return
  */
 protected Geometry transformLineString(LineString geom, Geometry parent) {
   // should check for 1-point sequences and downgrade them to points
   return factory.createLineString(transformCoordinates(geom.getCoordinateSequence(), geom));
 }
Example #23
0
 public void testEquals2() throws Exception {
   LineString l1 = (LineString) reader.read("LINESTRING(1.111 2.222, 3.333 4.444)");
   LineString l2 = (LineString) reader.read("LINESTRING(3.333 4.444, 1.111 2.222)");
   assertTrue(l1.equals(l2));
 }
Example #24
0
 public void testIsCoordinate() throws Exception {
   LineString l = (LineString) reader.read("LINESTRING (0 0, 10 10, 10 0)");
   assertTrue(l.isCoordinate(new Coordinate(0, 0)));
   assertTrue(!l.isCoordinate(new Coordinate(5, 0)));
 }
Example #25
0
 public void testIsSimple() throws Exception {
   LineString l1 = (LineString) reader.read("LINESTRING (0 0, 10 10, 10 0, 0 10, 0 0)");
   assertTrue(!l1.isSimple());
   LineString l2 = (LineString) reader.read("LINESTRING (0 0, 10 10, 10 0, 0 10)");
   assertTrue(!l2.isSimple());
 }
Example #26
0
 /**
  * Tests whether there are any vertices left to iterator over. Specifically, hasNext() return
  * <tt>true</tt> if the current state of the iterator represents a valid location on the linear
  * geometry.
  *
  * @return <code>true</code> if there are more vertices to scan
  */
 public boolean hasNext() {
   if (componentIndex >= numLines) return false;
   if (componentIndex == numLines - 1 && vertexIndex >= currentLine.getNumPoints()) return false;
   return true;
 }
Example #27
0
 public void testGetGeometryType() throws Exception {
   LineString l = (LineString) reader.read("LINESTRING EMPTY");
   assertEquals("LineString", l.getGeometryType());
 }
Example #28
0
 public void testGetCoordinates() throws Exception {
   LineString l = (LineString) reader.read("LINESTRING(1.111 2.222, 5.555 6.666, 3.333 4.444)");
   Coordinate[] coordinates = l.getCoordinates();
   assertEquals(new Coordinate(5.555, 6.666), coordinates[1]);
 }
Example #29
0
 public void testEquals7() throws Exception {
   LineString l1 = (LineString) reader.read("LINESTRING(1.111 2.222, 5.555 6.666, 3.333 4.444)");
   LineString l2 = (LineString) reader.read("LINESTRING(1.111 2.222, 3.333 4.444, 5.555 6.666)");
   assertTrue(!l1.equals(l2));
 }
Example #30
0
 /**
  * Checks whether the iterator cursor is pointing to the
  * endpoint of a component {@link LineString}.
  *
  * @return <code>true</true> if the iterator is at an endpoint
  */
 public boolean isEndOfLine() {
   if (componentIndex >= numLines) return false;
   // LineString currentLine = (LineString) linear.getGeometryN(componentIndex);
   if (vertexIndex < currentLine.getNumPoints() - 1) return false;
   return true;
 }