Search in sources :

Example 41 with LineSegment

use of com.revolsys.geometry.model.segment.LineSegment in project com.revolsys.open by revolsys.

the class Polygon method getPointWithin.

@Override
default Point getPointWithin() {
    if (isEmpty()) {
        final GeometryFactory geometryFactory = getGeometryFactory();
        return geometryFactory.point();
    } else {
        Point centroid = null;
        try {
            centroid = getCentroid();
            if (centroid.within(this)) {
                return centroid;
            }
        } catch (final TopologyException e) {
        }
        if (centroid != null) {
            final BoundingBox boundingBox = getBoundingBox();
            final double x1 = centroid.getX();
            final double y1 = centroid.getY();
            for (final double x2 : new double[] { boundingBox.getMinX(), boundingBox.getMaxX() }) {
                for (final double y2 : new double[] { boundingBox.getMinY(), boundingBox.getMaxY() }) {
                    final LineSegment line = new LineSegmentDouble(2, x1, y1, x2, y2);
                    try {
                        final Geometry intersection = intersection(line);
                        if (!intersection.isEmpty()) {
                            return intersection.getPointWithin();
                        }
                    } catch (final TopologyException e) {
                    }
                }
            }
        }
        return getPoint();
    }
}
Also used : LineSegmentDouble(com.revolsys.geometry.model.segment.LineSegmentDouble) LineSegment(com.revolsys.geometry.model.segment.LineSegment)

Example 42 with LineSegment

use of com.revolsys.geometry.model.segment.LineSegment 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 43 with LineSegment

use of com.revolsys.geometry.model.segment.LineSegment in project com.revolsys.open by revolsys.

the class GeometryGraph method addEdge.

public void addEdge(final Node<LineSegment> fromNode, final Node<LineSegment> toNode) {
    final LineSegment lineSegment = new LineSegmentDoubleGF(fromNode, toNode);
    addEdge(lineSegment, fromNode, toNode);
}
Also used : LineSegmentDoubleGF(com.revolsys.geometry.model.segment.LineSegmentDoubleGF) LineSegment(com.revolsys.geometry.model.segment.LineSegment)

Example 44 with LineSegment

use of com.revolsys.geometry.model.segment.LineSegment 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 45 with LineSegment

use of com.revolsys.geometry.model.segment.LineSegment in project com.revolsys.open by revolsys.

the class GeometryGraph method intersects.

public boolean intersects(final LineString line) {
    BoundingBox boundingBox = line.getBoundingBox();
    final double scaleXY = getGeometryFactory().getScaleXY();
    double maxDistance = 0;
    if (scaleXY > 0) {
        maxDistance = 1 / scaleXY;
    }
    boundingBox = boundingBox.expand(maxDistance);
    if (boundingBox.intersects(this.boundingBox)) {
        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 (isStartPoint(node)) {
                            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 (isStartPoint(node)) {
                                    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)

Aggregations

LineSegment (com.revolsys.geometry.model.segment.LineSegment)61 Point (com.revolsys.geometry.model.Point)34 Edge (com.revolsys.geometry.graph.Edge)19 LineSegmentDouble (com.revolsys.geometry.model.segment.LineSegmentDouble)14 LineSegmentDoubleGF (com.revolsys.geometry.model.segment.LineSegmentDoubleGF)13 Geometry (com.revolsys.geometry.model.Geometry)12 PointOnLineSegment (com.revolsys.geometry.model.coordinates.filter.PointOnLineSegment)11 ArrayList (java.util.ArrayList)10 LineString (com.revolsys.geometry.model.LineString)9 Node (com.revolsys.geometry.graph.Node)7 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)6 EdgeAttributeValueComparator (com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator)5 BoundingBox (com.revolsys.geometry.model.BoundingBox)4 PointDoubleXY (com.revolsys.geometry.model.impl.PointDoubleXY)4 List (java.util.List)4 EdgeObjectFilter (com.revolsys.geometry.graph.filter.EdgeObjectFilter)2 LineStringGraph (com.revolsys.geometry.graph.linestring.LineStringGraph)2 Polygon (com.revolsys.geometry.model.Polygon)2 Vertex (com.revolsys.geometry.model.vertex.Vertex)2 Shape (java.awt.Shape)2