Search in sources :

Example 1 with LineStringGraph

use of com.revolsys.geometry.graph.linestring.LineStringGraph 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;
}
Also used : LineStringGraph(com.revolsys.geometry.graph.linestring.LineStringGraph) ArrayList(java.util.ArrayList) List(java.util.List) Edge(com.revolsys.geometry.graph.Edge) LineSegment(com.revolsys.geometry.model.segment.LineSegment)

Example 2 with LineStringGraph

use of com.revolsys.geometry.graph.linestring.LineStringGraph 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;
}
Also used : HashMap(java.util.HashMap) LineStringGraph(com.revolsys.geometry.graph.linestring.LineStringGraph) Node(com.revolsys.geometry.graph.Node) ArrayList(java.util.ArrayList) Point(com.revolsys.geometry.model.Point) LineString(com.revolsys.geometry.model.LineString) ArrayList(java.util.ArrayList) List(java.util.List) Edge(com.revolsys.geometry.graph.Edge) LineSegment(com.revolsys.geometry.model.segment.LineSegment) LineStringDouble(com.revolsys.geometry.model.impl.LineStringDouble)

Example 3 with LineStringGraph

use of com.revolsys.geometry.graph.linestring.LineStringGraph in project com.revolsys.open by revolsys.

the class LineStringGraphTest method testIntersectionTrueEndMiddle.

public void testIntersectionTrueEndMiddle() {
    final LineString line1 = this.geometryFactory.geometry("LINESTRING(800020 1000010,800010 1000010,800000 1000010)");
    final LineStringGraph graph = new LineStringGraph(line1);
    final LineString line2 = this.geometryFactory.geometry("LINESTRING(800000 1000000,800000 1000010,800000 1000020)");
    final boolean intersects = graph.intersects(line2);
    Assert.assertEquals("Intersects incorrect", true, intersects);
}
Also used : LineString(com.revolsys.geometry.model.LineString) LineStringGraph(com.revolsys.geometry.graph.linestring.LineStringGraph)

Example 4 with LineStringGraph

use of com.revolsys.geometry.graph.linestring.LineStringGraph in project com.revolsys.open by revolsys.

the class LineStringGraphTest method testIntersectionTrueMiddleEnd.

public void testIntersectionTrueMiddleEnd() {
    final LineString line1 = this.geometryFactory.geometry("LINESTRING(800000 1000000,800000 1000010,800000 1000020)");
    final LineStringGraph graph = new LineStringGraph(line1);
    final LineString line2 = this.geometryFactory.geometry("LINESTRING(800020 1000010,800010 1000010,800000 1000010)");
    final boolean intersects = graph.intersects(line2);
    Assert.assertEquals("Intersects incorrect", true, intersects);
}
Also used : LineString(com.revolsys.geometry.model.LineString) LineStringGraph(com.revolsys.geometry.graph.linestring.LineStringGraph)

Example 5 with LineStringGraph

use of com.revolsys.geometry.graph.linestring.LineStringGraph in project com.revolsys.open by revolsys.

the class LineStringGraphTest method testIntersectionFalseLoopEndLoop.

public void testIntersectionFalseLoopEndLoop() {
    final LineString line1 = this.geometryFactory.geometry("LINESTRING(1190705.094 390263.56,1190787.013 390248.01,1190811.675 390258.037,1190810.052 390224.994,1190801.125 390198.639,1190799.709 390184.59,1190796.247 390171.454,1190786.65 390161.059,1190772.755 390157.479,1190737.214 390156.946)");
    final LineStringGraph graph = new LineStringGraph(line1);
    final LineString line2 = this.geometryFactory.geometry("LINESTRING(1190737.214 390156.946,1190735.656 390156.923,1190734.175 390153.953,1190737.214 390156.946)");
    final boolean intersects = graph.intersects(line2);
    Assert.assertEquals("Intersects incorrect", false, intersects);
}
Also used : LineString(com.revolsys.geometry.model.LineString) LineStringGraph(com.revolsys.geometry.graph.linestring.LineStringGraph)

Aggregations

LineStringGraph (com.revolsys.geometry.graph.linestring.LineStringGraph)25 LineString (com.revolsys.geometry.model.LineString)24 Edge (com.revolsys.geometry.graph.Edge)2 LineSegment (com.revolsys.geometry.model.segment.LineSegment)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Node (com.revolsys.geometry.graph.Node)1 Point (com.revolsys.geometry.model.Point)1 LineStringDouble (com.revolsys.geometry.model.impl.LineStringDouble)1 HashMap (java.util.HashMap)1