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

Last change on this file since 17491 was 17100, checked in by GerdP, 4 years ago

fix #19869: test SharpAngles cannot cope with read-only datasets.
Don't use WaySegment.toWay() to calculate segment length as it adds a parent Way to existing nodes

File size: 5.6 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 final 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 d1 = ws1.getFirstNode().getEastNorth().distance(ws1.getSecondNode().getEastNorth());
108 double d2 = ws2.getFirstNode().getEastNorth().distance(ws2.getSecondNode().getEastNorth());
109 double shorterLen = Math.min(d1, d2);
110 if (shorterLen < maxLength) {
111 createNearlyOverlappingError(angle, way, pointNode);
112 }
113 }
114
115 private void createNearlyOverlappingError(double angle, Way way, OsmPrimitive primitive) {
116 Severity severity = getSeverity(angle);
117 if (severity != Severity.OTHER || (ValidatorPrefHelper.PREF_OTHER.get() || ValidatorPrefHelper.PREF_OTHER_UPLOAD.get())) {
118 int addCode = severity == Severity.OTHER ? 1 : 0;
119 TestError.Builder testError = TestError.builder(this, severity, SHARP_ANGLES + addCode)
120 .primitives(way)
121 .highlight(primitive)
122 .message(tr("Sharp angle"));
123 errors.add(testError.build());
124 }
125 }
126
127 private Severity getSeverity(double angle) {
128 return angle < maxAngle * 2 / 3 ? Severity.WARNING : Severity.OTHER;
129 }
130
131 /**
132 * Set the maximum length for the shortest segment
133 * @param length The max length in meters
134 */
135 public void setMaxLength(double length) {
136 maxLength = length;
137 }
138
139 /**
140 * Add a highway to ignore
141 * @param highway The highway type to ignore (e.g., if you want to ignore residential roads, use "residential")
142 */
143 public void addIgnoredHighway(String highway) {
144 ignoreHighways.add(highway);
145 }
146
147 /**
148 * Set the maximum angle
149 * @param angle The maximum angle in degrees.
150 */
151 public void setMaxAngle(double angle) {
152 maxAngle = angle;
153 }
154
155}
Note: See TracBrowser for help on using the repository browser.