Class PointBuilder

  1 /*
  2  * Copyright (c) 2016 Vivid Solutions.
  3  *
  4  * All rights reserved. This program and the accompanying materials
  5  * are made available under the terms of the Eclipse Public License 2.0
  6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  8  * and the Eclipse Distribution License is available at
  9  *
 10  * http://www.eclipse.org/org/documents/edl-v10.php.
 11  */
 12 package org.locationtech.jts.operation.overlay;
 13  
 14 import java.util.ArrayList;
 15 import java.util.Iterator;
 16 import java.util.List;
 17  
 18 import org.locationtech.jts.algorithm.PointLocator;
 19 import org.locationtech.jts.geom.Coordinate;
 20 import org.locationtech.jts.geom.GeometryFactory;
 21 import org.locationtech.jts.geom.Point;
 22 import org.locationtech.jts.geomgraph.Label;
 23 import org.locationtech.jts.geomgraph.Node;
 24  
 25 /**
 26  * Constructs {@link Point}s from the nodes of an overlay graph.
 27  * @version 1.7
 28  */
 29 public class PointBuilder {
 30   private OverlayOp op;
 31   private GeometryFactory geometryFactory;
 32   private List resultPointList = new ArrayList();
 33  
 34   public PointBuilder(OverlayOp op, GeometryFactory geometryFactory, PointLocator ptLocator) {
 35     this.op = op;
 36     this.geometryFactory = geometryFactory;
 37     // ptLocator is never used in this class
 38   }
 39  
 40   /**
 41    * Computes the Point geometries which will appear in the result,
 42    * given the specified overlay operation.
 43    *
 44    * @return a list of the Points objects in the result
 45    */
 46   public List build(int opCode)
 47   {
 48     extractNonCoveredResultNodes(opCode);
 49     /**
 50      * It can happen that connected result nodes are still covered by
 51      * result geometries, so must perform this filter.
 52      * (For instance, this can happen during topology collapse).
 53      */
 54     return resultPointList;
 55   }
 56  
 57   /**
 58    * Determines nodes which are in the result, and creates {@link Point}s for them.
 59    *
 60    * This method determines nodes which are candidates for the result via their
 61    * labelling and their graph topology.
 62    *
 63    * @param opCode the overlay operation
 64    */
 65   private void extractNonCoveredResultNodes(int opCode)
 66   {
 67     // testing only
 68     //if (true) return resultNodeList;
 69  
 70     for (Iterator nodeit = op.getGraph().getNodes().iterator(); nodeit.hasNext(); ) {
 71       Node n = (Node) nodeit.next();
 72  
 73       // filter out nodes which are known to be in the result
 74       if (n.isInResult())
 75         continue;
 76       // if an incident edge is in the result, then the node coordinate is included already
 77       if (n.isIncidentEdgeInResult())
 78         continue;
 79       if (n.getEdges().getDegree() == 0 || opCode == OverlayOp.INTERSECTION) {
 80  
 81         /**
 82          * For nodes on edges, only INTERSECTION can result in edge nodes being included even
 83          * if none of their incident edges are included
 84          */
 85           Label label = n.getLabel();
 86           if (OverlayOp.isResultOfOp(label, opCode)) {
 87             filterCoveredNodeToPoint(n);
 88           }
 89       }
 90     }
 91     //System.out.println("connectedResultNodes collected = " + connectedResultNodes.size());
 92   }
 93  
 94   /**
 95    * Converts non-covered nodes to Point objects and adds them to the result.
 96    *
 97    * A node is covered if it is contained in another element Geometry
 98    * with higher dimension (e.g. a node point might be contained in a polygon,
 99    * in which case the point can be eliminated from the result).
100    *
101    * @param n the node to test
102    */
103   private void filterCoveredNodeToPoint(Node n)
104   {
105     Coordinate coord = n.getCoordinate();
106     if (! op.isCoveredByLA(coord)) {
107       Point pt = geometryFactory.createPoint(coord);
108       resultPointList.add(pt);
109     }
110   }
111 }
112