Search in sources :

Example 1 with EdgeAttributeValueComparator

use of com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator in project com.revolsys.open by revolsys.

the class LineStringGraph method getLines.

public List<LineString> getLines() {
    removeDuplicateEdges();
    final EdgeAttributeValueComparator<LineSegment> comparator = new EdgeAttributeValueComparator<>("INDEX");
    final List<LineString> lines = new ArrayList<>();
    final List<Point> points = new ArrayList<>();
    final Consumer<Edge<LineSegment>> action = new Consumer<Edge<LineSegment>>() {

        private Node<LineSegment> previousNode = null;

        @Override
        public void accept(final Edge<LineSegment> edge) {
            final LineSegment lineSegment = edge.getObject();
            if (lineSegment.getLength() > 0) {
                final Node<LineSegment> fromNode = edge.getFromNode();
                final Node<LineSegment> toNode = edge.getToNode();
                if (this.previousNode == null) {
                    points.add(lineSegment.getPoint(0));
                    points.add(lineSegment.getPoint(1));
                } else if (fromNode == this.previousNode) {
                    if (edge.getLength() > 0) {
                        points.add(toNode);
                    }
                } else {
                    if (points.size() > 1) {
                        final LineString line = LineStringGraph.this.geometryFactory.lineString(points);
                        lines.add(line);
                    }
                    points.clear();
                    ;
                    points.add(lineSegment.getPoint(0));
                    points.add(lineSegment.getPoint(1));
                }
                if (points.size() > 1) {
                    final int toDegree = toNode.getDegree();
                    if (toDegree != 2) {
                        final LineString line = LineStringGraph.this.geometryFactory.lineString(points);
                        lines.add(line);
                        points.clear();
                        ;
                        points.add(toNode);
                    }
                }
                this.previousNode = toNode;
            }
        }
    };
    forEachEdge(comparator, action);
    if (points.size() > 1) {
        final LineString line = this.geometryFactory.lineString(points);
        lines.add(line);
    }
    return lines;
}
Also used : EdgeAttributeValueComparator(com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator) Node(com.revolsys.geometry.graph.Node) ArrayList(java.util.ArrayList) Point(com.revolsys.geometry.model.Point) Point(com.revolsys.geometry.model.Point) Consumer(java.util.function.Consumer) LineString(com.revolsys.geometry.model.LineString) Edge(com.revolsys.geometry.graph.Edge) LineSegment(com.revolsys.geometry.model.segment.LineSegment) PointOnLineSegment(com.revolsys.geometry.model.coordinates.filter.PointOnLineSegment)

Example 2 with EdgeAttributeValueComparator

use of com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator in project com.revolsys.open by revolsys.

the class LineStringGraph method removeDuplicateEdges.

private void removeDuplicateEdges() {
    final Comparator<Edge<LineSegment>> comparator = new EdgeAttributeValueComparator<>(INDEX);
    forEachEdge(comparator, (edge) -> {
        final Node<LineSegment> fromNode = edge.getFromNode();
        final Node<LineSegment> toNode = edge.getToNode();
        final Collection<Edge<LineSegment>> edges = fromNode.getEdgesTo(toNode);
        final int duplicateCount = edges.size();
        if (duplicateCount > 1) {
            edges.remove(edge);
            Edge.remove(edges);
        }
    });
}
Also used : EdgeAttributeValueComparator(com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator) Edge(com.revolsys.geometry.graph.Edge) Point(com.revolsys.geometry.model.Point) LineSegment(com.revolsys.geometry.model.segment.LineSegment) PointOnLineSegment(com.revolsys.geometry.model.coordinates.filter.PointOnLineSegment)

Example 3 with EdgeAttributeValueComparator

use of com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator in project com.revolsys.open by revolsys.

the class GeometryGraph method getGeometry.

/**
 * Only currently works for lines and points.
 *
 * @return
 */
public Geometry getGeometry() {
    removeDuplicateLineEdges();
    final EdgeAttributeValueComparator<LineSegment> comparator = new EdgeAttributeValueComparator<>("geometryIndex", "partIndex", "segmentIndex");
    final List<Geometry> geometries = new ArrayList<>(this.points);
    final GeometryFactory geometryFactory = getGeometryFactory();
    final List<Point> points = new ArrayList<>();
    final Consumer<Edge<LineSegment>> action = new Consumer<Edge<LineSegment>>() {

        private Node<LineSegment> previousNode = null;

        @Override
        public void accept(final Edge<LineSegment> edge) {
            final LineSegment lineSegment = edge.getObject();
            if (lineSegment.getLength() > 0) {
                final Node<LineSegment> fromNode = edge.getFromNode();
                final Node<LineSegment> toNode = edge.getToNode();
                if (this.previousNode == null) {
                    points.add(lineSegment.getPoint(0));
                    points.add(lineSegment.getPoint(1));
                } else if (fromNode == this.previousNode) {
                    if (edge.getLength() > 0) {
                        points.add(toNode);
                    }
                } else {
                    if (points.size() > 1) {
                        final LineString line = geometryFactory.lineString(points);
                        geometries.add(line);
                    }
                    points.clear();
                    ;
                    points.add(lineSegment.getPoint(0));
                    points.add(lineSegment.getPoint(1));
                }
                if (points.size() > 1) {
                    final int toDegree = toNode.getDegree();
                    if (toDegree != 2) {
                        final LineString line = geometryFactory.lineString(points);
                        geometries.add(line);
                        points.clear();
                        ;
                        points.add(toNode);
                    }
                }
                this.previousNode = toNode;
            }
        }
    };
    forEachEdge(comparator, action);
    if (points.size() > 1) {
        final LineString line = geometryFactory.lineString(points);
        geometries.add(line);
    }
    return geometryFactory.geometry(geometries);
}
Also used : EdgeAttributeValueComparator(com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) Node(com.revolsys.geometry.graph.Node) ArrayList(java.util.ArrayList) Point(com.revolsys.geometry.model.Point) Point(com.revolsys.geometry.model.Point) Geometry(com.revolsys.geometry.model.Geometry) Consumer(java.util.function.Consumer) LineString(com.revolsys.geometry.model.LineString) Edge(com.revolsys.geometry.graph.Edge) LineSegment(com.revolsys.geometry.model.segment.LineSegment)

Example 4 with EdgeAttributeValueComparator

use of com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator in project com.revolsys.open by revolsys.

the class GeometryGraph method removeDuplicateLineEdges.

public void removeDuplicateLineEdges() {
    final Comparator<Edge<LineSegment>> comparator = new EdgeAttributeValueComparator<>("geometryIndex", "partIndex", "segmentIndex");
    forEachEdge(comparator, (edge) -> {
        if (isLineString(edge)) {
            final Node<LineSegment> fromNode = edge.getFromNode();
            final Node<LineSegment> toNode = edge.getToNode();
            final Collection<Edge<LineSegment>> edges = fromNode.getEdgesTo(toNode);
            final int duplicateCount = edges.size();
            if (duplicateCount > 1) {
                edges.remove(edge);
                for (final Edge<LineSegment> removeEdge : edges) {
                    if (isLineString(removeEdge)) {
                        removeEdge.remove();
                    }
                }
            }
        }
    });
}
Also used : EdgeAttributeValueComparator(com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator) Edge(com.revolsys.geometry.graph.Edge) Point(com.revolsys.geometry.model.Point) LineSegment(com.revolsys.geometry.model.segment.LineSegment)

Example 5 with EdgeAttributeValueComparator

use of com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator in project com.revolsys.open by revolsys.

the class LineStringGraph method splitCrossingEdges.

public void splitCrossingEdges() {
    final Comparator<Edge<LineSegment>> comparator = new EdgeAttributeValueComparator<>(INDEX);
    forEachEdge(comparator, (edge) -> {
        final LineSegment line1 = edge.getObject();
        final Predicate<LineSegment> lineFilter = new CrossingLineSegmentFilter(line1);
        final Predicate<Edge<LineSegment>> filter = new EdgeObjectFilter<>(lineFilter);
        final List<Edge<LineSegment>> edges = getEdges(line1, filter);
        if (!edges.isEmpty()) {
            final List<Point> points = new ArrayList<>();
            for (final Edge<LineSegment> edge2 : edges) {
                final LineSegment line2 = edge2.getObject();
                final Geometry intersections = line1.getIntersection(line2);
                if (intersections instanceof Point) {
                    final Point intersection = (Point) intersections;
                    points.add(intersection);
                    edge2.split(intersection);
                }
            }
            if (!points.isEmpty()) {
                edge.splitEdge(points);
            }
        }
    });
}
Also used : EdgeAttributeValueComparator(com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator) EdgeObjectFilter(com.revolsys.geometry.graph.filter.EdgeObjectFilter) ArrayList(java.util.ArrayList) Point(com.revolsys.geometry.model.Point) Geometry(com.revolsys.geometry.model.Geometry) CrossingLineSegmentFilter(com.revolsys.geometry.model.coordinates.filter.CrossingLineSegmentFilter) Edge(com.revolsys.geometry.graph.Edge) LineSegment(com.revolsys.geometry.model.segment.LineSegment) PointOnLineSegment(com.revolsys.geometry.model.coordinates.filter.PointOnLineSegment)

Aggregations

Edge (com.revolsys.geometry.graph.Edge)5 EdgeAttributeValueComparator (com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator)5 Point (com.revolsys.geometry.model.Point)5 LineSegment (com.revolsys.geometry.model.segment.LineSegment)5 PointOnLineSegment (com.revolsys.geometry.model.coordinates.filter.PointOnLineSegment)3 ArrayList (java.util.ArrayList)3 Node (com.revolsys.geometry.graph.Node)2 Geometry (com.revolsys.geometry.model.Geometry)2 LineString (com.revolsys.geometry.model.LineString)2 Consumer (java.util.function.Consumer)2 EdgeObjectFilter (com.revolsys.geometry.graph.filter.EdgeObjectFilter)1 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 CrossingLineSegmentFilter (com.revolsys.geometry.model.coordinates.filter.CrossingLineSegmentFilter)1