Search in sources :

Example 61 with LineSegment

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

the class AbstractRecordLayerRenderer method getPointWithOrientation.

public static PointDoubleXYOrientation getPointWithOrientation(final Viewport2D viewport, final Geometry geometry, final String placementType) {
    if (viewport == null) {
        return new PointDoubleXYOrientation(0.0, 0.0, 0);
    } else {
        final GeometryFactory viewportGeometryFactory2d = viewport.getGeometryFactory2dFloating();
        if (viewportGeometryFactory2d != null && geometry != null && !geometry.isEmpty()) {
            Point point = null;
            double orientation = 0;
            if (geometry instanceof Point) {
                point = (Point) geometry;
            } else {
                final Matcher vertexIndexMatcher = PATTERN_VERTEX_INDEX.matcher(placementType);
                if (vertexIndexMatcher.matches()) {
                    final int vertexCount = geometry.getVertexCount();
                    final int vertexIndex = getIndex(vertexIndexMatcher);
                    if (vertexIndex >= -vertexCount && vertexIndex < vertexCount) {
                        final Vertex vertex = geometry.getVertex(vertexIndex);
                        orientation = vertex.getOrientaton(viewportGeometryFactory2d);
                        point = vertex.convertGeometry(viewportGeometryFactory2d);
                    }
                } else {
                    final Matcher segmentIndexMatcher = PATTERN_SEGMENT_INDEX.matcher(placementType);
                    if (segmentIndexMatcher.matches()) {
                        final int segmentCount = geometry.getSegmentCount();
                        if (segmentCount > 0) {
                            final int index = getIndex(segmentIndexMatcher);
                            LineSegment segment = geometry.getSegment(index);
                            segment = segment.convertGeometry(viewportGeometryFactory2d);
                            if (segment != null) {
                                point = segment.midPoint();
                                orientation = segment.getOrientaton();
                            }
                        }
                    } else {
                        PointDoubleXYOrientation pointDoubleXYOrientation = getPointWithOrientationCentre(viewportGeometryFactory2d, geometry);
                        if (!viewport.getBoundingBox().covers(pointDoubleXYOrientation)) {
                            try {
                                final Geometry clippedGeometry = viewport.getBoundingBox().toPolygon().intersection(geometry);
                                if (!clippedGeometry.isEmpty()) {
                                    double maxArea = 0;
                                    double maxLength = 0;
                                    for (int i = 0; i < clippedGeometry.getGeometryCount(); i++) {
                                        final Geometry part = clippedGeometry.getGeometry(i);
                                        if (part instanceof Polygon) {
                                            final double area = part.getArea();
                                            if (area > maxArea) {
                                                maxArea = area;
                                                pointDoubleXYOrientation = getPointWithOrientationCentre(viewportGeometryFactory2d, part);
                                            }
                                        } else if (part instanceof LineString) {
                                            if (maxArea == 0 && "auto".equals(placementType)) {
                                                final double length = part.getLength();
                                                if (length > maxLength) {
                                                    maxLength = length;
                                                    pointDoubleXYOrientation = getPointWithOrientationCentre(viewportGeometryFactory2d, part);
                                                }
                                            }
                                        } else if (part instanceof Point) {
                                            if (maxArea == 0 && maxLength == 0 && "auto".equals(placementType)) {
                                                pointDoubleXYOrientation = getPointWithOrientationCentre(viewportGeometryFactory2d, part);
                                            }
                                        }
                                    }
                                }
                            } catch (final Throwable t) {
                            }
                        }
                        return pointDoubleXYOrientation;
                    }
                }
            }
            if (Property.hasValue(point)) {
                if (viewport.getBoundingBox().covers(point)) {
                    point = point.convertPoint2d(viewportGeometryFactory2d);
                    return new PointDoubleXYOrientation(point, orientation);
                }
            }
        }
        return null;
    }
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) Vertex(com.revolsys.geometry.model.vertex.Vertex) PointDoubleXYOrientation(com.revolsys.geometry.model.impl.PointDoubleXYOrientation) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) Matcher(java.util.regex.Matcher) LineString(com.revolsys.geometry.model.LineString) Point(com.revolsys.geometry.model.Point) Polygon(com.revolsys.geometry.model.Polygon) Point(com.revolsys.geometry.model.Point) 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