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.OsmUtils;
|
---|
17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
18 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
19 | import org.openstreetmap.josm.data.validation.Test;
|
---|
20 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
21 | import org.openstreetmap.josm.tools.Utils;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Test that performs semantic checks on highways.
|
---|
25 | * @since 5902
|
---|
26 | */
|
---|
27 | public class Highways extends Test {
|
---|
28 |
|
---|
29 | protected static final int WRONG_ROUNDABOUT_HIGHWAY = 2701;
|
---|
30 |
|
---|
31 | protected static final List<String> CLASSIFIED_HIGHWAYS = Arrays.asList(
|
---|
32 | "motorway", "trunk", "primary", "secondary", "tertiary", "living_street", "residential", "unclassified");
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Constructs a new {@code Highways} test.
|
---|
36 | */
|
---|
37 | public Highways() {
|
---|
38 | super(tr("Highways"), tr("Performs semantic checks on highways."));
|
---|
39 | }
|
---|
40 |
|
---|
41 | protected class WrongRoundaboutHighway extends TestError {
|
---|
42 |
|
---|
43 | public final String correctValue;
|
---|
44 |
|
---|
45 | public WrongRoundaboutHighway(Way w, String key) {
|
---|
46 | super(Highways.this, Severity.WARNING,
|
---|
47 | tr("Incorrect roundabout (highway: {0} instead of {1})", w.get("highway"), key),
|
---|
48 | WRONG_ROUNDABOUT_HIGHWAY, w);
|
---|
49 | this.correctValue = key;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public void visit(Way w) {
|
---|
55 | if (w.isUsable()) {
|
---|
56 | if (w.hasKey("highway") && w.hasKey("junction") && w.get("junction").equals("roundabout")) {
|
---|
57 | Map<String, List<Way>> map = new HashMap<String, List<Way>>();
|
---|
58 | // Count all highways (per type) connected to this roundabout
|
---|
59 | // As roundabouts are closed ways, take care of not processing the first/last node twice
|
---|
60 | for (Node n : new HashSet<Node>(w.getNodes())) {
|
---|
61 | for (Way h : Utils.filteredCollection(n.getReferrers(), Way.class)) {
|
---|
62 | if (h != w && h.hasKey("highway")) {
|
---|
63 | List<Way> list = map.get(h.get("highway"));
|
---|
64 | if (list == null) {
|
---|
65 | map.put(h.get("highway"), list = new ArrayList<Way>());
|
---|
66 | }
|
---|
67 | list.add(h);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | // The roundabout should carry the highway tag of its two biggest highways
|
---|
72 | for (String s : CLASSIFIED_HIGHWAYS) {
|
---|
73 | List<Way> list = map.get(s);
|
---|
74 | if (list != null && list.size() >= 2) {
|
---|
75 | // Except when a single road is connected, but with two oneway segments
|
---|
76 | Boolean oneway1 = OsmUtils.getOsmBoolean(list.get(0).get("oneway"));
|
---|
77 | Boolean oneway2 = OsmUtils.getOsmBoolean(list.get(1).get("oneway"));
|
---|
78 | if (list.size() > 2 || oneway1 == null || oneway2 == null || !oneway1 || !oneway2) {
|
---|
79 | // Error when the highway tags do not match
|
---|
80 | if (!w.get("highway").equals(s)) {
|
---|
81 | errors.add(new TestError(this, Severity.WARNING,
|
---|
82 | tr("Incorrect roundabout (highway: {0} instead of {1})", w.get("highway"), s),
|
---|
83 | WRONG_ROUNDABOUT_HIGHWAY, w));
|
---|
84 | }
|
---|
85 | break;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public boolean isFixable(TestError testError) {
|
---|
95 | return testError instanceof WrongRoundaboutHighway;
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public Command fixError(TestError testError) {
|
---|
100 | if (testError instanceof WrongRoundaboutHighway) {
|
---|
101 | return new ChangePropertyCommand(testError.getPrimitives().iterator().next(),
|
---|
102 | "highway", ((WrongRoundaboutHighway) testError).correctValue);
|
---|
103 | }
|
---|
104 | return null;
|
---|
105 | }
|
---|
106 | }
|
---|