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.HashSet;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.Set;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.command.Command;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
14 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
16 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
17 | import org.openstreetmap.josm.data.validation.Test;
|
---|
18 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Checks for untagged ways
|
---|
23 | *
|
---|
24 | * @author frsantos
|
---|
25 | */
|
---|
26 | public class UntaggedWay extends Test
|
---|
27 | {
|
---|
28 | /** Empty way error */
|
---|
29 | protected static final int EMPTY_WAY = 301;
|
---|
30 | /** Untagged way error */
|
---|
31 | protected static final int UNTAGGED_WAY = 302;
|
---|
32 | /** Unnamed way error */
|
---|
33 | protected static final int UNNAMED_WAY = 303;
|
---|
34 | /** One node way error */
|
---|
35 | protected static final int ONE_NODE_WAY = 304;
|
---|
36 | /** Unnamed junction error */
|
---|
37 | protected static final int UNNAMED_JUNCTION = 305;
|
---|
38 | /** Untagged, but commented way error */
|
---|
39 | protected static final int COMMENTED_WAY = 306;
|
---|
40 |
|
---|
41 | private Set<Way> waysUsedInRelations;
|
---|
42 |
|
---|
43 | /** Ways that must have a name */
|
---|
44 | public static final Set<String> NAMED_WAYS = new HashSet<String>();
|
---|
45 | static {
|
---|
46 | NAMED_WAYS.add( "motorway" );
|
---|
47 | NAMED_WAYS.add( "trunk" );
|
---|
48 | NAMED_WAYS.add( "primary" );
|
---|
49 | NAMED_WAYS.add( "secondary" );
|
---|
50 | NAMED_WAYS.add( "tertiary" );
|
---|
51 | NAMED_WAYS.add( "residential" );
|
---|
52 | NAMED_WAYS.add( "pedestrian" ); ;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /** Whitelist of roles allowed to reference an untagged way */
|
---|
56 | public static final Set<String> WHITELIST = new HashSet<String>();
|
---|
57 | static {
|
---|
58 | WHITELIST.add( "outer" );
|
---|
59 | WHITELIST.add( "inner" );
|
---|
60 | WHITELIST.add( "perimeter" );
|
---|
61 | WHITELIST.add( "edge" );
|
---|
62 | WHITELIST.add( "outline" );
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Constructor
|
---|
67 | */
|
---|
68 | public UntaggedWay() {
|
---|
69 | super(tr("Untagged, empty and one node ways"),
|
---|
70 | tr("This test checks for untagged, empty and one node ways."));
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public void visit(Way w) {
|
---|
75 | if (!w.isUsable())
|
---|
76 | return;
|
---|
77 |
|
---|
78 | Map<String, String> tags = w.getKeys();
|
---|
79 | if (!tags.isEmpty()) {
|
---|
80 | String highway = tags.get("highway");
|
---|
81 | if (highway != null && NAMED_WAYS.contains(highway)) {
|
---|
82 | if (!tags.containsKey("name") && !tags.containsKey("ref")) {
|
---|
83 | boolean isRoundabout = false;
|
---|
84 | boolean hasName = false;
|
---|
85 | for (String key : w.keySet()) {
|
---|
86 | hasName = key.startsWith("name:") || key.endsWith("_name") || key.endsWith("_ref");
|
---|
87 | if (hasName) {
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | if (key.equals("junction")) {
|
---|
91 | isRoundabout = w.get("junction").equals("roundabout");
|
---|
92 | break;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (!hasName && !isRoundabout) {
|
---|
97 | errors.add(new TestError(this, Severity.WARNING, tr("Unnamed ways"), UNNAMED_WAY, w));
|
---|
98 | } else if (isRoundabout) {
|
---|
99 | errors.add(new TestError(this, Severity.WARNING, tr("Unnamed junction"), UNNAMED_JUNCTION, w));
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (!w.isTagged() && !waysUsedInRelations.contains(w)) {
|
---|
106 | if (w.hasKeys()) {
|
---|
107 | errors.add(new TestError(this, Severity.WARNING, tr("Untagged ways (commented)"), COMMENTED_WAY, w));
|
---|
108 | } else {
|
---|
109 | errors.add(new TestError(this, Severity.WARNING, tr("Untagged ways"), UNTAGGED_WAY, w));
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (w.getNodesCount() == 0) {
|
---|
114 | errors.add(new TestError(this, Severity.ERROR, tr("Empty ways"), EMPTY_WAY, w));
|
---|
115 | } else if (w.getNodesCount() == 1) {
|
---|
116 | errors.add(new TestError(this, Severity.ERROR, tr("One node ways"), ONE_NODE_WAY, w));
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public void startTest(ProgressMonitor monitor) {
|
---|
122 | super.startTest(monitor);
|
---|
123 | waysUsedInRelations = new HashSet<Way>();
|
---|
124 | for (Relation r : Main.main.getCurrentDataSet().getRelations()) {
|
---|
125 | if (r.isUsable()) {
|
---|
126 | for (RelationMember m : r.getMembers()) {
|
---|
127 | if (r.isMultipolygon() || WHITELIST.contains(m.getRole())) {
|
---|
128 | OsmPrimitive member = m.getMember();
|
---|
129 | if (member != null && member instanceof Way && member.isUsable() && !member.isTagged()) {
|
---|
130 | waysUsedInRelations.add((Way)member);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | public void endTest() {
|
---|
140 | waysUsedInRelations = null;
|
---|
141 | super.endTest();
|
---|
142 | }
|
---|
143 |
|
---|
144 | @Override
|
---|
145 | public boolean isFixable(TestError testError) {
|
---|
146 | if (testError.getTester() instanceof UntaggedWay)
|
---|
147 | return testError.getCode() == EMPTY_WAY
|
---|
148 | || testError.getCode() == ONE_NODE_WAY;
|
---|
149 |
|
---|
150 | return false;
|
---|
151 | }
|
---|
152 |
|
---|
153 | @Override
|
---|
154 | public Command fixError(TestError testError) {
|
---|
155 | return deletePrimitivesIfNeeded(testError.getPrimitives());
|
---|
156 | }
|
---|
157 | }
|
---|