Search in sources :

Example 11 with Segment

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

the class MarkerStyleRenderer method renderMarkerSegments.

public static final void renderMarkerSegments(final Viewport2D viewport, final Graphics2D graphics, Geometry geometry, final MarkerStyle style) {
    geometry = getGeometry(viewport, geometry);
    if (Property.hasValue(geometry)) {
        final String orientationType = style.getMarkerOrientationType();
        if ("none".equals(orientationType)) {
            for (final Segment segment : geometry.segments()) {
                final Point point = segment.midPoint();
                renderMarker(viewport, graphics, point, style, 0);
            }
        } else {
            for (final Segment segment : geometry.segments()) {
                final Point point = segment.midPoint();
                final double orientation = segment.getOrientaton();
                renderMarker(viewport, graphics, point, style, orientation);
            }
        }
    }
}
Also used : LineString(com.revolsys.geometry.model.LineString) Point(com.revolsys.geometry.model.Point) Segment(com.revolsys.geometry.model.segment.Segment)

Example 12 with Segment

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

the class LineString method split.

default List<LineString> split(Point point) {
    final GeometryFactory geometryFactory = getGeometryFactory();
    point = point.convertGeometry(geometryFactory);
    final double x = point.getX();
    final double y = point.getY();
    final Pair<GeometryComponent, Double> result = findClosestGeometryComponent(x, y);
    if (result.isEmpty()) {
        return Collections.<LineString>singletonList(this);
    } else {
        final int vertexCount = getVertexCount();
        final GeometryComponent geometryComponent = result.getValue1();
        final double distance = result.getValue2();
        if (geometryComponent instanceof Vertex) {
            final Vertex vertex = (Vertex) geometryComponent;
            final int vertexIndex = vertex.getVertexIndex();
            if (distance == 0) {
                if (vertexIndex <= 0 || vertexIndex >= vertexCount - 1) {
                    return Collections.<LineString>singletonList(this);
                } else {
                    final LineString line1 = subLine(vertexIndex + 1);
                    final LineString line2 = subLine(vertexIndex, vertexCount - vertexIndex);
                    return Arrays.asList(line1, line2);
                }
            } else {
                final LineString line1 = subLine(vertexIndex + 1, point);
                final LineString line2 = subLine(point, vertexIndex, vertexCount - vertexIndex, null);
                return Arrays.asList(line1, line2);
            }
        } else if (geometryComponent instanceof Segment) {
            final Segment segment = (Segment) geometryComponent;
            final int segmentIndex = segment.getSegmentIndex();
            final LineString line1 = subLine(segmentIndex + 1, point);
            final LineString line2 = subLine(point, segmentIndex + 1, vertexCount - segmentIndex - 1, null);
            return Arrays.asList(line1, line2);
        } else {
            return Collections.<LineString>singletonList(this);
        }
    }
}
Also used : Vertex(com.revolsys.geometry.model.vertex.Vertex) LineStringVertex(com.revolsys.geometry.model.vertex.LineStringVertex) AbstractVertex(com.revolsys.geometry.model.vertex.AbstractVertex) PreparedLineString(com.revolsys.geometry.model.prep.PreparedLineString) LineSegmentDouble(com.revolsys.geometry.model.segment.LineSegmentDouble) BiConsumerDouble(com.revolsys.util.function.BiConsumerDouble) Consumer3Double(com.revolsys.util.function.Consumer3Double) Consumer4Double(com.revolsys.util.function.Consumer4Double) LineStringSegment(com.revolsys.geometry.model.segment.LineStringSegment) Segment(com.revolsys.geometry.model.segment.Segment)

Example 13 with Segment

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

the class Lineal method addIsSimpleErrors.

static boolean addIsSimpleErrors(final Lineal lineal, final List<GeometryValidationError> errors, final boolean shortCircuit) {
    final LineSegmentIndex index = new LineSegmentIndex(lineal);
    for (final Segment segment : lineal.segments()) {
        final int segmentIndex = segment.getSegmentIndex();
        final int partIndex = segment.getPartIndex();
        if (segment.getLength() == 0) {
            errors.add(new DuplicateVertexError(segment.getGeometryVertex(0)));
        } else {
            final List<LineSegment> segments = index.queryBoundingBox(segment);
            for (final LineSegment lineSegment : segments) {
                final Segment segment2 = (Segment) lineSegment;
                final int partIndex2 = segment2.getPartIndex();
                final int segmentIndex2 = segment2.getSegmentIndex();
                if (partIndex2 > partIndex || partIndex == partIndex2 && segmentIndex2 > segmentIndex) {
                    if (segment.equals(lineSegment)) {
                        final SelfOverlapSegmentError error = new SelfOverlapSegmentError(segment);
                        errors.add(error);
                        if (shortCircuit) {
                            return false;
                        }
                    } else {
                        final Geometry intersection = segment.getIntersection(lineSegment);
                        if (intersection instanceof Point) {
                            final Point pointIntersection = (Point) intersection;
                            boolean isIntersection = true;
                            // Process segments on the same linestring part
                            if (partIndex == partIndex2) {
                                // segment
                                if (segmentIndex + 1 == segmentIndex2) {
                                    if (lineSegment.equalsVertex(2, 0, pointIntersection)) {
                                        isIntersection = false;
                                    }
                                // A loop can touch itself at the start/end
                                } else if (segment.isLineClosed()) {
                                    if (segment.isLineStart() && segment2.isLineEnd()) {
                                        if (segment.equalsVertex(2, 0, pointIntersection)) {
                                            isIntersection = false;
                                        }
                                    }
                                }
                            } else {
                                if (!segment.isLineClosed() && !segment2.isLineClosed()) {
                                    final boolean segment1EndIntersection = segment.isEndIntersection(pointIntersection);
                                    final boolean segment2EndIntersection = segment2.isEndIntersection(pointIntersection);
                                    if (segment1EndIntersection && segment2EndIntersection) {
                                        isIntersection = false;
                                    }
                                }
                            }
                            if (isIntersection) {
                                GeometryValidationError error;
                                if (segment.equalsVertex(2, 0, pointIntersection)) {
                                    final Vertex vertex = segment.getGeometryVertex(0);
                                    error = new SelfIntersectionVertexError(vertex);
                                } else if (segment.equalsVertex(2, 1, pointIntersection)) {
                                    final Vertex vertex = segment.getGeometryVertex(1);
                                    error = new SelfIntersectionVertexError(vertex);
                                } else {
                                    error = new SelfIntersectionPointError(lineal, pointIntersection);
                                }
                                errors.add(error);
                                if (shortCircuit) {
                                    return false;
                                }
                            }
                        } else if (intersection instanceof LineSegment) {
                            final LineSegment lineIntersection = (LineSegment) intersection;
                            GeometryValidationError error;
                            if (segment.equals(lineIntersection)) {
                                error = new SelfOverlapSegmentError(segment);
                            } else if (lineSegment.equals(lineIntersection)) {
                                error = new SelfOverlapSegmentError(segment2);
                            } else {
                                error = new SelfOverlapLineSegmentError(lineal, lineIntersection);
                            }
                            errors.add(error);
                            if (shortCircuit) {
                                return false;
                            }
                        }
                    }
                }
            }
        }
    }
    return errors.isEmpty();
}
Also used : GeometryValidationError(com.revolsys.geometry.operation.valid.GeometryValidationError) Vertex(com.revolsys.geometry.model.vertex.Vertex) SelfOverlapLineSegmentError(com.revolsys.geometry.operation.simple.SelfOverlapLineSegmentError) SelfIntersectionPointError(com.revolsys.geometry.operation.simple.SelfIntersectionPointError) DuplicateVertexError(com.revolsys.geometry.operation.simple.DuplicateVertexError) LineSegment(com.revolsys.geometry.model.segment.LineSegment) Segment(com.revolsys.geometry.model.segment.Segment) LineSegmentIndex(com.revolsys.geometry.index.LineSegmentIndex) SelfIntersectionVertexError(com.revolsys.geometry.operation.simple.SelfIntersectionVertexError) SelfOverlapSegmentError(com.revolsys.geometry.operation.simple.SelfOverlapSegmentError) LineSegment(com.revolsys.geometry.model.segment.LineSegment)

Example 14 with Segment

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

the class LineString method findClosestGeometryComponent.

@Override
default Pair<GeometryComponent, Double> findClosestGeometryComponent(final double x, final double y) {
    if (isEmpty()) {
        return new Pair<>();
    } else {
        final GeometryFactory geometryFactory = getGeometryFactory();
        boolean closestIsVertex = false;
        int closestIndex = 0;
        double x1 = getX(0);
        double y1 = getY(0);
        if (x == x1 && y == y1) {
            final AbstractVertex closestVertex = getVertex(0);
            return new Pair<>(closestVertex, 0.0);
        } else {
            double closestDistance = geometryFactory.makePrecise(0, MathUtil.distance(x, y, x1, y1));
            final int vertexCount = getVertexCount();
            for (int vertexIndex = 1; vertexIndex < vertexCount; vertexIndex++) {
                final double x2 = getX(vertexIndex);
                final double y2 = getY(vertexIndex);
                if (x == x2 && y == y2) {
                    final AbstractVertex closestVertex = getVertex(vertexIndex);
                    return new Pair<>(closestVertex, 0.0);
                } else {
                    final double toDistance = geometryFactory.makePrecise(0, MathUtil.distance(x, y, x2, y2));
                    if (toDistance <= closestDistance) {
                        if (!closestIsVertex || toDistance < closestDistance) {
                            closestIndex = vertexIndex;
                            closestIsVertex = true;
                            closestDistance = toDistance;
                        }
                    }
                    final double segmentDistance = geometryFactory.makePrecise(0, LineSegmentUtil.distanceLinePoint(x1, y1, x2, y2, x, y));
                    if (segmentDistance == 0) {
                        final Segment closestSegment = getSegment(vertexIndex - 1);
                        return new Pair<>(closestSegment, 0.0);
                    } else if (segmentDistance < closestDistance) {
                        closestIsVertex = false;
                        closestIndex = vertexIndex - 1;
                        closestDistance = segmentDistance;
                    }
                }
                x1 = x2;
                y1 = y2;
            }
            if (closestIsVertex) {
                final Vertex closestVertex = getVertex(closestIndex);
                return new Pair<>(closestVertex, closestDistance);
            } else {
                final Segment closestSegment = getSegment(closestIndex);
                return new Pair<>(closestSegment, closestDistance);
            }
        }
    }
}
Also used : AbstractVertex(com.revolsys.geometry.model.vertex.AbstractVertex) Vertex(com.revolsys.geometry.model.vertex.Vertex) LineStringVertex(com.revolsys.geometry.model.vertex.LineStringVertex) AbstractVertex(com.revolsys.geometry.model.vertex.AbstractVertex) LineStringSegment(com.revolsys.geometry.model.segment.LineStringSegment) Segment(com.revolsys.geometry.model.segment.Segment) Pair(com.revolsys.util.Pair)

Example 15 with Segment

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

the class Geometry method findClosestGeometryComponent.

default Pair<GeometryComponent, Double> findClosestGeometryComponent(final double x, final double y, final double maxDistance) {
    if (isEmpty()) {
        return new Pair<>();
    } else {
        GeometryComponent closestComponent = null;
        double closestDistance = Double.POSITIVE_INFINITY;
        for (final Segment segment : segments()) {
            boolean matched = false;
            if (segment.isLineStart()) {
                final Vertex from = segment.getGeometryVertex(0);
                if (from.equalsVertex(x, y)) {
                    return new Pair<>(from, 0.0);
                } else {
                    final double fromDistance = from.distance(x, y);
                    if (fromDistance <= maxDistance) {
                        if (// 
                        fromDistance < closestDistance || fromDistance == closestDistance && !(closestComponent instanceof Vertex)) {
                            closestDistance = fromDistance;
                            closestComponent = from.clone();
                            matched = true;
                        }
                    }
                }
            }
            {
                final Vertex to = segment.getGeometryVertex(1);
                if (to.equalsVertex(x, y)) {
                    return new Pair<>(to, 0.0);
                } else {
                    final double toDistance = to.distance(x, y);
                    if (toDistance <= maxDistance) {
                        if (// 
                        toDistance < closestDistance || toDistance == closestDistance && !(closestComponent instanceof Vertex)) {
                            closestDistance = toDistance;
                            closestComponent = to.clone();
                            matched = true;
                        }
                    }
                }
            }
            if (!matched) {
                final double segmentDistance = segment.distance(x, y);
                if (segmentDistance == 0) {
                    return new Pair<>(segment, 0.0);
                } else if (segmentDistance <= maxDistance) {
                    if (// 
                    segmentDistance < closestDistance || segmentDistance == closestDistance && !(closestComponent instanceof Vertex)) {
                        closestDistance = segmentDistance;
                        closestComponent = segment.clone();
                    }
                }
            }
        }
        if (Double.isFinite(closestDistance)) {
            return new Pair<>(closestComponent, closestDistance);
        } else {
            return new Pair<>();
        }
    }
}
Also used : Vertex(com.revolsys.geometry.model.vertex.Vertex) Segment(com.revolsys.geometry.model.segment.Segment) Pair(com.revolsys.util.Pair)

Aggregations

Segment (com.revolsys.geometry.model.segment.Segment)22 Point (com.revolsys.geometry.model.Point)16 Vertex (com.revolsys.geometry.model.vertex.Vertex)6 LineSegment (com.revolsys.geometry.model.segment.LineSegment)4 BoundingBox (com.revolsys.geometry.model.BoundingBox)3 Geometry (com.revolsys.geometry.model.Geometry)2 LineString (com.revolsys.geometry.model.LineString)2 LineStringSegment (com.revolsys.geometry.model.segment.LineStringSegment)2 AbstractVertex (com.revolsys.geometry.model.vertex.AbstractVertex)2 LineStringVertex (com.revolsys.geometry.model.vertex.LineStringVertex)2 GeometryLocation (com.revolsys.geometry.operation.distance.GeometryLocation)2 Pair (com.revolsys.util.Pair)2 PointPairDistance (com.revolsys.geometry.algorithm.distance.PointPairDistance)1 LineSegmentIndex (com.revolsys.geometry.index.LineSegmentIndex)1 GeometryComponent (com.revolsys.geometry.model.GeometryComponent)1 PointDoubleXY (com.revolsys.geometry.model.impl.PointDoubleXY)1 PointDoubleXYOrientation (com.revolsys.geometry.model.impl.PointDoubleXYOrientation)1 PreparedLineString (com.revolsys.geometry.model.prep.PreparedLineString)1 LineSegmentDouble (com.revolsys.geometry.model.segment.LineSegmentDouble)1 DuplicateVertexError (com.revolsys.geometry.operation.simple.DuplicateVertexError)1