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;
}
}
Aggregations