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

Last change on this file since 12402 was 12318, checked in by Don-vip, 7 years ago

see #14895 - checkstyle

  • Property svn:eol-style set to native
File size: 9.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.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.HashMap;
10import java.util.HashSet;
11import java.util.List;
12import java.util.Locale;
13import java.util.Map;
14import java.util.Map.Entry;
15import java.util.Set;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.coor.EastNorth;
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.Relation;
22import org.openstreetmap.josm.data.osm.RelationMember;
23import org.openstreetmap.josm.data.osm.Way;
24import org.openstreetmap.josm.data.validation.Severity;
25import org.openstreetmap.josm.data.validation.Test;
26import org.openstreetmap.josm.data.validation.TestError;
27import org.openstreetmap.josm.tools.Geometry;
28import org.openstreetmap.josm.tools.Pair;
29import org.openstreetmap.josm.tools.SubclassFilteredCollection;
30
31/**
32 * Performs validation tests on addresses (addr:housenumber) and associatedStreet relations.
33 * @since 5644
34 */
35public class Addresses extends Test {
36
37 protected static final int HOUSE_NUMBER_WITHOUT_STREET = 2601;
38 protected static final int DUPLICATE_HOUSE_NUMBER = 2602;
39 protected static final int MULTIPLE_STREET_NAMES = 2603;
40 protected static final int MULTIPLE_STREET_RELATIONS = 2604;
41 protected static final int HOUSE_NUMBER_TOO_FAR = 2605;
42
43 // CHECKSTYLE.OFF: SingleSpaceSeparator
44 protected static final String ADDR_HOUSE_NUMBER = "addr:housenumber";
45 protected static final String ADDR_INTERPOLATION = "addr:interpolation";
46 protected static final String ADDR_NEIGHBOURHOOD = "addr:neighbourhood";
47 protected static final String ADDR_PLACE = "addr:place";
48 protected static final String ADDR_STREET = "addr:street";
49 protected static final String ASSOCIATED_STREET = "associatedStreet";
50 // CHECKSTYLE.ON: SingleSpaceSeparator
51
52 /**
53 * Constructor
54 */
55 public Addresses() {
56 super(tr("Addresses"), tr("Checks for errors in addresses and associatedStreet relations."));
57 }
58
59 protected List<Relation> getAndCheckAssociatedStreets(OsmPrimitive p) {
60 List<Relation> list = OsmPrimitive.getFilteredList(p.getReferrers(), Relation.class);
61 list.removeIf(r -> !r.hasTag("type", ASSOCIATED_STREET));
62 if (list.size() > 1) {
63 Severity level;
64 // warning level only if several relations have different names, see #10945
65 final String name = list.get(0).get("name");
66 if (name == null || SubclassFilteredCollection.filter(list, r -> r.hasTag("name", name)).size() < list.size()) {
67 level = Severity.WARNING;
68 } else {
69 level = Severity.OTHER;
70 }
71 List<OsmPrimitive> errorList = new ArrayList<>(list);
72 errorList.add(0, p);
73 errors.add(TestError.builder(this, level, MULTIPLE_STREET_RELATIONS)
74 .message(tr("Multiple associatedStreet relations"))
75 .primitives(errorList)
76 .build());
77 }
78 return list;
79 }
80
81 protected void checkHouseNumbersWithoutStreet(OsmPrimitive p) {
82 List<Relation> associatedStreets = getAndCheckAssociatedStreets(p);
83 // Find house number without proper location
84 // (neither addr:street, associatedStreet, addr:place, addr:neighbourhood or addr:interpolation)
85 if (p.hasKey(ADDR_HOUSE_NUMBER) && !p.hasKey(ADDR_STREET, ADDR_PLACE, ADDR_NEIGHBOURHOOD)) {
86 for (Relation r : associatedStreets) {
87 if (r.hasTag("type", ASSOCIATED_STREET)) {
88 return;
89 }
90 }
91 for (Way w : OsmPrimitive.getFilteredList(p.getReferrers(), Way.class)) {
92 if (w.hasKey(ADDR_INTERPOLATION) && w.hasKey(ADDR_STREET)) {
93 return;
94 }
95 }
96 // No street found
97 errors.add(TestError.builder(this, Severity.WARNING, HOUSE_NUMBER_WITHOUT_STREET)
98 .message(tr("House number without street"))
99 .primitives(p)
100 .build());
101 }
102 }
103
104 @Override
105 public void visit(Node n) {
106 checkHouseNumbersWithoutStreet(n);
107 }
108
109 @Override
110 public void visit(Way w) {
111 checkHouseNumbersWithoutStreet(w);
112 }
113
114 @Override
115 public void visit(Relation r) {
116 checkHouseNumbersWithoutStreet(r);
117 if (r.hasTag("type", ASSOCIATED_STREET)) {
118 // Used to count occurences of each house number in order to find duplicates
119 Map<String, List<OsmPrimitive>> map = new HashMap<>();
120 // Used to detect different street names
121 String relationName = r.get("name");
122 Set<OsmPrimitive> wrongStreetNames = new HashSet<>();
123 // Used to check distance
124 Set<OsmPrimitive> houses = new HashSet<>();
125 Set<Way> street = new HashSet<>();
126 for (RelationMember m : r.getMembers()) {
127 String role = m.getRole();
128 OsmPrimitive p = m.getMember();
129 if ("house".equals(role)) {
130 houses.add(p);
131 String number = p.get(ADDR_HOUSE_NUMBER);
132 if (number != null) {
133 number = number.trim().toUpperCase(Locale.ENGLISH);
134 List<OsmPrimitive> list = map.get(number);
135 if (list == null) {
136 list = new ArrayList<>();
137 map.put(number, list);
138 }
139 list.add(p);
140 }
141 if (relationName != null && p.hasKey(ADDR_STREET) && !relationName.equals(p.get(ADDR_STREET))) {
142 if (wrongStreetNames.isEmpty()) {
143 wrongStreetNames.add(r);
144 }
145 wrongStreetNames.add(p);
146 }
147 } else if ("street".equals(role)) {
148 if (p instanceof Way) {
149 street.add((Way) p);
150 }
151 if (relationName != null && p.hasTagDifferent("name", relationName)) {
152 if (wrongStreetNames.isEmpty()) {
153 wrongStreetNames.add(r);
154 }
155 wrongStreetNames.add(p);
156 }
157 }
158 }
159 // Report duplicate house numbers
160 for (Entry<String, List<OsmPrimitive>> entry : map.entrySet()) {
161 List<OsmPrimitive> list = entry.getValue();
162 if (list.size() > 1) {
163 errors.add(TestError.builder(this, Severity.WARNING, DUPLICATE_HOUSE_NUMBER)
164 .message(tr("Duplicate house numbers"), marktr("House number ''{0}'' duplicated"), entry.getKey())
165 .primitives(list)
166 .build());
167 }
168 }
169 // Report wrong street names
170 if (!wrongStreetNames.isEmpty()) {
171 errors.add(TestError.builder(this, Severity.WARNING, MULTIPLE_STREET_NAMES)
172 .message(tr("Multiple street names in relation"))
173 .primitives(wrongStreetNames)
174 .build());
175 }
176 // Report addresses too far away
177 if (!street.isEmpty()) {
178 for (OsmPrimitive house : houses) {
179 if (house.isUsable()) {
180 checkDistance(house, street);
181 }
182 }
183 }
184 }
185 }
186
187 protected void checkDistance(OsmPrimitive house, Collection<Way> street) {
188 EastNorth centroid;
189 if (house instanceof Node) {
190 centroid = ((Node) house).getEastNorth();
191 } else if (house instanceof Way) {
192 List<Node> nodes = ((Way) house).getNodes();
193 if (house.hasKey(ADDR_INTERPOLATION)) {
194 for (Node n : nodes) {
195 if (n.hasKey(ADDR_HOUSE_NUMBER)) {
196 checkDistance(n, street);
197 }
198 }
199 return;
200 }
201 centroid = Geometry.getCentroid(nodes);
202 } else {
203 return; // TODO handle multipolygon houses ?
204 }
205 if (centroid == null) return; // fix #8305
206 double maxDistance = Main.pref.getDouble("validator.addresses.max_street_distance", 200.0);
207 boolean hasIncompleteWays = false;
208 for (Way streetPart : street) {
209 for (Pair<Node, Node> chunk : streetPart.getNodePairs(false)) {
210 EastNorth p1 = chunk.a.getEastNorth();
211 EastNorth p2 = chunk.b.getEastNorth();
212 if (p1 != null && p2 != null) {
213 EastNorth closest = Geometry.closestPointToSegment(p1, p2, centroid);
214 if (closest.distance(centroid) <= maxDistance) {
215 return;
216 }
217 } else {
218 Main.warn("Addresses test skipped chunck "+chunk+" for street part "+streetPart+" because p1 or p2 is null");
219 }
220 }
221 if (!hasIncompleteWays && streetPart.isIncomplete()) {
222 hasIncompleteWays = true;
223 }
224 }
225 // No street segment found near this house, report error on if the relation does not contain incomplete street ways (fix #8314)
226 if (hasIncompleteWays) return;
227 List<OsmPrimitive> errorList = new ArrayList<>(street);
228 errorList.add(0, house);
229 errors.add(TestError.builder(this, Severity.WARNING, HOUSE_NUMBER_TOO_FAR)
230 .message(tr("House number too far from street"))
231 .primitives(errorList)
232 .build());
233 }
234}
Note: See TracBrowser for help on using the repository browser.