1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.geom.Point2D;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.Objects;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
18 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
19 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
20 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
21 | import org.openstreetmap.josm.data.validation.Test;
|
---|
22 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
23 | import org.openstreetmap.josm.data.validation.util.ValUtil;
|
---|
24 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Tests if there are segments that crosses in the same layer
|
---|
28 | *
|
---|
29 | * @author frsantos
|
---|
30 | */
|
---|
31 | public abstract class CrossingWays extends Test {
|
---|
32 | protected static final int CROSSING_WAYS = 601;
|
---|
33 |
|
---|
34 | private static final String HIGHWAY = "highway";
|
---|
35 | private static final String RAILWAY = "railway";
|
---|
36 | private static final String WATERWAY = "waterway";
|
---|
37 |
|
---|
38 | /** All way segments, grouped by cells */
|
---|
39 | private final Map<Point2D, List<WaySegment>> cellSegments = new HashMap<>(1000);
|
---|
40 | /** The already detected ways in error */
|
---|
41 | private final Map<List<Way>, List<WaySegment>> seenWays = new HashMap<>(50);
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * General crossing ways test.
|
---|
45 | */
|
---|
46 | public static class Ways extends CrossingWays {
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Constructs a new crossing {@code Ways} test.
|
---|
50 | */
|
---|
51 | public Ways() {
|
---|
52 | super(tr("Crossing ways"));
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public boolean isPrimitiveUsable(OsmPrimitive w) {
|
---|
57 | return super.isPrimitiveUsable(w)
|
---|
58 | && !isProposedOrAbandoned(w)
|
---|
59 | && (isHighway(w)
|
---|
60 | || w.hasKey(WATERWAY)
|
---|
61 | || isRailway(w)
|
---|
62 | || isCoastline(w)
|
---|
63 | || isBuilding(w));
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | boolean ignoreWaySegmentCombination(Way w1, Way w2) {
|
---|
68 | if (w1 == w2)
|
---|
69 | return false;
|
---|
70 | if (!Objects.equals(getLayer(w1), getLayer(w2))) {
|
---|
71 | return true;
|
---|
72 | }
|
---|
73 | if (w1.hasKey(HIGHWAY) && w2.hasKey(HIGHWAY) && !Objects.equals(w1.get("level"), w2.get("level"))) {
|
---|
74 | return true;
|
---|
75 | }
|
---|
76 | if (isSubwayOrTramOrRazed(w2)) {
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 | if (isCoastline(w1) != isCoastline(w2)) {
|
---|
80 | return true;
|
---|
81 | }
|
---|
82 | if ((w1.hasTag(WATERWAY, "river", "stream", "canal", "drain", "ditch") && w2.hasTag(WATERWAY, "riverbank"))
|
---|
83 | || (w2.hasTag(WATERWAY, "river", "stream", "canal", "drain", "ditch") && w1.hasTag(WATERWAY, "riverbank"))) {
|
---|
84 | return true;
|
---|
85 | }
|
---|
86 | if (isProposedOrAbandoned(w2)) {
|
---|
87 | return true;
|
---|
88 | }
|
---|
89 | return false;
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | String createMessage(Way w1, Way w2) {
|
---|
94 | if (isBuilding(w1)) {
|
---|
95 | return tr("Crossing buildings");
|
---|
96 | } else if (w1.hasKey(WATERWAY) && w2.hasKey(WATERWAY)) {
|
---|
97 | return tr("Crossing waterways");
|
---|
98 | } else if ((w1.hasKey(HIGHWAY) && w2.hasKey(WATERWAY))
|
---|
99 | || (w2.hasKey(HIGHWAY) && w1.hasKey(WATERWAY))) {
|
---|
100 | return tr("Crossing waterway/highway");
|
---|
101 | } else {
|
---|
102 | return tr("Crossing ways");
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Crossing boundaries ways test.
|
---|
109 | */
|
---|
110 | public static class Boundaries extends CrossingWays {
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Constructs a new crossing {@code Boundaries} test.
|
---|
114 | */
|
---|
115 | public Boundaries() {
|
---|
116 | super(tr("Crossing boundaries"));
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public boolean isPrimitiveUsable(OsmPrimitive p) {
|
---|
121 | return super.isPrimitiveUsable(p) && p.hasKey("boundary")
|
---|
122 | && (!(p instanceof Relation) || (((Relation) p).isMultipolygon() && !((Relation) p).hasIncompleteMembers()));
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | boolean ignoreWaySegmentCombination(Way w1, Way w2) {
|
---|
127 | return !Objects.equals(w1.get("boundary"), w2.get("boundary"));
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Override
|
---|
131 | String createMessage(Way w1, Way w2) {
|
---|
132 | return tr("Crossing boundaries");
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override
|
---|
136 | public void visit(Relation r) {
|
---|
137 | for (Way w : r.getMemberPrimitives(Way.class)) {
|
---|
138 | visit(w);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Crossing barriers ways test.
|
---|
145 | */
|
---|
146 | public static class Barrier extends CrossingWays {
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Constructs a new crossing {@code Barrier} test.
|
---|
150 | */
|
---|
151 | public Barrier() {
|
---|
152 | super(tr("Crossing barriers"));
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override
|
---|
156 | public boolean isPrimitiveUsable(OsmPrimitive p) {
|
---|
157 | return super.isPrimitiveUsable(p) && p.hasKey("barrier");
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Override
|
---|
161 | boolean ignoreWaySegmentCombination(Way w1, Way w2) {
|
---|
162 | if (!Objects.equals(getLayer(w1), getLayer(w2))) {
|
---|
163 | return true;
|
---|
164 | }
|
---|
165 | return false;
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | String createMessage(Way w1, Way w2) {
|
---|
170 | return tr("Crossing barriers");
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Self crossing ways test (for all the rest)
|
---|
176 | */
|
---|
177 | public static class SelfCrossing extends CrossingWays {
|
---|
178 | CrossingWays.Ways normalTest = new Ways();
|
---|
179 | CrossingWays.Barrier barrierTest = new Barrier();
|
---|
180 | CrossingWays.Boundaries boundariesTest = new Boundaries();
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Constructs a new SelfIntersection test.
|
---|
184 | */
|
---|
185 | public SelfCrossing() {
|
---|
186 | super(tr("Self crossing"));
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Override
|
---|
190 | public boolean isPrimitiveUsable(OsmPrimitive p) {
|
---|
191 | return super.isPrimitiveUsable(p) && !(normalTest.isPrimitiveUsable(p) || barrierTest.isPrimitiveUsable(p)
|
---|
192 | || boundariesTest.isPrimitiveUsable(p));
|
---|
193 | }
|
---|
194 |
|
---|
195 | @Override
|
---|
196 | boolean ignoreWaySegmentCombination(Way w1, Way w2) {
|
---|
197 | return w1 != w2; // should not happen
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Override
|
---|
201 | String createMessage(Way w1, Way w2) {
|
---|
202 | return tr("Self-crossing ways");
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Constructs a new {@code CrossingWays} test.
|
---|
208 | * @param title The test title
|
---|
209 | * @since 6691
|
---|
210 | */
|
---|
211 | public CrossingWays(String title) {
|
---|
212 | super(title, tr("This test checks if two roads, railways, waterways or buildings crosses in the same layer, " +
|
---|
213 | "but are not connected by a node."));
|
---|
214 | }
|
---|
215 |
|
---|
216 | @Override
|
---|
217 | public void startTest(ProgressMonitor monitor) {
|
---|
218 | super.startTest(monitor);
|
---|
219 | cellSegments.clear();
|
---|
220 | seenWays.clear();
|
---|
221 | }
|
---|
222 |
|
---|
223 | @Override
|
---|
224 | public void endTest() {
|
---|
225 | super.endTest();
|
---|
226 | cellSegments.clear();
|
---|
227 | seenWays.clear();
|
---|
228 | }
|
---|
229 |
|
---|
230 | static String getLayer(OsmPrimitive w) {
|
---|
231 | String layer1 = w.get("layer");
|
---|
232 | if ("0".equals(layer1)) {
|
---|
233 | layer1 = null; // 0 is default value for layer.
|
---|
234 | }
|
---|
235 | return layer1;
|
---|
236 | }
|
---|
237 |
|
---|
238 | static boolean isCoastline(OsmPrimitive w) {
|
---|
239 | return w.hasTag("natural", "water", "coastline") || w.hasTag("landuse", "reservoir");
|
---|
240 | }
|
---|
241 |
|
---|
242 | static boolean isHighway(OsmPrimitive w) {
|
---|
243 | return w.hasTagDifferent(HIGHWAY, "rest_area", "services");
|
---|
244 | }
|
---|
245 |
|
---|
246 | static boolean isRailway(OsmPrimitive w) {
|
---|
247 | return w.hasKey(RAILWAY) && !isSubwayOrTramOrRazed(w);
|
---|
248 | }
|
---|
249 |
|
---|
250 | static boolean isSubwayOrTramOrRazed(OsmPrimitive w) {
|
---|
251 | return w.hasTag(RAILWAY, "subway", "tram", "razed");
|
---|
252 | }
|
---|
253 |
|
---|
254 | static boolean isProposedOrAbandoned(OsmPrimitive w) {
|
---|
255 | return w.hasTag(HIGHWAY, "proposed") || w.hasTag(RAILWAY, "proposed", "abandoned");
|
---|
256 | }
|
---|
257 |
|
---|
258 | abstract boolean ignoreWaySegmentCombination(Way w1, Way w2);
|
---|
259 |
|
---|
260 | abstract String createMessage(Way w1, Way w2);
|
---|
261 |
|
---|
262 | @Override
|
---|
263 | public void visit(Way w) {
|
---|
264 | if (this instanceof SelfCrossing) {
|
---|
265 | // free memory, we are not interested in previous ways
|
---|
266 | cellSegments.clear();
|
---|
267 | seenWays.clear();
|
---|
268 | }
|
---|
269 |
|
---|
270 | int nodesSize = w.getNodesCount();
|
---|
271 | for (int i = 0; i < nodesSize - 1; i++) {
|
---|
272 | final WaySegment es1 = new WaySegment(w, i);
|
---|
273 | final EastNorth en1 = es1.getFirstNode().getEastNorth();
|
---|
274 | final EastNorth en2 = es1.getSecondNode().getEastNorth();
|
---|
275 | if (en1 == null || en2 == null) {
|
---|
276 | Main.warn("Crossing ways test skipped "+es1);
|
---|
277 | continue;
|
---|
278 | }
|
---|
279 | for (List<WaySegment> segments : getSegments(cellSegments, en1, en2)) {
|
---|
280 | for (WaySegment es2 : segments) {
|
---|
281 | List<Way> prims;
|
---|
282 | List<WaySegment> highlight;
|
---|
283 |
|
---|
284 | if (!es1.intersects(es2) || ignoreWaySegmentCombination(es1.way, es2.way)) {
|
---|
285 | continue;
|
---|
286 | }
|
---|
287 |
|
---|
288 | prims = new ArrayList<>();
|
---|
289 | prims.add(es1.way);
|
---|
290 | if (es1.way != es2.way)
|
---|
291 | prims.add(es2.way);
|
---|
292 | if ((highlight = seenWays.get(prims)) == null) {
|
---|
293 | highlight = new ArrayList<>();
|
---|
294 | highlight.add(es1);
|
---|
295 | highlight.add(es2);
|
---|
296 |
|
---|
297 | final String message = createMessage(es1.way, es2.way);
|
---|
298 | errors.add(TestError.builder(this, Severity.WARNING, CROSSING_WAYS)
|
---|
299 | .message(message)
|
---|
300 | .primitives(prims)
|
---|
301 | .highlightWaySegments(highlight)
|
---|
302 | .build());
|
---|
303 | seenWays.put(prims, highlight);
|
---|
304 | } else {
|
---|
305 | highlight.add(es1);
|
---|
306 | highlight.add(es2);
|
---|
307 | }
|
---|
308 | }
|
---|
309 | segments.add(es1);
|
---|
310 | }
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Returns all the cells this segment crosses. Each cell contains the list
|
---|
316 | * of segments already processed
|
---|
317 | * @param cellSegments map with already collected way segments
|
---|
318 | * @param n1 The first EastNorth
|
---|
319 | * @param n2 The second EastNorth
|
---|
320 | * @return A list with all the cells the segment crosses
|
---|
321 | */
|
---|
322 | public static List<List<WaySegment>> getSegments(Map<Point2D, List<WaySegment>> cellSegments, EastNorth n1, EastNorth n2) {
|
---|
323 |
|
---|
324 | List<List<WaySegment>> cells = new ArrayList<>();
|
---|
325 | for (Point2D cell : ValUtil.getSegmentCells(n1, n2, OsmValidator.griddetail)) {
|
---|
326 | List<WaySegment> segments = cellSegments.get(cell);
|
---|
327 | if (segments == null) {
|
---|
328 | segments = new ArrayList<>();
|
---|
329 | cellSegments.put(cell, segments);
|
---|
330 | }
|
---|
331 | cells.add(segments);
|
---|
332 | }
|
---|
333 | return cells;
|
---|
334 | }
|
---|
335 | }
|
---|