source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/SharpAngles.java@ 16220

Last change on this file since 16220 was 15463, checked in by GerdP, 5 years ago

see #18232: remove Test.compareTo() and more

File size: 5.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Arrays;
7import java.util.Collection;
8import java.util.TreeSet;
9
10import org.openstreetmap.josm.data.coor.EastNorth;
11import org.openstreetmap.josm.data.osm.Node;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.Way;
14import org.openstreetmap.josm.data.osm.WaySegment;
15import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
16import org.openstreetmap.josm.data.validation.Severity;
17import org.openstreetmap.josm.data.validation.Test;
18import org.openstreetmap.josm.data.validation.TestError;
19import org.openstreetmap.josm.tools.Geometry;
20import org.openstreetmap.josm.tools.bugreport.BugReport;
21
22/**
23 * Find highways that have sharp angles
24 * @author Taylor Smock
25 * @since 15406
26 */
27public class SharpAngles extends Test {
28 private static final int SHARPANGLESCODE = 3800;
29 /** The code for a sharp angle */
30 private static final int SHARP_ANGLES = SHARPANGLESCODE + 0;
31 /** The maximum angle for sharp angles */
32 private double maxAngle = 45.0; // degrees
33 /** The length that at least one way segment must be shorter than */
34 private double maxLength = 10.0; // meters
35 /** Specific highway types to ignore */
36 private Collection<String> ignoreHighways = new TreeSet<>(
37 Arrays.asList("platform", "rest_area", "services", "via_ferrata"));
38
39 /**
40 * Construct a new {@code IntersectionIssues} object
41 */
42 public SharpAngles() {
43 super(tr("Sharp angles"), tr("Check for sharp angles on roads"));
44 }
45
46 @Override
47 public void visit(Way way) {
48 if (!way.isUsable()) return;
49 if (shouldBeTestedForSharpAngles(way)) {
50 try {
51 checkWayForSharpAngles(way);
52 } catch (RuntimeException e) {
53 throw BugReport.intercept(e).put("way", way);
54 }
55 }
56 }
57
58 /**
59 * Check whether or not a way should be checked for sharp angles
60 * @param way The way that needs to be checked
61 * @return {@code true} if the way should be checked.
62 */
63 public boolean shouldBeTestedForSharpAngles(Way way) {
64 return (way.hasKey("highway") && !way.hasTag("area", "yes") && !way.hasKey("via_ferrata_scale") &&
65 !ignoreHighways.contains(way.get("highway")));
66 }
67
68 /**
69 * Check nodes in a way for sharp angles
70 * @param way A way to check for sharp angles
71 */
72 public void checkWayForSharpAngles(Way way) {
73 Node node1 = null;
74 Node node2 = null;
75 Node node3 = null;
76 int i = -2;
77 for (Node node : way.getNodes()) {
78 node1 = node2;
79 node2 = node3;
80 node3 = node;
81 checkAngle(node1, node2, node3, i, way, false);
82 i++;
83 }
84 if (way.isClosed() && way.getNodesCount() > 2) {
85 node1 = node2;
86 node2 = node3;
87 // Get the second node, not the first node, since a closed way has first node == last node
88 node3 = way.getNode(1);
89 checkAngle(node1, node2, node3, i, way, true);
90 }
91 }
92
93 private void checkAngle(Node node1, Node node2, Node node3, int i, Way way, boolean last) {
94 if (node1 == null || node2 == null || node3 == null) return;
95 EastNorth n1 = node1.getEastNorth();
96 EastNorth n2 = node2.getEastNorth();
97 EastNorth n3 = node3.getEastNorth();
98 double angle = Math.toDegrees(Math.abs(Geometry.getCornerAngle(n1, n2, n3)));
99 if (angle < maxAngle) {
100 processSharpAngleForErrorCreation(angle, i, way, last, node2);
101 }
102 }
103
104 private void processSharpAngleForErrorCreation(double angle, int i, Way way, boolean last, Node pointNode) {
105 WaySegment ws1 = new WaySegment(way, i);
106 WaySegment ws2 = new WaySegment(way, last ? 0 : i + 1);
107 double shorterLen = Math.min(ws1.toWay().getLength(), ws2.toWay().getLength());
108 if (shorterLen < maxLength) {
109 createNearlyOverlappingError(angle, way, pointNode);
110 }
111 }
112
113 private void createNearlyOverlappingError(double angle, Way way, OsmPrimitive primitive) {
114 Severity severity = getSeverity(angle);
115 if (severity != Severity.OTHER || (ValidatorPrefHelper.PREF_OTHER.get() || ValidatorPrefHelper.PREF_OTHER_UPLOAD.get())) {
116 int addCode = severity == Severity.OTHER ? 1 : 0;
117 TestError.Builder testError = TestError.builder(this, severity, SHARP_ANGLES + addCode)
118 .primitives(way)
119 .highlight(primitive)
120 .message(tr("Sharp angle"));
121 errors.add(testError.build());
122 }
123 }
124
125 private Severity getSeverity(double angle) {
126 return angle < maxAngle * 2 / 3 ? Severity.WARNING : Severity.OTHER;
127 }
128
129 /**
130 * Set the maximum length for the shortest segment
131 * @param length The max length in meters
132 */
133 public void setMaxLength(double length) {
134 maxLength = length;
135 }
136
137 /**
138 * Add a highway to ignore
139 * @param highway The highway type to ignore (e.g., if you want to ignore residential roads, use "residential")
140 */
141 public void addIgnoredHighway(String highway) {
142 ignoreHighways.add(highway);
143 }
144
145 /**
146 * Set the maximum angle
147 * @param angle The maximum angle in degrees.
148 */
149 public void setMaxAngle(double angle) {
150 maxAngle = angle;
151 }
152
153}
Note: See TracBrowser for help on using the repository browser.