source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/Addresses.java@ 16824

Last change on this file since 16824 was 16824, checked in by simon04, 4 years ago

Remove Collection.contains check for Collection.remove

  • Property svn:eol-style set to native
File size: 22.0 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.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.HashMap;
12import java.util.HashSet;
13import java.util.List;
14import java.util.Locale;
15import java.util.Map;
16import java.util.Map.Entry;
17import java.util.Objects;
18import java.util.Set;
19import java.util.stream.Collectors;
20import java.util.stream.Stream;
21
22import org.openstreetmap.josm.command.Command;
23import org.openstreetmap.josm.command.DeleteCommand;
24import org.openstreetmap.josm.data.coor.EastNorth;
25import org.openstreetmap.josm.data.coor.LatLon;
26import org.openstreetmap.josm.data.osm.Node;
27import org.openstreetmap.josm.data.osm.OsmPrimitive;
28import org.openstreetmap.josm.data.osm.Relation;
29import org.openstreetmap.josm.data.osm.RelationMember;
30import org.openstreetmap.josm.data.osm.TagMap;
31import org.openstreetmap.josm.data.osm.Way;
32import org.openstreetmap.josm.data.preferences.DoubleProperty;
33import org.openstreetmap.josm.data.validation.Severity;
34import org.openstreetmap.josm.data.validation.Test;
35import org.openstreetmap.josm.data.validation.TestError;
36import org.openstreetmap.josm.tools.Geometry;
37import org.openstreetmap.josm.tools.Logging;
38import org.openstreetmap.josm.tools.Pair;
39import org.openstreetmap.josm.tools.SubclassFilteredCollection;
40import org.openstreetmap.josm.tools.Territories;
41import org.openstreetmap.josm.tools.Utils;
42
43/**
44 * Performs validation tests on addresses (addr:housenumber) and associatedStreet relations.
45 * @since 5644
46 */
47public class Addresses extends Test {
48
49 protected static final int HOUSE_NUMBER_WITHOUT_STREET = 2601;
50 protected static final int DUPLICATE_HOUSE_NUMBER = 2602;
51 protected static final int MULTIPLE_STREET_NAMES = 2603;
52 protected static final int MULTIPLE_STREET_RELATIONS = 2604;
53 protected static final int HOUSE_NUMBER_TOO_FAR = 2605;
54 protected static final int OBSOLETE_RELATION = 2606;
55
56 protected static final DoubleProperty MAX_DUPLICATE_DISTANCE = new DoubleProperty("validator.addresses.max_duplicate_distance", 200.0);
57 protected static final DoubleProperty MAX_STREET_DISTANCE = new DoubleProperty("validator.addresses.max_street_distance", 200.0);
58
59 // CHECKSTYLE.OFF: SingleSpaceSeparator
60 protected static final String ADDR_HOUSE_NUMBER = "addr:housenumber";
61 protected static final String ADDR_INTERPOLATION = "addr:interpolation";
62 protected static final String ADDR_NEIGHBOURHOOD = "addr:neighbourhood";
63 protected static final String ADDR_PLACE = "addr:place";
64 protected static final String ADDR_STREET = "addr:street";
65 protected static final String ADDR_SUBURB = "addr:suburb";
66 protected static final String ADDR_CITY = "addr:city";
67 protected static final String ADDR_UNIT = "addr:unit";
68 protected static final String ADDR_FLATS = "addr:flats";
69 protected static final String ADDR_HOUSE_NAME = "addr:housename";
70 protected static final String ADDR_POSTCODE = "addr:postcode";
71 protected static final String ASSOCIATED_STREET = "associatedStreet";
72 // CHECKSTYLE.ON: SingleSpaceSeparator
73
74 private Map<String, Collection<OsmPrimitive>> knownAddresses;
75 private Set<String> ignoredAddresses;
76
77 /**
78 * Constructor
79 */
80 public Addresses() {
81 super(tr("Addresses"), tr("Checks for errors in addresses and associatedStreet relations."));
82 }
83
84 protected List<Relation> getAndCheckAssociatedStreets(OsmPrimitive p) {
85 final List<Relation> list = p.referrers(Relation.class)
86 .filter(r -> r.hasTag("type", ASSOCIATED_STREET))
87 .collect(Collectors.toList());
88 if (list.size() > 1) {
89 Severity level;
90 // warning level only if several relations have different names, see #10945
91 final String name = list.get(0).get("name");
92 if (name == null || SubclassFilteredCollection.filter(list, r -> r.hasTag("name", name)).size() < list.size()) {
93 level = Severity.WARNING;
94 } else {
95 level = Severity.OTHER;
96 }
97 List<OsmPrimitive> errorList = new ArrayList<>(list);
98 errorList.add(0, p);
99 errors.add(TestError.builder(this, level, MULTIPLE_STREET_RELATIONS)
100 .message(tr("Multiple associatedStreet relations"))
101 .primitives(errorList)
102 .build());
103 }
104 return list;
105 }
106
107 /**
108 * Checks for house numbers for which the street is unknown.
109 * @param p primitive to test
110 * @return error found, or null
111 */
112 protected TestError checkHouseNumbersWithoutStreet(OsmPrimitive p) {
113 // Find house number without proper location
114 // (neither addr:street, associatedStreet, addr:place, addr:neighbourhood or addr:interpolation)
115 if (p.hasKey(ADDR_HOUSE_NUMBER) && !p.hasKey(ADDR_STREET, ADDR_PLACE, ADDR_NEIGHBOURHOOD)
116 && getAndCheckAssociatedStreets(p).isEmpty()
117 && p.referrers(Way.class).noneMatch(w -> w.hasKey(ADDR_INTERPOLATION) && w.hasKey(ADDR_STREET))) {
118 // no street found
119 TestError e = TestError.builder(this, Severity.WARNING, HOUSE_NUMBER_WITHOUT_STREET)
120 .message(tr("House number without street"))
121 .primitives(p)
122 .build();
123 errors.add(e);
124 return e;
125 }
126 return null;
127 }
128
129 static boolean isPOI(OsmPrimitive p) {
130 return p.hasKey("shop", "amenity", "tourism", "leisure", "emergency", "craft", "office", "name");
131 }
132
133 static boolean hasAddress(OsmPrimitive p) {
134 return p.hasKey(ADDR_HOUSE_NUMBER) && p.hasKey(ADDR_STREET, ADDR_PLACE);
135 }
136
137 /**
138 * adds the OsmPrimitive to the address map if it complies to the restrictions
139 * @param p OsmPrimitive that has an address
140 */
141 private void collectAddress(OsmPrimitive p) {
142 if (!isPOI(p)) {
143 for (String simplifiedAddress : getSimplifiedAddresses(p)) {
144 if (!ignoredAddresses.contains(simplifiedAddress)) {
145 knownAddresses.computeIfAbsent(simplifiedAddress, x -> new ArrayList<>()).add(p);
146 }
147 }
148 }
149 }
150
151 protected void initAddressMap(OsmPrimitive primitive) {
152 knownAddresses = new HashMap<>();
153 ignoredAddresses = new HashSet<>();
154 for (OsmPrimitive p : primitive.getDataSet().allNonDeletedPrimitives()) {
155 if (p instanceof Node && p.hasKey(ADDR_UNIT, ADDR_FLATS)) {
156 for (OsmPrimitive r : p.getReferrers()) {
157 if (hasAddress(r)) {
158 // ignore addresses of buildings that are connected to addr:unit nodes
159 // it's quite reasonable that there are more buildings with this address
160 for (String simplifiedAddress : getSimplifiedAddresses(r)) {
161 if (!ignoredAddresses.contains(simplifiedAddress)) {
162 ignoredAddresses.add(simplifiedAddress);
163 } else {
164 knownAddresses.remove(simplifiedAddress);
165 }
166 }
167 }
168 }
169 }
170 if (hasAddress(p)) {
171 collectAddress(p);
172 }
173 }
174 }
175
176 @Override
177 public void endTest() {
178 knownAddresses = null;
179 ignoredAddresses = null;
180 super.endTest();
181 }
182
183 protected List<TestError> checkForDuplicate(OsmPrimitive p) {
184 if (knownAddresses == null) {
185 initAddressMap(p);
186 }
187 if (!isPOI(p) && hasAddress(p)) {
188 List<TestError> result = new ArrayList<>();
189 for (String simplifiedAddress : getSimplifiedAddresses(p)) {
190 if (!ignoredAddresses.contains(simplifiedAddress) && knownAddresses.containsKey(simplifiedAddress)) {
191 double maxDistance = MAX_DUPLICATE_DISTANCE.get();
192 for (OsmPrimitive p2 : knownAddresses.get(simplifiedAddress)) {
193 if (p == p2) {
194 continue;
195 }
196 Severity severityLevel;
197 String city1 = p.get(ADDR_CITY);
198 String city2 = p2.get(ADDR_CITY);
199 double distance = getDistance(p, p2);
200 if (city1 != null && city2 != null) {
201 if (city1.equals(city2)) {
202 if ((!p.hasKey(ADDR_POSTCODE) || !p2.hasKey(ADDR_POSTCODE)
203 || p.get(ADDR_POSTCODE).equals(p2.get(ADDR_POSTCODE)))
204 && (!p.hasKey(ADDR_SUBURB) || !p2.hasKey(ADDR_SUBURB)
205 || p.get(ADDR_SUBURB).equals(p2.get(ADDR_SUBURB)))) {
206 severityLevel = Severity.WARNING;
207 } else {
208 // address including city identical but postcode or suburb differs
209 // most likely perfectly fine
210 severityLevel = Severity.OTHER;
211 }
212 } else {
213 // address differs only by city - notify if very close, otherwise ignore
214 if (distance < maxDistance) {
215 severityLevel = Severity.OTHER;
216 } else {
217 continue;
218 }
219 }
220 } else {
221 // at least one address has no city specified
222 if (p.hasKey(ADDR_POSTCODE) && p2.hasKey(ADDR_POSTCODE)
223 && p.get(ADDR_POSTCODE).equals(p2.get(ADDR_POSTCODE))) {
224 // address including postcode identical
225 severityLevel = Severity.WARNING;
226 } else {
227 // city/postcode unclear - warn if very close, otherwise only notify
228 // TODO: get city from surrounding boundaries?
229 if (distance < maxDistance) {
230 severityLevel = Severity.WARNING;
231 } else {
232 severityLevel = Severity.OTHER;
233 }
234 }
235 }
236 result.add(TestError.builder(this, severityLevel, DUPLICATE_HOUSE_NUMBER)
237 .message(tr("Duplicate house numbers"), marktr("''{0}'' ({1}m)"), simplifiedAddress, (int) distance)
238 .primitives(Arrays.asList(p, p2)).build());
239 }
240 knownAddresses.get(simplifiedAddress).remove(p); // otherwise we would get every warning two times
241 }
242 }
243 errors.addAll(result);
244 return Collections.unmodifiableList(result);
245 }
246 return Collections.emptyList();
247 }
248
249 static List<String> getSimplifiedAddresses(OsmPrimitive p) {
250 String simplifiedStreetName = p.hasKey(ADDR_STREET) ? p.get(ADDR_STREET) : p.get(ADDR_PLACE);
251 // ignore whitespaces and dashes in street name, so that "Mozart-Gasse", "Mozart Gasse" and "Mozartgasse" are all seen as equal
252 return expandHouseNumber(p.get(ADDR_HOUSE_NUMBER)).stream().map(addrHouseNumber -> Utils.strip(Stream.of(
253 simplifiedStreetName.replaceAll("[ -]", ""),
254 addrHouseNumber,
255 p.get(ADDR_HOUSE_NAME),
256 p.get(ADDR_UNIT),
257 p.get(ADDR_FLATS))
258 .filter(Objects::nonNull)
259 .collect(Collectors.joining(" ")))
260 .toUpperCase(Locale.ENGLISH)).collect(Collectors.toList());
261 }
262
263 /**
264 * Split addr:housenumber on , and ; (common separators)
265 *
266 * @param houseNumber The housenumber to be split
267 * @return A list of addr:housenumber equivalents
268 */
269 static List<String> expandHouseNumber(String houseNumber) {
270 return Arrays.asList(houseNumber.split(",|;", -1));
271 }
272
273 @Override
274 public void visit(Node n) {
275 checkHouseNumbersWithoutStreet(n);
276 checkForDuplicate(n);
277 }
278
279 @Override
280 public void visit(Way w) {
281 checkHouseNumbersWithoutStreet(w);
282 checkForDuplicate(w);
283 }
284
285 @Override
286 public void visit(Relation r) {
287 checkHouseNumbersWithoutStreet(r);
288 checkForDuplicate(r);
289 if (r.hasTag("type", ASSOCIATED_STREET)) {
290 checkIfObsolete(r);
291 // Used to count occurrences of each house number in order to find duplicates
292 Map<String, List<OsmPrimitive>> map = new HashMap<>();
293 // Used to detect different street names
294 String relationName = r.get("name");
295 Set<OsmPrimitive> wrongStreetNames = new HashSet<>();
296 // Used to check distance
297 Set<OsmPrimitive> houses = new HashSet<>();
298 Set<Way> street = new HashSet<>();
299 for (RelationMember m : r.getMembers()) {
300 String role = m.getRole();
301 OsmPrimitive p = m.getMember();
302 if ("house".equals(role)) {
303 houses.add(p);
304 String number = p.get(ADDR_HOUSE_NUMBER);
305 if (number != null) {
306 number = number.trim().toUpperCase(Locale.ENGLISH);
307 List<OsmPrimitive> list = map.computeIfAbsent(number, k -> new ArrayList<>());
308 list.add(p);
309 }
310 if (relationName != null && p.hasKey(ADDR_STREET) && !relationName.equals(p.get(ADDR_STREET))) {
311 if (wrongStreetNames.isEmpty()) {
312 wrongStreetNames.add(r);
313 }
314 wrongStreetNames.add(p);
315 }
316 } else if ("street".equals(role)) {
317 if (p instanceof Way) {
318 street.add((Way) p);
319 }
320 if (relationName != null && p.hasTagDifferent("name", relationName)) {
321 if (wrongStreetNames.isEmpty()) {
322 wrongStreetNames.add(r);
323 }
324 wrongStreetNames.add(p);
325 }
326 }
327 }
328 // Report duplicate house numbers
329 for (Entry<String, List<OsmPrimitive>> entry : map.entrySet()) {
330 List<OsmPrimitive> list = entry.getValue();
331 if (list.size() > 1) {
332 errors.add(TestError.builder(this, Severity.WARNING, DUPLICATE_HOUSE_NUMBER)
333 .message(tr("Duplicate house numbers"), marktr("House number ''{0}'' duplicated"), entry.getKey())
334 .primitives(list)
335 .build());
336 }
337 }
338 // Report wrong street names
339 if (!wrongStreetNames.isEmpty()) {
340 errors.add(TestError.builder(this, Severity.WARNING, MULTIPLE_STREET_NAMES)
341 .message(tr("Multiple street names in relation"))
342 .primitives(wrongStreetNames)
343 .build());
344 }
345 // Report addresses too far away
346 if (!street.isEmpty()) {
347 for (OsmPrimitive house : houses) {
348 if (house.isUsable()) {
349 checkDistance(house, street);
350 }
351 }
352 }
353 }
354 }
355
356 /**
357 * returns rough distance between two OsmPrimitives
358 * @param a primitive a
359 * @param b primitive b
360 * @return distance of center of bounding boxes in meters
361 */
362 static double getDistance(OsmPrimitive a, OsmPrimitive b) {
363 LatLon centerA = a.getBBox().getCenter();
364 LatLon centerB = b.getBBox().getCenter();
365 return (centerA.greatCircleDistance(centerB));
366 }
367
368 protected void checkDistance(OsmPrimitive house, Collection<Way> street) {
369 EastNorth centroid;
370 if (house instanceof Node) {
371 centroid = ((Node) house).getEastNorth();
372 } else if (house instanceof Way) {
373 List<Node> nodes = ((Way) house).getNodes();
374 if (house.hasKey(ADDR_INTERPOLATION)) {
375 for (Node n : nodes) {
376 if (n.hasKey(ADDR_HOUSE_NUMBER)) {
377 checkDistance(n, street);
378 }
379 }
380 return;
381 }
382 centroid = Geometry.getCentroid(nodes);
383 } else {
384 return; // TODO handle multipolygon houses ?
385 }
386 if (centroid == null) return; // fix #8305
387 double maxDistance = MAX_STREET_DISTANCE.get();
388 boolean hasIncompleteWays = false;
389 for (Way streetPart : street) {
390 for (Pair<Node, Node> chunk : streetPart.getNodePairs(false)) {
391 EastNorth p1 = chunk.a.getEastNorth();
392 EastNorth p2 = chunk.b.getEastNorth();
393 if (p1 != null && p2 != null) {
394 EastNorth closest = Geometry.closestPointToSegment(p1, p2, centroid);
395 if (closest.distance(centroid) <= maxDistance) {
396 return;
397 }
398 } else {
399 Logging.warn("Addresses test skipped chunk "+chunk+" for street part "+streetPart+" because p1 or p2 is null");
400 }
401 }
402 if (!hasIncompleteWays && streetPart.isIncomplete()) {
403 hasIncompleteWays = true;
404 }
405 }
406 // No street segment found near this house, report error on if the relation does not contain incomplete street ways (fix #8314)
407 if (hasIncompleteWays) return;
408 List<OsmPrimitive> errorList = new ArrayList<>(street);
409 errorList.add(0, house);
410 errors.add(TestError.builder(this, Severity.WARNING, HOUSE_NUMBER_TOO_FAR)
411 .message(tr("House number too far from street"))
412 .primitives(errorList)
413 .build());
414 }
415
416 /**
417 * Check if an associatedStreet Relation is obsolete. This test marks only those relations which
418 * are complete and don't contain any information which isn't also tagged on the members.
419 * The strategy is to avoid any false positive.
420 * @param r the relation
421 */
422 private void checkIfObsolete(Relation r) {
423 if (r.isIncomplete())
424 return;
425 /** array of country codes for which the test should be performed. For now, only Germany */
426 String[] countryCodes = {"DE"};
427 TagMap neededtagsForHouse = new TagMap();
428 for (Entry<String, String> tag : r.getKeys().entrySet()) {
429 String key = tag.getKey();
430 if (key.startsWith("name:")) {
431 return; // maybe check if all members have corresponding tags?
432 } else if (key.startsWith("addr:")) {
433 neededtagsForHouse.put(key, tag.getValue());
434 } else {
435 switch (key) {
436 case "name":
437 case "type":
438 case "source":
439 break;
440 default:
441 // unexpected tag in relation
442 return;
443 }
444 }
445 }
446
447 for (RelationMember m : r.getMembers()) {
448 if (m.getMember().isIncomplete() || !isInWarnCountry(m, countryCodes))
449 return;
450
451 String role = m.getRole();
452 if ("".equals(role)) {
453 if (m.isWay() && m.getMember().hasKey("highway")) {
454 role = "street";
455 } else if (m.getMember().hasTag("building"))
456 role = "house";
457 }
458 switch (role) {
459 case "house":
460 case "addr:houselink":
461 case "address":
462 if (!m.getMember().hasTag(ADDR_STREET) || !m.getMember().hasTag(ADDR_HOUSE_NUMBER))
463 return;
464 for (Entry<String, String> tag : neededtagsForHouse.entrySet()) {
465 if (!m.getMember().hasTag(tag.getKey(), tag.getValue()))
466 return;
467 }
468 break;
469 case "street":
470 if (!m.getMember().hasTag("name") && r.hasTag("name"))
471 return;
472 break;
473 default:
474 // unknown role: don't create auto-fix
475 return;
476 }
477 }
478 errors.add(TestError.builder(this, Severity.WARNING, OBSOLETE_RELATION)
479 .message(tr("Relation is obsolete"))
480 .primitives(r)
481 .build());
482 }
483
484 private static boolean isInWarnCountry(RelationMember m, String[] countryCodes) {
485 if (countryCodes.length == 0)
486 return true;
487 LatLon center = null;
488
489 if (m.isNode()) {
490 center = m.getNode().getCoor();
491 } else if (m.isWay()) {
492 center = m.getWay().getBBox().getCenter();
493 } else if (m.isRelation() && m.getRelation().isMultipolygon()) {
494 center = m.getRelation().getBBox().getCenter();
495 }
496 if (center == null)
497 return false;
498 for (String country : countryCodes) {
499 if (Territories.isIso3166Code(country, center))
500 return true;
501 }
502 return false;
503 }
504
505 /**
506 * remove obsolete relation.
507 */
508 @Override
509 public Command fixError(TestError testError) {
510 return new DeleteCommand(testError.getPrimitives());
511 }
512
513 @Override
514 public boolean isFixable(TestError testError) {
515 if (!(testError.getTester() instanceof Addresses))
516 return false;
517 return testError.getCode() == OBSOLETE_RELATION;
518 }
519
520}
Note: See TracBrowser for help on using the repository browser.