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.util.Arrays;
|
---|
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 | {
|
---|
47 | for (OsmPrimitive p : r) {
|
---|
48 | testForError(w, w.firstNode(), p);
|
---|
49 | }
|
---|
50 | }
|
---|
51 | hasway = false;
|
---|
52 | r = w.lastNode().getReferrers();
|
---|
53 | for (OsmPrimitive p : r) {
|
---|
54 | if(p != w && p.hasKey("highway")) {
|
---|
55 | hasway = true;
|
---|
56 | break;
|
---|
57 | }
|
---|
58 | }
|
---|
59 | if(!hasway) {
|
---|
60 | for (OsmPrimitive p : r) {
|
---|
61 | testForError(w, w.lastNode(), p);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | private void testForError(Way w, Node wayNode, OsmPrimitive p) {
|
---|
67 | if (isArea(p)) {
|
---|
68 | addError(w, wayNode, p);
|
---|
69 | } else {
|
---|
70 | for (OsmPrimitive r : p.getReferrers()) {
|
---|
71 | if (r instanceof Relation
|
---|
72 | && r.hasTag("type", "multipolygon")
|
---|
73 | && isArea(r)) {
|
---|
74 | addError(w, wayNode, p);
|
---|
75 | break;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | private boolean isArea(OsmPrimitive p) {
|
---|
82 | return (p.hasKey("landuse") || p.hasKey("natural"))
|
---|
83 | && ElemStyles.hasAreaElemStyle(p, false);
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void addError(Way w, Node wayNode, OsmPrimitive p) {
|
---|
87 | errors.add(new TestError(this, Severity.WARNING,
|
---|
88 | tr("Way terminates on Area"), 2301,
|
---|
89 | Arrays.asList(w, p),
|
---|
90 | Arrays.asList(wayNode)));
|
---|
91 | }
|
---|
92 | }
|
---|