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