1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
14 | import org.openstreetmap.josm.command.Command;
|
---|
15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
18 | import org.openstreetmap.josm.data.osm.Way;
|
---|
19 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
20 | import org.openstreetmap.josm.data.validation.Test;
|
---|
21 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
22 | import org.openstreetmap.josm.tools.Utils;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Test that performs semantic checks on highways.
|
---|
26 | * @since 5902
|
---|
27 | */
|
---|
28 | public class Highways extends Test {
|
---|
29 |
|
---|
30 | protected static final int WRONG_ROUNDABOUT_HIGHWAY = 2701;
|
---|
31 | protected static final int MISSING_PEDESTRIAN_CROSSING = 2702;
|
---|
32 |
|
---|
33 | protected static final List<String> CLASSIFIED_HIGHWAYS = Arrays.asList(
|
---|
34 | "motorway", "trunk", "primary", "secondary", "tertiary", "living_street", "residential", "unclassified");
|
---|
35 |
|
---|
36 | boolean leftByPedestrians = false;
|
---|
37 | boolean leftByCyclists = false;
|
---|
38 | boolean leftByCars = false;
|
---|
39 | int pedestrianWays = 0;
|
---|
40 | int cyclistWays = 0;
|
---|
41 | int carsWays = 0;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new {@code Highways} test.
|
---|
45 | */
|
---|
46 | public Highways() {
|
---|
47 | super(tr("Highways"), tr("Performs semantic checks on highways."));
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected class WrongRoundaboutHighway extends TestError {
|
---|
51 |
|
---|
52 | public final String correctValue;
|
---|
53 |
|
---|
54 | public WrongRoundaboutHighway(Way w, String key) {
|
---|
55 | super(Highways.this, Severity.WARNING,
|
---|
56 | tr("Incorrect roundabout (highway: {0} instead of {1})", w.get("highway"), key),
|
---|
57 | WRONG_ROUNDABOUT_HIGHWAY, w);
|
---|
58 | this.correctValue = key;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public void visit(Node n) {
|
---|
64 | if (n.isUsable() && !n.hasTag("highway", "crossing") && !n.hasTag("crossing", "no") && n.isReferredByWays(2)) {
|
---|
65 | testMissingPedestrianCrossing(n);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public void visit(Way w) {
|
---|
71 | if (w.isUsable() && w.hasKey("highway") && w.hasKey("junction") && w.get("junction").equals("roundabout")) {
|
---|
72 | testWrongRoundabout(w);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void testWrongRoundabout(Way w) {
|
---|
77 | Map<String, List<Way>> map = new HashMap<String, List<Way>>();
|
---|
78 | // Count all highways (per type) connected to this roundabout
|
---|
79 | // As roundabouts are closed ways, take care of not processing the first/last node twice
|
---|
80 | for (Node n : new HashSet<Node>(w.getNodes())) {
|
---|
81 | for (Way h : Utils.filteredCollection(n.getReferrers(), Way.class)) {
|
---|
82 | if (h != w && h.hasKey("highway")) {
|
---|
83 | List<Way> list = map.get(h.get("highway"));
|
---|
84 | if (list == null) {
|
---|
85 | map.put(h.get("highway"), list = new ArrayList<Way>());
|
---|
86 | }
|
---|
87 | list.add(h);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | // The roundabout should carry the highway tag of its two biggest highways
|
---|
92 | for (String s : CLASSIFIED_HIGHWAYS) {
|
---|
93 | List<Way> list = map.get(s);
|
---|
94 | if (list != null && list.size() >= 2) {
|
---|
95 | // Except when a single road is connected, but with two oneway segments
|
---|
96 | Boolean oneway1 = OsmUtils.getOsmBoolean(list.get(0).get("oneway"));
|
---|
97 | Boolean oneway2 = OsmUtils.getOsmBoolean(list.get(1).get("oneway"));
|
---|
98 | if (list.size() > 2 || oneway1 == null || oneway2 == null || !oneway1 || !oneway2) {
|
---|
99 | // Error when the highway tags do not match
|
---|
100 | if (!w.get("highway").equals(s)) {
|
---|
101 | errors.add(new WrongRoundaboutHighway(w, tr("Incorrect roundabout (highway: {0} instead of {1})", w.get("highway"), s)));
|
---|
102 | }
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void testMissingPedestrianCrossing(Node n) {
|
---|
110 | leftByPedestrians = false;
|
---|
111 | leftByCyclists = false;
|
---|
112 | leftByCars = false;
|
---|
113 | pedestrianWays = 0;
|
---|
114 | cyclistWays = 0;
|
---|
115 | carsWays = 0;
|
---|
116 |
|
---|
117 | for (Way w : OsmPrimitive.getFilteredList(n.getReferrers(), Way.class)) {
|
---|
118 | String highway = w.get("highway");
|
---|
119 | if (highway != null) {
|
---|
120 | if (highway.equals("footway") || highway.equals("path")) {
|
---|
121 | handlePedestrianWay(n, w);
|
---|
122 | if (w.hasTag("bicycle", "yes", "designated")) {
|
---|
123 | handleCyclistWay(n, w);
|
---|
124 | }
|
---|
125 | } else if (highway.equals("cycleway")) {
|
---|
126 | handleCyclistWay(n, w);
|
---|
127 | if (w.hasTag("foot", "yes", "designated")) {
|
---|
128 | handlePedestrianWay(n, w);
|
---|
129 | }
|
---|
130 | } else if (CLASSIFIED_HIGHWAYS.contains(highway)) {
|
---|
131 | // Only look at classified highways for now:
|
---|
132 | // - service highways support is TBD (see #9141 comments)
|
---|
133 | // - roads should be determined first. Another warning is raised anyway
|
---|
134 | handleCarWay(n, w);
|
---|
135 | }
|
---|
136 | if ((leftByPedestrians || leftByCyclists) && leftByCars) {
|
---|
137 | errors.add(new TestError(this, Severity.WARNING, tr("Missing crossing information"), MISSING_PEDESTRIAN_CROSSING, n));
|
---|
138 | return;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void handleCarWay(Node n, Way w) {
|
---|
145 | carsWays++;
|
---|
146 | if (!w.isFirstLastNode(n) || carsWays > 1) {
|
---|
147 | leftByCars = true;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | private void handleCyclistWay(Node n, Way w) {
|
---|
152 | cyclistWays++;
|
---|
153 | if (!w.isFirstLastNode(n) || cyclistWays > 1) {
|
---|
154 | leftByCyclists = true;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | private void handlePedestrianWay(Node n, Way w) {
|
---|
159 | pedestrianWays++;
|
---|
160 | if (!w.isFirstLastNode(n) || pedestrianWays > 1) {
|
---|
161 | leftByPedestrians = true;
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Override
|
---|
166 | public boolean isFixable(TestError testError) {
|
---|
167 | return testError instanceof WrongRoundaboutHighway;
|
---|
168 | }
|
---|
169 |
|
---|
170 | @Override
|
---|
171 | public Command fixError(TestError testError) {
|
---|
172 | if (testError instanceof WrongRoundaboutHighway) {
|
---|
173 | return new ChangePropertyCommand(testError.getPrimitives().iterator().next(),
|
---|
174 | "highway", ((WrongRoundaboutHighway) testError).correctValue);
|
---|
175 | }
|
---|
176 | return null;
|
---|
177 | }
|
---|
178 | }
|
---|