/**
   * Tests whether this PreparedPolygon intersects a given geometry.
   *
   * @param geom the test geometry
   * @return true if the test geometry intersects
   */
  public boolean intersects(Geometry geom) {
    /**
     * Do point-in-poly tests first, since they are cheaper and may result in a quick positive
     * result.
     *
     * <p>If a point of any test components lie in target, result is true
     */
    boolean isInPrepGeomArea = isAnyTestComponentInTarget(geom);
    if (isInPrepGeomArea) return true;

    /** If any segments intersect, result is true */
    List lineSegStr = SegmentStringUtil.extractSegmentStrings(geom);
    // only request intersection finder if there are segments (ie NOT for point inputs)
    if (lineSegStr.size() > 0) {
      boolean segsIntersect = prepPoly.getIntersectionFinder().intersects(lineSegStr);
      if (segsIntersect) return true;
    }

    /**
     * If the test has dimension = 2 as well, it is necessary to test for proper inclusion of the
     * target. Since no segments intersect, it is sufficient to test representative points.
     */
    if (geom.getDimension() == 2) {
      // TODO: generalize this to handle GeometryCollections
      boolean isPrepGeomInArea =
          isAnyTargetComponentInAreaTest(geom, prepPoly.getRepresentativePoints());
      if (isPrepGeomInArea) return true;
    }

    return false;
  }
 private void addLine(Coordinate[] pts) {
   SegmentString segStr = new BasicSegmentString(pts, null);
   List segChains = MonotoneChainBuilder.getChains(segStr.getCoordinates(), segStr);
   for (Iterator i = segChains.iterator(); i.hasNext(); ) {
     MonotoneChain mc = (MonotoneChain) i.next();
     index.insert(mc.getEnvelope(), mc);
   }
 }
 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);
   }
 }
 /**
  * Tests whether any component of the test Geometry intersects the interior of the target
  * geometry. Handles test geometries with both linear and point components.
  *
  * @param geom a geometry to test
  * @return true if any component of the argument intersects the prepared area geometry interior
  */
 protected boolean isAnyTestComponentInTargetInterior(Geometry testGeom) {
   List coords = ComponentCoordinateExtracter.getCoordinates(testGeom);
   for (Iterator i = coords.iterator(); i.hasNext(); ) {
     Coordinate p = (Coordinate) i.next();
     int loc = targetPointLocator.locate(p);
     if (loc == Location.INTERIOR) return true;
   }
   return false;
 }
Esempio n. 5
0
 private static Geometry convertSegStrings(Iterator it) {
   GeometryFactory fact = new GeometryFactory();
   List lines = new ArrayList();
   while (it.hasNext()) {
     SegmentString ss = (SegmentString) it.next();
     LineString line = fact.createLineString(ss.getCoordinates());
     lines.add(line);
   }
   return fact.buildGeometry(lines);
 }
Esempio n. 6
0
 /**
  * Computes the {@link Polygon} formed by this ring and any contained holes.
  *
  * @return the {@link Polygon} formed by this ring and its holes.
  */
 public Polygon getPolygon() {
   LinearRing[] holeLR = null;
   if (holes != null) {
     holeLR = new LinearRing[holes.size()];
     for (int i = 0; i < holes.size(); i++) {
       holeLR[i] = (LinearRing) holes.get(i);
     }
   }
   Polygon poly = factory.createPolygon(ring, holeLR);
   return poly;
 }
 /**
  * Tests whether any representative point of the test Geometry intersects the target geometry.
  * Only handles test geometries which are Puntal (dimension 0)
  *
  * @param geom a Puntal geometry to test
  * @return true if any point of the argument intersects the prepared geometry
  */
 protected boolean isAnyTestPointInTarget(Geometry testGeom) {
   /**
    * This could be optimized by using the segment index on the lineal target. However, it seems
    * like the L/P case would be pretty rare in practice.
    */
   PointLocator locator = new PointLocator();
   List coords = ComponentCoordinateExtracter.getCoordinates(testGeom);
   for (Iterator i = coords.iterator(); i.hasNext(); ) {
     Coordinate p = (Coordinate) i.next();
     if (locator.intersects(p, prepLine.getGeometry())) return true;
   }
   return false;
 }
Esempio n. 8
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;
  }
Esempio n. 9
0
  public void findEdge(List dirEdgeList) {
    /**
     * Check all forward DirectedEdges only. This is still general, because each edge has a forward
     * DirectedEdge.
     */
    for (Iterator i = dirEdgeList.iterator(); i.hasNext(); ) {
      DirectedEdge de = (DirectedEdge) i.next();
      if (!de.isForward()) continue;
      checkForRightmostCoordinate(de);
    }

    /**
     * If the rightmost point is a node, we need to identify which of the incident edges is
     * rightmost.
     */
    Assert.isTrue(
        minIndex != 0 || minCoord.equals(minDe.getCoordinate()),
        "inconsistency in rightmost processing");
    if (minIndex == 0) {
      findRightmostEdgeAtNode();
    } else {
      findRightmostEdgeAtVertex();
    }
    /** now check that the extreme side is the R side. If not, use the sym instead. */
    orientedDe = minDe;
    int rightmostSide = getRightmostSide(minDe, minIndex);
    if (rightmostSide == Position.LEFT) {
      orientedDe = minDe.getSym();
    }
  }
Esempio n. 10
0
  public Geometry buffer(Geometry g, double distance) {
    PrecisionModel precisionModel = workingPrecisionModel;
    if (precisionModel == null) precisionModel = g.getPrecisionModel();

    // factory must be the same as the one used by the input
    geomFact = g.getFactory();

    OffsetCurveBuilder curveBuilder = new OffsetCurveBuilder(precisionModel, bufParams);

    OffsetCurveSetBuilder curveSetBuilder = new OffsetCurveSetBuilder(g, distance, curveBuilder);

    List bufferSegStrList = curveSetBuilder.getCurves();

    // short-circuit test
    if (bufferSegStrList.size() <= 0) {
      return createEmptyResultGeometry();
    }

    // BufferDebug.runCount++;
    // String filename = "run" + BufferDebug.runCount + "_curves";
    // System.out.println("saving " + filename);
    // BufferDebug.saveEdges(bufferEdgeList, filename);
    // DEBUGGING ONLY
    // WKTWriter wktWriter = new WKTWriter();
    // Debug.println("Rings: " + wktWriter.write(convertSegStrings(bufferSegStrList.iterator())));
    // wktWriter.setMaxCoordinatesPerLine(10);
    // System.out.println(wktWriter.writeFormatted(convertSegStrings(bufferSegStrList.iterator())));

    computeNodedEdges(bufferSegStrList, precisionModel);
    graph = new PlanarGraph(new OverlayNodeFactory());
    graph.addEdges(edgeList.getEdges());

    List subgraphList = createSubgraphs(graph);
    PolygonBuilder polyBuilder = new PolygonBuilder(geomFact);
    buildSubgraphs(subgraphList, polyBuilder);
    List resultPolyList = polyBuilder.getPolygons();

    // just in case...
    if (resultPolyList.size() <= 0) {
      return createEmptyResultGeometry();
    }

    Geometry resultGeom = geomFact.buildGeometry(resultPolyList);
    return resultGeom;
  }
Esempio n. 11
0
 private List createSubgraphs(PlanarGraph graph) {
   List subgraphList = new ArrayList();
   for (Iterator i = graph.getNodes().iterator(); i.hasNext(); ) {
     Node node = (Node) i.next();
     if (!node.isVisited()) {
       BufferSubgraph subgraph = new BufferSubgraph();
       subgraph.create(node);
       subgraphList.add(subgraph);
     }
   }
   /**
    * Sort the subgraphs in descending order of their rightmost coordinate. This ensures that when
    * the Polygons for the subgraphs are built, subgraphs for shells will have been built before
    * the subgraphs for any holes they contain.
    */
   Collections.sort(subgraphList, Collections.reverseOrder());
   return subgraphList;
 }
 private void countSegs(
     RayCrossingCounter rcc, Envelope rayEnv, List monoChains, MCSegmentCounter mcSegCounter) {
   for (Iterator i = monoChains.iterator(); i.hasNext(); ) {
     MonotoneChain mc = (MonotoneChain) i.next();
     mc.select(rayEnv, mcSegCounter);
     // short-circuit if possible
     if (rcc.isOnSegment()) return;
   }
 }
 /**
  * Tests whether any component of the target geometry intersects the test geometry (which must be
  * an areal geometry)
  *
  * @param geom the test geometry
  * @param repPts the representative points of the target geometry
  * @return true if any component intersects the areal test geometry
  */
 protected boolean isAnyTargetComponentInAreaTest(Geometry testGeom, List targetRepPts) {
   PointOnGeometryLocator piaLoc = new SimplePointInAreaLocator(testGeom);
   for (Iterator i = targetRepPts.iterator(); i.hasNext(); ) {
     Coordinate p = (Coordinate) i.next();
     int loc = piaLoc.locate(p);
     if (loc != Location.EXTERIOR) return true;
   }
   return false;
 }
Esempio n. 14
0
 /**
  * Computes the list of coordinates which are contained in this ring. The coordinatea are computed
  * once only and cached.
  *
  * @return an array of the {@link Coordinate}s in this ring
  */
 private Coordinate[] getCoordinates() {
   if (ringPts == null) {
     CoordinateList coordList = new CoordinateList();
     for (Iterator i = deList.iterator(); i.hasNext(); ) {
       DirectedEdge de = (DirectedEdge) i.next();
       PolygonizeEdge edge = (PolygonizeEdge) de.getEdge();
       addEdge(edge.getLine().getCoordinates(), de.getEdgeDirection(), coordList);
     }
     ringPts = coordList.toCoordinateArray();
   }
   return ringPts;
 }
Esempio n. 15
0
 /**
  * Completes the building of the input subgraphs by depth-labelling them, and adds them to the
  * PolygonBuilder. The subgraph list must be sorted in rightmost-coordinate order.
  *
  * @param subgraphList the subgraphs to build
  * @param polyBuilder the PolygonBuilder which will build the final polygons
  */
 private void buildSubgraphs(List subgraphList, PolygonBuilder polyBuilder) {
   List processedGraphs = new ArrayList();
   for (Iterator i = subgraphList.iterator(); i.hasNext(); ) {
     BufferSubgraph subgraph = (BufferSubgraph) i.next();
     Coordinate p = subgraph.getRightmostCoordinate();
     //      int outsideDepth = 0;
     //      if (polyBuilder.containsPoint(p))
     //        outsideDepth = 1;
     SubgraphDepthLocater locater = new SubgraphDepthLocater(processedGraphs);
     int outsideDepth = locater.getDepth(p);
     //      try {
     subgraph.computeDepth(outsideDepth);
     //      }
     //      catch (RuntimeException ex) {
     //        // debugging only
     //        //subgraph.saveDirEdges();
     //        throw ex;
     //      }
     subgraph.findResultEdges();
     processedGraphs.add(subgraph);
     polyBuilder.add(subgraph.getDirectedEdges(), subgraph.getNodes());
   }
 }
Esempio n. 16
0
 /**
  * Adds a {@link DirectedEdge} which is known to form part of this ring.
  *
  * @param de the {@link DirectedEdge} to add.
  */
 public void add(DirectedEdge de) {
   deList.add(de);
 }
Esempio n. 17
0
 /**
  * Adds a hole to the polygon formed by this ring.
  *
  * @param hole the {@link LinearRing} forming the hole.
  */
 public void addHole(LinearRing hole) {
   if (holes == null) holes = new ArrayList();
   holes.add(hole);
 }