1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.data.validation.tests.CrossingWays.HIGHWAY;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.osm.Node;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
14 | import org.openstreetmap.josm.data.validation.Test;
|
---|
15 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
16 | import org.openstreetmap.josm.gui.mappaint.ElemStyles;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Checks for ways connected to areas.
|
---|
20 | * @since 4682
|
---|
21 | */
|
---|
22 | public class WayConnectedToArea extends Test {
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Constructs a new {@code WayConnectedToArea} test.
|
---|
26 | */
|
---|
27 | public WayConnectedToArea() {
|
---|
28 | super(tr("Way connected to Area"), tr("Checks for ways connected to areas."));
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public void visit(Way w) {
|
---|
33 | if (!w.isUsable() || w.isClosed() || !w.hasKey(HIGHWAY)) {
|
---|
34 | return;
|
---|
35 | }
|
---|
36 |
|
---|
37 | boolean hasway = false;
|
---|
38 | List<OsmPrimitive> r = w.firstNode().getReferrers();
|
---|
39 | for (OsmPrimitive p : r) {
|
---|
40 | if (p != w && p.hasKey(HIGHWAY)) {
|
---|
41 | hasway = true;
|
---|
42 | break;
|
---|
43 | }
|
---|
44 | }
|
---|
45 | if (!hasway) {
|
---|
46 | for (OsmPrimitive p : r) {
|
---|
47 | testForError(w, w.firstNode(), p);
|
---|
48 | }
|
---|
49 | }
|
---|
50 | hasway = false;
|
---|
51 | r = w.lastNode().getReferrers();
|
---|
52 | for (OsmPrimitive p : r) {
|
---|
53 | if (p != w && p.hasKey(HIGHWAY)) {
|
---|
54 | hasway = true;
|
---|
55 | break;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | if (!hasway) {
|
---|
59 | for (OsmPrimitive p : r) {
|
---|
60 | testForError(w, w.lastNode(), p);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void testForError(Way w, Node wayNode, OsmPrimitive p) {
|
---|
66 | if (wayNode.isOutsideDownloadArea()
|
---|
67 | || wayNode.getReferrers().stream().anyMatch(p1 -> p1.hasTag("route", "ferry"))) {
|
---|
68 | return;
|
---|
69 | } else if (isArea(p)) {
|
---|
70 | addPossibleError(w, wayNode, p, p);
|
---|
71 | } else {
|
---|
72 | for (OsmPrimitive r : p.getReferrers()) {
|
---|
73 | if (r instanceof Relation
|
---|
74 | && r.hasTag("type", "multipolygon")
|
---|
75 | && isArea(r)) {
|
---|
76 | addPossibleError(w, wayNode, p, r);
|
---|
77 | break;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | private static boolean isArea(OsmPrimitive p) {
|
---|
84 | return p.hasKey("landuse", "natural") && ElemStyles.hasAreaElemStyle(p, false);
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void addPossibleError(Way w, Node wayNode, OsmPrimitive p, OsmPrimitive area) {
|
---|
88 | // Avoid "legal" cases (see #10655)
|
---|
89 | if (w.hasKey(HIGHWAY) && wayNode.hasTag("leisure", "slipway") && area.hasTag("natural", "water")) {
|
---|
90 | return;
|
---|
91 | }
|
---|
92 | errors.add(TestError.builder(this, Severity.WARNING, 2301)
|
---|
93 | .message(tr("Way terminates on Area"))
|
---|
94 | .primitives(w, p)
|
---|
95 | .highlight(wayNode)
|
---|
96 | .build());
|
---|
97 | }
|
---|
98 | }
|
---|