Search in sources :

Example 31 with Edge

use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.

the class LineStringGraph method intersects.

public boolean intersects(final LineString line) {
    BoundingBox envelope = line.getBoundingBox();
    final double scaleXY = this.geometryFactory.getScaleXY();
    double maxDistance = 0;
    if (scaleXY > 0) {
        maxDistance = 1 / scaleXY;
    }
    envelope = envelope.expand(maxDistance);
    if (envelope.intersects(this.envelope)) {
        final LineString points = line;
        final int numPoints = points.getVertexCount();
        final Point fromPoint = points.getPoint(0);
        final Point toPoint = points.getPoint(numPoints - 1);
        Point previousPoint = fromPoint;
        for (int i = 1; i < numPoints; i++) {
            final Point nextPoint = points.getPoint(i);
            final LineSegment line1 = new LineSegmentDoubleGF(previousPoint, nextPoint);
            final List<Edge<LineSegment>> edges = EdgeLessThanDistance.getEdges(this, line1, maxDistance);
            for (final Edge<LineSegment> edge2 : edges) {
                final LineSegment line2 = edge2.getObject();
                final Geometry intersections = line1.getIntersection(line2);
                for (final Point intersection : intersections.vertices()) {
                    if (intersection.equals(fromPoint) || intersection.equals(toPoint)) {
                        // Point intersection, make sure it's not at the start
                        final Node<LineSegment> node = findNode(intersection);
                        final int degree = node.getDegree();
                        if (node.equals(2, this.fromPoint)) {
                            if (degree > 2) {
                                // into account loops
                                return true;
                            }
                        } else if (degree > 1) {
                            // Intersection not at the start/end of the other line
                            return true;
                        }
                    } else {
                        // Intersection not at the start/end of the line
                        return true;
                    }
                }
                for (final Point point : line1.vertices()) {
                    if (line2.distancePoint(point) < maxDistance) {
                        if (point.equals(fromPoint) || point.equals(toPoint)) {
                            // Point intersection, make sure it's not at the start
                            final double maxDistance1 = maxDistance;
                            for (final Node<LineSegment> node : this.getNodes(point, maxDistance1)) {
                                final int degree = node.getDegree();
                                if (node.equals(2, this.fromPoint)) {
                                    if (degree > 2) {
                                        // into account loops
                                        return true;
                                    }
                                } else if (degree > 1) {
                                    // Intersection not at the start/end of the other line
                                    return true;
                                }
                            }
                        } else {
                            // Intersection not at the start/end of the line
                            return true;
                        }
                    }
                }
            }
            previousPoint = nextPoint;
        }
    }
    return false;
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) LineString(com.revolsys.geometry.model.LineString) BoundingBox(com.revolsys.geometry.model.BoundingBox) Point(com.revolsys.geometry.model.Point) Edge(com.revolsys.geometry.graph.Edge) Point(com.revolsys.geometry.model.Point) LineSegmentDoubleGF(com.revolsys.geometry.model.segment.LineSegmentDoubleGF) LineSegment(com.revolsys.geometry.model.segment.LineSegment) PointOnLineSegment(com.revolsys.geometry.model.coordinates.filter.PointOnLineSegment)

Example 32 with Edge

use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.

the class LinearIntersectionNotEqualLineEdgeCleanupVisitor method accept.

@Override
public void accept(final Edge<Record> edge) {
    final String typePath = edge.getTypeName();
    final Graph<Record> graph = edge.getGraph();
    final LineString line = edge.getLineString();
    Predicate<Edge<Record>> attributeAndGeometryFilter = new EdgeTypeNameFilter<>(typePath);
    final Predicate<Edge<Record>> filter = getPredicate();
    if (filter != null) {
        attributeAndGeometryFilter = attributeAndGeometryFilter.and(filter);
    }
    final Predicate<Record> notEqualLineFilter = new RecordGeometryFilter<>(new EqualFilter<>(line)).negate();
    final RecordGeometryFilter<LineString> linearIntersectionFilter = new RecordGeometryFilter<>(new LinearIntersectionFilter(line));
    attributeAndGeometryFilter = attributeAndGeometryFilter.and(new EdgeObjectFilter<>(notEqualLineFilter.and(linearIntersectionFilter)));
    final List<Edge<Record>> intersectingEdges = graph.getEdges(line, attributeAndGeometryFilter);
    if (!intersectingEdges.isEmpty()) {
        if (intersectingEdges.size() == 1 && line.getLength() > 10) {
            if (line.getVertexCount() > 2) {
                final Edge<Record> edge2 = intersectingEdges.get(0);
                final LineString line2 = edge2.getLineString();
                if (middleCoordinatesEqual(line, line2)) {
                    final boolean firstEqual = line.equalsVertex(2, 0, line2, 0);
                    if (!firstEqual) {
                        final Node<Record> fromNode1 = edge.getFromNode();
                        final Node<Record> fromNode2 = edge2.getFromNode();
                        if (fromNode1.distancePoint(fromNode2) < 2) {
                            graph.moveNodesToMidpoint(typePath, fromNode1, fromNode2);
                            return;
                        }
                    }
                    final boolean lastEqual = line.equalsVertex(2, line.getVertexCount() - 1, line2, line.getVertexCount() - 1);
                    if (!lastEqual) {
                        final Node<Record> toNode1 = edge.getToNode();
                        final Node<Record> toNode2 = edge2.getToNode();
                        if (toNode1.distancePoint(toNode2) < 2) {
                            graph.moveNodesToMidpoint(typePath, toNode1, toNode2);
                            return;
                        }
                    }
                }
            }
        }
        Logs.error(this, "Has intersecting edges " + line);
    }
}
Also used : EdgeObjectFilter(com.revolsys.geometry.graph.filter.EdgeObjectFilter) LinearIntersectionFilter(com.revolsys.geometry.filter.LinearIntersectionFilter) LineString(com.revolsys.geometry.model.LineString) RecordGeometryFilter(com.revolsys.record.filter.RecordGeometryFilter) LineString(com.revolsys.geometry.model.LineString) Record(com.revolsys.record.Record) EqualFilter(com.revolsys.geometry.filter.EqualFilter) EdgeTypeNameFilter(com.revolsys.geometry.graph.filter.EdgeTypeNameFilter) Edge(com.revolsys.geometry.graph.Edge)

Example 33 with Edge

use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.

the class NodeOnEdgeVisitor method getEdges.

public static <T> List<Edge<T>> getEdges(final Graph<T> graph, final Node<T> node, final double maxDistance) {
    final CreateListVisitor<Edge<T>> results = new CreateListVisitor<>();
    final Point point = node;
    BoundingBox boundingBox = point.getBoundingBox();
    boundingBox = boundingBox.expand(maxDistance);
    final IdObjectIndex<Edge<T>> index = graph.getEdgeIndex();
    final NodeOnEdgeVisitor<T> visitor = new NodeOnEdgeVisitor<>(node, boundingBox, maxDistance, results);
    index.forEach(boundingBox, visitor);
    final List<Edge<T>> edges = results.getList();
    Collections.sort(edges);
    return edges;
}
Also used : BoundingBox(com.revolsys.geometry.model.BoundingBox) CreateListVisitor(com.revolsys.visitor.CreateListVisitor) Point(com.revolsys.geometry.model.Point) Edge(com.revolsys.geometry.graph.Edge)

Example 34 with Edge

use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.

the class PolygonNodeRemovalVisitor method accept.

@Override
public void accept(final Node<Record> node) {
    final Set<Edge<Record>> edges = new LinkedHashSet<>(node.getEdges());
    while (edges.size() > 1) {
        final Edge<Record> edge = edges.iterator().next();
        final Record object = edge.getObject();
        final Set<Edge<Record>> matchedEdges = new HashSet<>();
        final End end = edge.getEnd(node);
        for (final Edge<Record> matchEdge : edges) {
            final Record matchObject = matchEdge.getObject();
            if (edge != matchEdge) {
                final End matchEnd = matchEdge.getEnd(node);
                if (end != matchEnd) {
                    if (DataType.equal(object, matchObject, this.excludedAttributes)) {
                        matchedEdges.add(matchEdge);
                    }
                }
            }
        }
        if (matchedEdges.size() == 1) {
            final Edge<Record> matchedEdge = matchedEdges.iterator().next();
            if (end.isFrom()) {
                this.graph.merge(node, matchedEdge, edge);
            } else {
                this.graph.merge(node, edge, matchedEdge);
            }
        }
        edges.removeAll(matchedEdges);
        edges.remove(edge);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Record(com.revolsys.record.Record) End(com.revolsys.geometry.model.End) Edge(com.revolsys.geometry.graph.Edge) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 35 with Edge

use of com.revolsys.geometry.graph.Edge in project com.revolsys.open by revolsys.

the class EdgeIntersectLineVisitor method getEdges.

public static <T> List<Edge<T>> getEdges(final Graph<T> graph, final LineString line) {
    final CreateListVisitor<Edge<T>> results = new CreateListVisitor<>();
    final BoundingBox env = line.getBoundingBox();
    final IdObjectIndex<Edge<T>> index = graph.getEdgeIndex();
    index.forEach(env, new EdgeIntersectLineVisitor<>(line, results));
    return results.getList();
}
Also used : BoundingBox(com.revolsys.geometry.model.BoundingBox) CreateListVisitor(com.revolsys.visitor.CreateListVisitor) Edge(com.revolsys.geometry.graph.Edge)

Aggregations

Edge (com.revolsys.geometry.graph.Edge)40 LineString (com.revolsys.geometry.model.LineString)22 Point (com.revolsys.geometry.model.Point)21 LineSegment (com.revolsys.geometry.model.segment.LineSegment)19 ArrayList (java.util.ArrayList)14 BoundingBox (com.revolsys.geometry.model.BoundingBox)10 PointOnLineSegment (com.revolsys.geometry.model.coordinates.filter.PointOnLineSegment)9 Node (com.revolsys.geometry.graph.Node)8 Geometry (com.revolsys.geometry.model.Geometry)8 LinkedHashSet (java.util.LinkedHashSet)7 Record (com.revolsys.record.Record)6 CreateListVisitor (com.revolsys.visitor.CreateListVisitor)6 HashSet (java.util.HashSet)6 EdgeAttributeValueComparator (com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator)5 List (java.util.List)5 EdgeObjectFilter (com.revolsys.geometry.graph.filter.EdgeObjectFilter)4 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)4 LineSegmentDoubleGF (com.revolsys.geometry.model.segment.LineSegmentDoubleGF)4 EdgeTypeNameFilter (com.revolsys.geometry.graph.filter.EdgeTypeNameFilter)2 LineStringGraph (com.revolsys.geometry.graph.linestring.LineStringGraph)2