source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/DeprecatedTags.java@ 6238

Last change on this file since 6238 was 6238, checked in by Don-vip, 11 years ago

fix #9087 - deprecated fix for barrier=wire_fence created deprecated fence_type=chain

File size: 8.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collection;
7import java.util.LinkedList;
8import java.util.List;
9
10import org.openstreetmap.josm.command.ChangePropertyCommand;
11import org.openstreetmap.josm.command.Command;
12import org.openstreetmap.josm.command.SequenceCommand;
13import org.openstreetmap.josm.data.osm.Node;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.Relation;
16import org.openstreetmap.josm.data.osm.Tag;
17import org.openstreetmap.josm.data.osm.Way;
18import org.openstreetmap.josm.data.validation.Severity;
19import org.openstreetmap.josm.data.validation.Test;
20import org.openstreetmap.josm.data.validation.TestError;
21import org.openstreetmap.josm.tools.Utils;
22
23/**
24 * Checks and corrects deprecated tags.
25 * @since 4442
26 */
27public class DeprecatedTags extends Test {
28
29 private List<DeprecationCheck> checks = new LinkedList<DeprecationCheck>();
30
31 /**
32 * Constructs a new {@code DeprecatedTags} test.
33 */
34 public DeprecatedTags() {
35 super(tr("Deprecated Tags"), tr("Checks and corrects deprecated tags."));
36 checks.add(new DeprecationCheck(2101).
37 testAndRemove("barrier", "wire_fence").
38 add("barrier", "fence").
39 add("fence_type", "chain_link"));
40 checks.add(new DeprecationCheck(2102).
41 testAndRemove("barrier", "wood_fence").
42 add("barrier", "fence").
43 add("fence_type", "wood"));
44 checks.add(new DeprecationCheck(2103).
45 testAndRemove("highway", "ford").
46 add("ford", "yes"));
47 // from http://wiki.openstreetmap.org/wiki/Deprecated_features
48 checks.add(new DeprecationCheck(2104).
49 test("class").
50 alternative("highway"));
51 checks.add(new DeprecationCheck(2105).
52 testAndRemove("highway", "stile").
53 add("barrier", "stile"));
54 checks.add(new DeprecationCheck(2106).
55 testAndRemove("highway", "incline").
56 add("highway", "road").
57 add("incline", "up"));
58 checks.add(new DeprecationCheck(2107).
59 testAndRemove("highway", "incline_steep").
60 add("highway", "road").
61 add("incline", "up"));
62 checks.add(new DeprecationCheck(2108).
63 testAndRemove("highway", "unsurfaced").
64 add("highway", "road").
65 add("incline", "unpaved"));
66 checks.add(new DeprecationCheck(2109).
67 test("landuse", "wood").
68 alternative("landuse", "forest").
69 alternative("natural", "wood"));
70 checks.add(new DeprecationCheck(2110).
71 testAndRemove("natural", "marsh").
72 add("natural", "wetland").
73 add("wetland", "marsh"));
74 checks.add(new DeprecationCheck(2111).
75 test("highway", "byway"));
76 checks.add(new DeprecationCheck(2112).
77 test("power_source").
78 alternative("generator:source"));
79 checks.add(new DeprecationCheck(2113).
80 test("power_rating").
81 alternative("generator:output"));
82 // from http://wiki.openstreetmap.org/wiki/Tag:shop=organic
83 checks.add(new DeprecationCheck(2114).
84 testAndRemove("shop", "organic").
85 add("shop", "supermarket").
86 add("organic", "only"));
87 // from http://wiki.openstreetmap.org/wiki/Key:bicycle_parking
88 checks.add(new DeprecationCheck(2115).
89 testAndRemove("bicycle_parking", "sheffield").
90 add("bicycle_parking", "stands"));
91 // http://wiki.openstreetmap.org/wiki/Tag:emergency=phone
92 checks.add(new DeprecationCheck(2116).
93 testAndRemove("amenity", "emergency_phone").
94 add("emergency", "phone"));
95 // http://wiki.openstreetmap.org/wiki/Tag:sport=gaelic_football
96 // fix #8132
97 checks.add(new DeprecationCheck(2117).
98 testAndRemove("sport", "gaelic_football").
99 add("sport", "gaelic_games"));
100 // http://wiki.openstreetmap.org/wiki/Tag:power=station
101 // see #8847 / #8961
102 checks.add(new DeprecationCheck(2118).
103 test("power", "station").
104 alternative("power", "plant").
105 alternative("power", "sub_station"));
106 checks.add(new DeprecationCheck(2119).
107 testAndRemove("generator:method", "dam").
108 add("generator:method", "water-storage"));
109 checks.add(new DeprecationCheck(2120).
110 testAndRemove("generator:method", "pumped-storage").
111 add("generator:method", "water-pumped-storage"));
112 checks.add(new DeprecationCheck(2121).
113 testAndRemove("generator:method", "pumping").
114 add("generator:method", "water-pumped-storage"));
115 // http://wiki.openstreetmap.org/wiki/Key:fence_type
116 // see #8962
117 checks.add(new DeprecationCheck(2122).
118 test("fence_type", "chain").
119 alternative("barrier", "chain").
120 alternative("fence_type", "chain_link"));
121 // http://wiki.openstreetmap.org/wiki/Key:entrance
122 // see #9000
123 checks.add(new DeprecationCheck(2123).
124 test("building", "entrance").
125 alternative("entrance"));
126 }
127
128 /**
129 * Visiting call for primitives.
130 * @param p The primitive to inspect.
131 */
132 public void visit(OsmPrimitive p) {
133 for (DeprecationCheck check : checks) {
134 if (check.matchesPrimitive(p)) {
135 errors.add(new DeprecationError(p, check));
136 }
137 }
138 }
139
140 @Override
141 public void visit(Node n) {
142 visit((OsmPrimitive) n);
143 }
144
145 @Override
146 public void visit(Way w) {
147 visit((OsmPrimitive) w);
148 }
149
150 @Override
151 public void visit(Relation r) {
152 visit((OsmPrimitive) r);
153 }
154
155 private static class DeprecationCheck {
156
157 int code;
158 List<Tag> test = new LinkedList<Tag>();
159 List<Tag> change = new LinkedList<Tag>();
160 List<Tag> alternatives = new LinkedList<Tag>();
161
162 public DeprecationCheck(int code) {
163 this.code = code;
164 }
165
166 DeprecationCheck test(String key, String value) {
167 test.add(new Tag(key, value));
168 return this;
169 }
170
171 DeprecationCheck test(String key) {
172 return test(key, null);
173 }
174
175 DeprecationCheck add(String key, String value) {
176 change.add(new Tag(key, value));
177 return this;
178 }
179
180 DeprecationCheck remove(String key) {
181 change.add(new Tag(key));
182 return this;
183 }
184
185 DeprecationCheck testAndRemove(String key, String value) {
186 return test(key, value).remove(key);
187 }
188/*
189 DeprecationCheck testAndRemove(String key) {
190 return test(key).remove(key);
191 }
192*/
193 DeprecationCheck alternative(String key, String value) {
194 alternatives.add(new Tag(key, value));
195 return this;
196 }
197
198 DeprecationCheck alternative(String key) {
199 return alternative(key, null);
200 }
201
202 boolean matchesPrimitive(OsmPrimitive p) {
203 for (Tag tag : test) {
204 String key = tag.getKey();
205 String value = tag.getValue();
206 if (value.isEmpty() && !p.hasKey(key))
207 return false;
208 if (!value.isEmpty() && !value.equals(p.get(key)))
209 return false;
210 }
211 return true;
212 }
213
214 Command fixPrimitive(OsmPrimitive p) {
215 Collection<Command> cmds = new LinkedList<Command>();
216 for (Tag tag : change) {
217 cmds.add(new ChangePropertyCommand(p, tag.getKey(), tag.getValue()));
218 }
219 return new SequenceCommand(tr("Deprecation fix of {0}", Utils.join(", ", test)), cmds);
220 }
221
222 String getDescription() {
223 if (alternatives.isEmpty())
224 return tr("{0} is deprecated", Utils.join(", ", test));
225 else
226 return tr("{0} is deprecated, use {1} instead", Utils.join(", ", test), Utils.join(tr(" or "), alternatives));
227 }
228 }
229
230 private class DeprecationError extends TestError {
231
232 OsmPrimitive p;
233 DeprecationCheck check;
234
235 DeprecationError(OsmPrimitive p, DeprecationCheck check) {
236 super(DeprecatedTags.this, Severity.WARNING, check.getDescription(), check.code, p);
237 this.p = p;
238 this.check = check;
239 }
240
241 @Override
242 public boolean isFixable() {
243 return !check.change.isEmpty();
244 }
245
246 @Override
247 public Command getFix() {
248 return check.fixPrimitive(p);
249 }
250 }
251}
Note: See TracBrowser for help on using the repository browser.