use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.
the class CoordinatesListUtil method containsWithinTolerance.
/**
* <p>
* Check within a given tolerance that the LINESTRING defined by points2 is
* contained within the points1.
* </p>
* <p>
* The algorithm is as follows:
* <ol>
* <li>Find all coordinates from points2 that are within the tolerance from
* the line segments of points1.</li>
* <li>Find all coordinates from points1 that are within the tolerance from
* the line segments of points2.</li>
* <li>Split all the line sgements of points1 that were matched in step 1.</li>
* <li>Split all the line sgements of points2 that were matched in step 2.</li>
* <li>Line is contained if all line segments from point2 have matching lines
* in points1.</li>
* </ol>
*
* @param points1
* @param points2
* @param tolerance
* @return
*/
public static boolean containsWithinTolerance(final LineString points1, final LineString points2, final double tolerance) {
final LineStringGraph graph1 = new LineStringGraph(points1);
final LineStringGraph graph2 = new LineStringGraph(points2);
graph1.forEachNode((node) -> movePointsWithinTolerance(null, graph2, tolerance, node));
graph1.forEachNode((node) -> movePointsWithinTolerance(null, graph1, tolerance, node));
final Map<Edge<LineSegment>, List<Node<LineSegment>>> pointsOnEdge1 = graph1.getPointsOnEdges(graph2, tolerance);
final Map<Edge<LineSegment>, List<Node<LineSegment>>> pointsOnEdge2 = graph2.getPointsOnEdges(graph1, tolerance);
graph1.splitEdges(pointsOnEdge1);
graph2.splitEdges(pointsOnEdge2);
for (final Edge<LineSegment> edge : graph2.getEdges()) {
final Node<LineSegment> fromNode = edge.getFromNode();
final Node<LineSegment> toNode = edge.getToNode();
if (!graph1.hasEdgeBetween(fromNode, toNode)) {
return false;
}
}
return true;
}
use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.
the class CoordinatesListUtil method intersection.
public static List<LineString> intersection(final GeometryFactory geometryFactory, final LineString points1, final LineString points2, final double maxDistance) {
final LineStringGraph graph1 = new LineStringGraph(points1);
graph1.setPrecisionModel(geometryFactory);
final LineStringGraph graph2 = new LineStringGraph(points2);
graph2.setPrecisionModel(geometryFactory);
final Map<Point, Point> movedNodes = new HashMap<>();
graph1.forEachNode((node) -> movePointsWithinTolerance(movedNodes, graph2, maxDistance, node));
graph2.forEachNode((node) -> movePointsWithinTolerance(movedNodes, graph1, maxDistance, node));
final Map<Edge<LineSegment>, List<Node<LineSegment>>> pointsOnEdge1 = graph1.getPointsOnEdges(graph2, maxDistance);
final Map<Edge<LineSegment>, List<Node<LineSegment>>> pointsOnEdge2 = graph2.getPointsOnEdges(graph1, maxDistance);
graph1.splitEdges(pointsOnEdge1);
graph2.splitEdges(pointsOnEdge2);
Point startPoint = points1.getPoint(0);
if (movedNodes.containsKey(startPoint)) {
startPoint = movedNodes.get(startPoint);
}
Point endPoint = points1.getPoint(points1.getVertexCount() - 1);
if (movedNodes.containsKey(endPoint)) {
endPoint = movedNodes.get(endPoint);
}
final List<LineString> intersections = new ArrayList<>();
final List<Point> currentCoordinates = new ArrayList<>();
Node<LineSegment> previousNode = graph1.getNode(startPoint);
do {
final List<Edge<LineSegment>> outEdges = previousNode.getOutEdges();
if (outEdges.isEmpty()) {
previousNode = null;
} else if (outEdges.size() > 1) {
throw new IllegalArgumentException("Cannot handle overlaps\n" + points1 + "\n " + points2);
} else {
final Edge<LineSegment> edge = outEdges.get(0);
final LineSegment line = edge.getObject();
final Node<LineSegment> nextNode = edge.getToNode();
if (graph2.hasEdgeBetween(previousNode, nextNode)) {
if (currentCoordinates.size() == 0) {
currentCoordinates.add(line.getPoint(0));
}
currentCoordinates.add(line.getPoint(1));
} else {
if (currentCoordinates.size() > 0) {
final LineString points = new LineStringDouble(points1.getAxisCount(), currentCoordinates);
intersections.add(points);
currentCoordinates.clear();
}
}
previousNode = nextNode;
}
} while (previousNode != null && !endPoint.equals(2, startPoint));
if (currentCoordinates.size() > 0) {
final LineString points = new LineStringDouble(points1.getAxisCount(), currentCoordinates);
intersections.add(points);
}
return intersections;
}
use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.
the class LineMatchGraph method getEdgesWithoutMatch.
private Set<Edge<LineSegmentMatch>> getEdgesWithoutMatch(final LineString line, final int index) {
final Set<Edge<LineSegmentMatch>> edges = new LinkedHashSet<>();
final LineString coordinatesList = line;
final Point coordinate0 = coordinatesList.getPoint(0);
Point previousCoordinate = coordinate0;
for (int i = 1; i < coordinatesList.getVertexCount(); i++) {
final Point coordinate = coordinatesList.getPoint(i);
if (previousCoordinate.distancePoint(coordinate) > 0) {
final Edge<LineSegmentMatch> edge = getEdge(previousCoordinate, coordinate);
if (edge != null) {
final LineSegmentMatch lineSegmentMatch = edge.getObject();
if (!lineSegmentMatch.hasMatches(index)) {
edges.add(edge);
}
}
}
previousCoordinate = coordinate;
}
return edges;
}
use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.
the class LineMatchGraph method getDuplicateMatchLength.
public double getDuplicateMatchLength(final Node<LineSegmentMatch> node, final boolean direction, final int index1, final int index2) {
List<Edge<LineSegmentMatch>> edges;
if (direction) {
edges = node.getOutEdges();
} else {
edges = node.getInEdges();
}
for (final Edge<LineSegmentMatch> edge : edges) {
final LineSegmentMatch lineSegmentMatch = edge.getObject();
if (lineSegmentMatch.getMatchCount(index1) > 2) {
if (lineSegmentMatch.hasMatches(index1, index2)) {
final LineSegment segment = lineSegmentMatch.getSegment(index2);
final double length = segment.getLength();
final Node<LineSegmentMatch> nextNode = edge.getOppositeNode(node);
return length + getDuplicateMatchLength(nextNode, direction, index1, index2);
}
}
}
return 0;
}
use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.
the class LineMatchGraph method integrateLine.
/**
* Integrate the line, splitting any edges which the nodes from this line are
* on the other edges.
*
* @param line
* @param index
*/
private void integrateLine(final LineString line, final int index) {
final Set<Edge<LineSegmentMatch>> edgesToProcess = getEdgesWithoutMatch(line, index);
if (!edgesToProcess.isEmpty()) {
for (final Edge<LineSegmentMatch> edge : edgesToProcess) {
if (!edge.isRemoved()) {
final LineSegmentMatch lineSegmentMatch = edge.getObject();
final LineSegment segment = lineSegmentMatch.getSegment();
if (!lineSegmentMatch.hasMatches(index)) {
final List<Edge<LineSegmentMatch>> matchEdges = BoundingBoxIntersectsEdgeVisitor.getEdges(this, edge, this.tolerance);
if (!matchEdges.isEmpty()) {
final boolean allowSplit = edge.getLength() >= 2 * this.tolerance;
final Set<Node<LineSegmentMatch>> splitNodes = new TreeSet<>(new NodeDistanceComparator<>(edge.getFromNode()));
final Node<LineSegmentMatch> lineStart = edge.getFromNode();
final Node<LineSegmentMatch> lineEnd = edge.getToNode();
for (final ListIterator<Edge<LineSegmentMatch>> iterator = matchEdges.listIterator(); iterator.hasNext(); ) {
final Edge<LineSegmentMatch> matchEdge = iterator.next();
iterator.remove();
final LineSegmentMatch matchLineSegmentMatch = matchEdge.getObject();
if (!matchLineSegmentMatch.hasSegment(index) && matchLineSegmentMatch.hasOtherSegment(index)) {
final Node<LineSegmentMatch> line2Start = matchEdge.getFromNode();
final Node<LineSegmentMatch> line2End = matchEdge.getToNode();
final Set<Node<LineSegmentMatch>> matchSplitNodes = new TreeSet<>(new NodeDistanceComparator<>(line2Start));
final LineSegment matchSegment = matchLineSegmentMatch.getSegment();
if (matchEdge.getLength() >= 2 * this.tolerance) {
if (matchSegment.isPointOnLineMiddle(lineStart, this.tolerance)) {
matchSplitNodes.add(lineStart);
}
if (matchSegment.isPointOnLineMiddle(lineEnd, this.tolerance)) {
matchSplitNodes.add(lineEnd);
}
}
if (!matchSplitNodes.isEmpty()) {
final List<Edge<LineSegmentMatch>> splitEdges = splitEdge(matchEdge, matchSplitNodes);
for (final Edge<LineSegmentMatch> splitEdge : splitEdges) {
iterator.add(splitEdge);
}
} else if (allowSplit) {
if (segment.isPointOnLineMiddle(line2Start, this.tolerance)) {
splitNodes.add(line2Start);
}
if (segment.isPointOnLineMiddle(line2End, this.tolerance)) {
splitNodes.add(line2End);
}
}
}
}
if (!splitNodes.isEmpty() && allowSplit) {
splitEdge(edge, splitNodes);
}
}
}
}
}
}
}
Aggregations