Example #1
0
  private Polygon editPolygon(Polygon polygon, GeometryEditorOperation operation) {
    Polygon newPolygon = (Polygon) operation.edit(polygon, factory);
    // create one if needed
    if (newPolygon == null) newPolygon = factory.createPolygon((CoordinateSequence) null);
    if (newPolygon.isEmpty()) {
      // RemoveSelectedPlugIn relies on this behaviour. [Jon Aquino]
      return newPolygon;
    }

    LinearRing shell = (LinearRing) edit(newPolygon.getExteriorRing(), operation);
    if (shell == null || shell.isEmpty()) {
      // RemoveSelectedPlugIn relies on this behaviour. [Jon Aquino]
      return factory.createPolygon(null, null);
    }

    ArrayList holes = new ArrayList();
    for (int i = 0; i < newPolygon.getNumInteriorRing(); i++) {
      LinearRing hole = (LinearRing) edit(newPolygon.getInteriorRingN(i), operation);
      if (hole == null || hole.isEmpty()) {
        continue;
      }
      holes.add(hole);
    }

    return factory.createPolygon(shell, (LinearRing[]) holes.toArray(new LinearRing[] {}));
  }
 @Test
 public void testLinearRing() throws SQLException {
   logger.trace("void testLinearRing()");
   logger.info(lr_str);
   LinearRing lr = new LinearRing(lr_str);
   logger.info(lr.toString());
 }
 @Override
 public void visit(LinearRing geom) {
   writeByteOrder(output);
   DimensionalFlag dimension = DimensionalFlag.valueOf(geom.is3D(), geom.isMeasured());
   writeTypeCodeAndSrid(geom, dimension, output);
   writeRing(geom);
 }
Example #4
0
  /** reverses the order of points in lr (is CW -> CCW or CCW->CW) */
  LinearRing reverseRing(LinearRing lr) {
    int numPoints = lr.getNumPoints();
    Coordinate[] newCoords = new Coordinate[numPoints];

    for (int t = 0; t < numPoints; t++) {
      newCoords[t] = lr.getCoordinateN(numPoints - t - 1);
    }

    return new LinearRing(newCoords, new PrecisionModel(), 0);
  }
 @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 #6
0
  /**
   * Find the innermost enclosing shell EdgeRing containing the argument EdgeRing, if any. The
   * innermost enclosing ring is the <i>smallest</i> enclosing ring. The algorithm used depends on
   * the fact that: <br>
   * ring A contains ring B iff envelope(ring A) contains envelope(ring B) <br>
   * This routine is only safe to use if the chosen point of the hole is known to be properly
   * contained in a shell (which is guaranteed to be the case if the hole does not touch its shell)
   *
   * @return containing EdgeRing, if there is one or null if no containing EdgeRing is found
   */
  public static EdgeRing findEdgeRingContaining(EdgeRing testEr, List shellList) {
    LinearRing testRing = testEr.getRing();
    Envelope testEnv = testRing.getEnvelopeInternal();
    Coordinate testPt = testRing.getCoordinateN(0);

    EdgeRing minShell = null;
    Envelope minShellEnv = null;
    for (Iterator it = shellList.iterator(); it.hasNext(); ) {
      EdgeRing tryShell = (EdgeRing) it.next();
      LinearRing tryShellRing = tryShell.getRing();
      Envelope tryShellEnv = tryShellRing.getEnvelopeInternal();
      // the hole envelope cannot equal the shell envelope
      // (also guards against testing rings against themselves)
      if (tryShellEnv.equals(testEnv)) continue;
      // hole must be contained in shell
      if (!tryShellEnv.contains(testEnv)) continue;

      testPt =
          CoordinateArrays.ptNotInList(testRing.getCoordinates(), tryShellRing.getCoordinates());
      boolean isContained = false;
      if (CGAlgorithms.isPointInRing(testPt, tryShellRing.getCoordinates())) isContained = true;

      // check if this new containing ring is smaller than the current minimum ring
      if (isContained) {
        if (minShell == null || minShellEnv.contains(tryShellEnv)) {
          minShell = tryShell;
          minShellEnv = minShell.getRing().getEnvelopeInternal();
        }
      }
    }
    return minShell;
  }
Example #7
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());
  }
Example #8
0
 /**
  * Transforms a LinearRing. The transformation of a LinearRing may result in a coordinate sequence
  * which does not form a structurally valid ring (i.e. a degnerate ring of 3 or fewer points). In
  * this case a LineString is returned. Subclasses may wish to override this method and check for
  * this situation (e.g. a subclass may choose to eliminate degenerate linear rings)
  *
  * @param geom the ring to simplify
  * @param parent the parent geometry
  * @return a LinearRing if the transformation resulted in a structurally valid ring
  * @return a LineString if the transformation caused the LinearRing to collapse to 3 or fewer
  *     points
  */
 protected Geometry transformLinearRing(LinearRing geom, Geometry parent) {
   CoordinateSequence seq = transformCoordinates(geom.getCoordinateSequence(), geom);
   if (seq == null) return factory.createLinearRing((CoordinateSequence) null);
   int seqSize = seq.size();
   // ensure a valid LinearRing
   if (seqSize > 0 && seqSize < 4 && !preserveType) return factory.createLineString(seq);
   return factory.createLinearRing(seq);
 }
 @Test
 public void linearRingSerializerTest() throws Exception {
   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);
   String linearRingGeoJsonString = mapper.writeValueAsString(linearRing);
   logger.info(":::::::::::::::::::::::GEO_JSON_LINE_STRING : \n{}\n", linearRingGeoJsonString);
   org.geojson.LineString l =
       mapper.readValue(linearRingGeoJsonString, org.geojson.LineString.class);
   mapper.writeValue(new File("./target/LinearRing.json"), l);
 }
 private void writeRing(LinearRing geom) {
   output.putUInt(geom.getNumPoints());
   writePoints(geom.getPoints(), geom.getCoordinateDimension(), output);
 }
Example #11
0
 protected void writeRing(LinearRing<P> geom) {
   output.putUInt(geom.getNumPositions());
   writePoints(geom.getPositions(), geom.getCoordinateDimension(), output);
 }
Example #12
0
 /**
  * Tests if the {@link LinearRing} ring formed by this edge ring is topologically valid.
  *
  * @return true if the ring is valid
  */
 public boolean isValid() {
   getCoordinates();
   if (ringPts.length <= 3) return false;
   getRing();
   return ring.isValid();
 }
Example #13
0
 /**
  * Tests whether this ring is a hole. Due to the way the edges in the polyongization graph are
  * linked, a ring is a hole if it is oriented counter-clockwise.
  *
  * @return <code>true</code> if this ring is a hole
  */
 public boolean isHole() {
   LinearRing ring = getRing();
   return CGAlgorithms.isCCW(ring.getCoordinates());
 }