1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.Comparator;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.Map.Entry;
|
---|
15 | import java.util.Set;
|
---|
16 | import java.util.regex.Pattern;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
21 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
22 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
23 | import org.openstreetmap.josm.data.osm.Way;
|
---|
24 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
25 | import org.openstreetmap.josm.data.validation.Test;
|
---|
26 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
27 | import org.openstreetmap.josm.tools.Logging;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Check for inconsistencies in lane information between relation and members.
|
---|
31 | */
|
---|
32 | public class ConnectivityRelations extends Test {
|
---|
33 |
|
---|
34 | protected static final int INCONSISTENT_LANE_COUNT = 3900;
|
---|
35 |
|
---|
36 | protected static final int UNKNOWN_CONNECTIVITY_ROLE = 3901;
|
---|
37 |
|
---|
38 | protected static final int NO_CONNECTIVITY_TAG = 3902;
|
---|
39 |
|
---|
40 | protected static final int MALFORMED_CONNECTIVITY_TAG = 3903;
|
---|
41 |
|
---|
42 | protected static final int MISSING_COMMA_CONNECTIVITY_TAG = 3904;
|
---|
43 |
|
---|
44 | protected static final int TOO_MANY_ROLES = 3905;
|
---|
45 |
|
---|
46 | protected static final int MISSING_ROLE = 3906;
|
---|
47 |
|
---|
48 | protected static final int MEMBER_MISSING_LANES = 3907;
|
---|
49 |
|
---|
50 | protected static final int CONNECTIVITY_IMPLIED = 3908;
|
---|
51 |
|
---|
52 | private static final String CONNECTIVITY_TAG = "connectivity";
|
---|
53 | private static final String VIA = "via";
|
---|
54 | private static final String TO = "to";
|
---|
55 | private static final String FROM = "from";
|
---|
56 | private static final int BW = -1000;
|
---|
57 | private static final Pattern OPTIONAL_LANE_PATTERN = Pattern.compile("\\([0-9-]+\\)");
|
---|
58 | private static final Pattern TO_LANE_PATTERN = Pattern.compile("\\p{Zs}*[,:;]\\p{Zs}*");
|
---|
59 | private static final Pattern MISSING_COMMA_PATTERN = Pattern.compile("[0-9]+\\([0-9]+\\)|\\([0-9]+\\)[0-9]+");
|
---|
60 | private static final Pattern LANE_TAG_PATTERN = Pattern.compile(".*:lanes");
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Constructor
|
---|
64 | */
|
---|
65 | public ConnectivityRelations() {
|
---|
66 | super(tr("Connectivity Relations"), tr("Validates connectivity relations"));
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Convert the connectivity tag into a map of values
|
---|
71 | *
|
---|
72 | * @param relation A relation with a {@code connectivity} tag.
|
---|
73 | * @return A Map in the form of {@code Map<Lane From, Map<Lane To, Optional>>} May contain nulls when errors are encountered
|
---|
74 | */
|
---|
75 | public static Map<Integer, Map<Integer, Boolean>> parseConnectivityTag(Relation relation) {
|
---|
76 | String cnTag = relation.get(CONNECTIVITY_TAG);
|
---|
77 | if (cnTag == null) {
|
---|
78 | return Collections.emptyMap();
|
---|
79 | }
|
---|
80 | final String joined = cnTag.replace("bw", Integer.toString(BW));
|
---|
81 |
|
---|
82 |
|
---|
83 | final Map<Integer, Map<Integer, Boolean>> result = new HashMap<>();
|
---|
84 | String[] lanes = joined.split("\\|", -1);
|
---|
85 | for (int i = 0; i < lanes.length; i++) {
|
---|
86 | String[] lane = lanes[i].split(":", -1);
|
---|
87 | int laneNumber;
|
---|
88 | //Ignore connections from bw, since we cannot derive a lane number from bw
|
---|
89 | if (!"bw".equals(lane[0])) {
|
---|
90 | laneNumber = Integer.parseInt(lane[0].trim());
|
---|
91 | } else {
|
---|
92 | laneNumber = BW;
|
---|
93 | }
|
---|
94 | Map<Integer, Boolean> connections = new HashMap<>();
|
---|
95 | String[] toLanes = TO_LANE_PATTERN.split(lane[1], -1);
|
---|
96 | for (int j = 0; j < toLanes.length; j++) {
|
---|
97 | String toLane = toLanes[j].trim();
|
---|
98 | try {
|
---|
99 | if (OPTIONAL_LANE_PATTERN.matcher(toLane).matches()) {
|
---|
100 | toLane = toLane.replace("(", "").replace(")", "").trim();
|
---|
101 | if (!"bw".equals(toLane)) {
|
---|
102 | connections.put(Integer.parseInt(toLane), Boolean.TRUE);
|
---|
103 | } else
|
---|
104 | connections.put(BW, Boolean.TRUE);
|
---|
105 | } else {
|
---|
106 | if (!toLane.contains("bw")) {
|
---|
107 | connections.put(Integer.parseInt(toLane), Boolean.FALSE);
|
---|
108 | } else {
|
---|
109 | connections.put(BW, Boolean.FALSE);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | } catch (NumberFormatException e) {
|
---|
113 | if (MISSING_COMMA_PATTERN.matcher(toLane).matches()) {
|
---|
114 | connections.put(null, true);
|
---|
115 | } else {
|
---|
116 | connections.put(null, null);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 | result.put(laneNumber, connections);
|
---|
121 | }
|
---|
122 | return Collections.unmodifiableMap(result);
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | public void visit(Relation r) {
|
---|
127 | if (r.hasTag("type", CONNECTIVITY_TAG)) {
|
---|
128 | if (!r.hasKey(CONNECTIVITY_TAG)) {
|
---|
129 | errors.add(TestError.builder(this, Severity.WARNING, NO_CONNECTIVITY_TAG)
|
---|
130 | .message(tr("Connectivity relation without connectivity tag")).primitives(r).build());
|
---|
131 | } else if (!r.hasIncompleteMembers()) {
|
---|
132 | boolean badRole = checkForBadRole(r);
|
---|
133 | boolean missingRole = checkForMissingRole(r);
|
---|
134 | if (!badRole && !missingRole) {
|
---|
135 | Map<String, Integer> roleLanes = checkForInconsistentLanes(r);
|
---|
136 | checkForImpliedConnectivity(r, roleLanes);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Compare lane tags of members to values in the {@code connectivity} tag of the relation
|
---|
144 | *
|
---|
145 | * @param relation A relation with a {@code connectivity} tag.
|
---|
146 | * @return A Map in the form of {@code Map<Role, Lane Count>}
|
---|
147 | */
|
---|
148 | private Map<String, Integer> checkForInconsistentLanes(Relation relation) {
|
---|
149 | StringBuilder lanelessRoles = new StringBuilder();
|
---|
150 | int lanelessRolesCount = 0;
|
---|
151 | // Lane count from connectivity tag
|
---|
152 | Map<Integer, Map<Integer, Boolean>> connTagLanes = parseConnectivityTag(relation);
|
---|
153 | // If the ways involved in the connectivity tag are assuming a standard 2-way bi-directional highway
|
---|
154 | boolean defaultLanes = true;
|
---|
155 | for (Entry<Integer, Map<Integer, Boolean>> thisEntry : connTagLanes.entrySet()) {
|
---|
156 | for (Entry<Integer, Boolean> thisEntry2 : thisEntry.getValue().entrySet()) {
|
---|
157 | Logging.debug("Checking: " + thisEntry2.toString());
|
---|
158 | if (thisEntry2.getKey() != null && thisEntry2.getKey() > 1) {
|
---|
159 | defaultLanes = false;
|
---|
160 | break;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | if (!defaultLanes) {
|
---|
164 | break;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | // Lane count from member tags
|
---|
168 | Map<String, Integer> roleLanes = new HashMap<>();
|
---|
169 | for (RelationMember rM : relation.getMembers()) {
|
---|
170 | // Check lanes
|
---|
171 | if (rM.getType() == OsmPrimitiveType.WAY) {
|
---|
172 | OsmPrimitive prim = rM.getMember();
|
---|
173 | if (!VIA.equals(rM.getRole())) {
|
---|
174 | Map<String, String> primKeys = prim.getKeys();
|
---|
175 | List<Long> laneCounts = new ArrayList<>();
|
---|
176 | long maxLaneCount;
|
---|
177 | if (prim.hasTag("lanes")) {
|
---|
178 | laneCounts.add(Long.parseLong(prim.get("lanes")));
|
---|
179 | }
|
---|
180 | for (Entry<String, String> entry : primKeys.entrySet()) {
|
---|
181 | String thisKey = entry.getKey();
|
---|
182 | String thisValue = entry.getValue();
|
---|
183 | if (LANE_TAG_PATTERN.matcher(thisKey).matches()) {
|
---|
184 | //Count bar characters
|
---|
185 | long count = thisValue.chars().filter(ch -> ch == '|').count() + 1;
|
---|
186 | laneCounts.add(count);
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (!laneCounts.equals(Collections.emptyList())) {
|
---|
191 | maxLaneCount = Collections.max(laneCounts);
|
---|
192 | roleLanes.put(rM.getRole(), (int) maxLaneCount);
|
---|
193 | } else {
|
---|
194 | String addString = "'" + rM.getRole() + "'";
|
---|
195 | StringBuilder sb = new StringBuilder(addString);
|
---|
196 | if (lanelessRoles.length() > 0) {
|
---|
197 | sb.insert(0, " and ");
|
---|
198 | }
|
---|
199 | lanelessRoles.append(sb.toString());
|
---|
200 | lanelessRolesCount++;
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (lanelessRoles.toString().isEmpty()) {
|
---|
207 | boolean fromCheck = roleLanes.get(FROM) < Collections
|
---|
208 | .max(connTagLanes.entrySet(), Comparator.comparingInt(Map.Entry::getKey)).getKey();
|
---|
209 | boolean toCheck = false;
|
---|
210 | for (Entry<Integer, Map<Integer, Boolean>> to : connTagLanes.entrySet()) {
|
---|
211 | if (!to.getValue().containsKey(null)) {
|
---|
212 | toCheck = roleLanes.get(TO) < Collections
|
---|
213 | .max(to.getValue().entrySet(), Comparator.comparingInt(Map.Entry::getKey)).getKey();
|
---|
214 | } else {
|
---|
215 | if (to.getValue().containsValue(true)) {
|
---|
216 | errors.add(TestError.builder(this, Severity.ERROR, MISSING_COMMA_CONNECTIVITY_TAG)
|
---|
217 | .message(tr("Connectivity tag missing comma between optional and non-optional values")).primitives(relation)
|
---|
218 | .build());
|
---|
219 | } else {
|
---|
220 | errors.add(TestError.builder(this, Severity.ERROR, MALFORMED_CONNECTIVITY_TAG)
|
---|
221 | .message(tr("Connectivity tag contains unusual data")).primitives(relation)
|
---|
222 | .build());
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 | if (fromCheck || toCheck) {
|
---|
227 | errors.add(TestError.builder(this, Severity.WARNING, INCONSISTENT_LANE_COUNT)
|
---|
228 | .message(tr("Inconsistent lane numbering between relation and member tags")).primitives(relation)
|
---|
229 | .build());
|
---|
230 | }
|
---|
231 | } else if (!defaultLanes) {
|
---|
232 | errors.add(TestError.builder(this, Severity.WARNING, MEMBER_MISSING_LANES)
|
---|
233 | .message(trn("Relation {0} member is missing a lanes or *:lanes tag",
|
---|
234 | "Relation {0} members are missing a lanes or *:lanes tag", lanelessRolesCount,
|
---|
235 | lanelessRoles))
|
---|
236 | .primitives(relation).build());
|
---|
237 | }
|
---|
238 | return roleLanes;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Check the relation to see if the connectivity described is already implied by other data
|
---|
243 | *
|
---|
244 | * @param relation A relation with a {@code connectivity} tag.
|
---|
245 | * @param roleLanes The lane counts for each relation role
|
---|
246 | */
|
---|
247 | private void checkForImpliedConnectivity(Relation relation, Map<String, Integer> roleLanes) {
|
---|
248 | Map<Integer, Map<Integer, Boolean>> connTagLanes = parseConnectivityTag(relation);
|
---|
249 | // Don't flag connectivity as already implied when:
|
---|
250 | // - Lane counts are different on the roads
|
---|
251 | // - Placement tags convey the connectivity
|
---|
252 | // - The relation passes through an intersection
|
---|
253 | // - If via member is a node, it's connected to ways not in the relation
|
---|
254 | // - If a via member is a way, ways not in the relation connect to its nodes
|
---|
255 | // - Highways that appear to be merging have a different cumulative number of lanes than
|
---|
256 | // the highway that they're merging into
|
---|
257 |
|
---|
258 | boolean connImplied = checkMemberTagsForImpliedConnectivity(relation, roleLanes) && !checkForIntersectionAtMembers(relation)
|
---|
259 | // Check if connectivity tag implies default connectivity
|
---|
260 | && connTagLanes.entrySet().stream()
|
---|
261 | .noneMatch(to -> {
|
---|
262 | int fromLane = to.getKey();
|
---|
263 | return to.getValue().entrySet().stream()
|
---|
264 | .anyMatch(lane -> lane.getKey() != null && fromLane != lane.getKey());
|
---|
265 | });
|
---|
266 |
|
---|
267 | if (connImplied) {
|
---|
268 | errors.add(TestError.builder(this, Severity.WARNING, CONNECTIVITY_IMPLIED)
|
---|
269 | .message(tr("This connectivity may already be implied")).primitives(relation)
|
---|
270 | .build());
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Check to see if there is an intersection present at the via member
|
---|
276 | *
|
---|
277 | * @param relation A relation with a {@code connectivity} tag.
|
---|
278 | * @return A Boolean that indicates whether an intersection is present at the via member
|
---|
279 | */
|
---|
280 | private static boolean checkForIntersectionAtMembers(Relation relation) {
|
---|
281 | OsmPrimitive viaPrim = relation.findRelationMembers("via").get(0);
|
---|
282 | Set<OsmPrimitive> relationMembers = relation.getMemberPrimitives();
|
---|
283 |
|
---|
284 | if (viaPrim.getType() == OsmPrimitiveType.NODE) {
|
---|
285 | Node viaNode = (Node) viaPrim;
|
---|
286 | List<Way> parentWays = viaNode.getParentWays();
|
---|
287 | if (parentWays.size() > 2) {
|
---|
288 | return parentWays.stream()
|
---|
289 | .anyMatch(thisWay -> !relationMembers.contains(thisWay) && thisWay.hasTag("highway"));
|
---|
290 | }
|
---|
291 | } else if (viaPrim.getType() == OsmPrimitiveType.WAY) {
|
---|
292 | Way viaWay = (Way) viaPrim;
|
---|
293 | return viaWay.getNodes().stream()
|
---|
294 | .map(Node::getParentWays).filter(parentWays -> parentWays.size() > 2)
|
---|
295 | .flatMap(Collection::stream)
|
---|
296 | .anyMatch(thisWay -> !relationMembers.contains(thisWay) && thisWay.hasTag("highway"));
|
---|
297 | }
|
---|
298 | return false;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Check the relation to see if the connectivity described is already implied by the relation members' tags
|
---|
303 | *
|
---|
304 | * @param relation A relation with a {@code connectivity} tag.
|
---|
305 | * @param roleLanes The lane counts for each relation role
|
---|
306 | * @return Whether connectivity is already implied by tags on relation members
|
---|
307 | */
|
---|
308 | private static boolean checkMemberTagsForImpliedConnectivity(Relation relation, Map<String, Integer> roleLanes) {
|
---|
309 | // The members have different lane counts
|
---|
310 | if (roleLanes.containsKey(TO) && roleLanes.containsKey(FROM) && !roleLanes.get(TO).equals(roleLanes.get(FROM))) {
|
---|
311 | return false;
|
---|
312 | }
|
---|
313 |
|
---|
314 | // The members don't have placement tags defining the connectivity
|
---|
315 | List<RelationMember> members = relation.getMembers();
|
---|
316 | Map<String, OsmPrimitive> toFromMembers = new HashMap<>();
|
---|
317 | for (RelationMember mem : members) {
|
---|
318 | if (mem.getRole().equals(FROM)) {
|
---|
319 | toFromMembers.put(FROM, mem.getMember());
|
---|
320 | } else if (mem.getRole().equals(TO)) {
|
---|
321 | toFromMembers.put(TO, mem.getMember());
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | return toFromMembers.get(TO).hasKey("placement") || toFromMembers.get(FROM).hasKey("placement");
|
---|
326 | }
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * Check if the roles of the relation are appropriate
|
---|
330 | *
|
---|
331 | * @param relation A relation with a {@code connectivity} tag.
|
---|
332 | * @return Whether one or more of the relation's members has an unusual role
|
---|
333 | */
|
---|
334 | private boolean checkForBadRole(Relation relation) {
|
---|
335 | // Check role names
|
---|
336 | int viaWays = 0;
|
---|
337 | int viaNodes = 0;
|
---|
338 | for (RelationMember relationMember : relation.getMembers()) {
|
---|
339 | if (relationMember.getMember() instanceof Way) {
|
---|
340 | if (relationMember.hasRole(VIA))
|
---|
341 | viaWays++;
|
---|
342 | else if (!relationMember.hasRole(FROM) && !relationMember.hasRole(TO)) {
|
---|
343 | return true;
|
---|
344 | }
|
---|
345 | } else if (relationMember.getMember() instanceof Node) {
|
---|
346 | if (!relationMember.hasRole(VIA)) {
|
---|
347 | return true;
|
---|
348 | }
|
---|
349 | viaNodes++;
|
---|
350 | }
|
---|
351 | }
|
---|
352 | return mixedViaNodeAndWay(relation, viaWays, viaNodes);
|
---|
353 | }
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Check if the relation contains all necessary roles
|
---|
357 | *
|
---|
358 | * @param relation A relation with a {@code connectivity} tag.
|
---|
359 | * @return Whether the relation is missing one or more of the critical {@code from}, {@code via}, or {@code to} roles
|
---|
360 | */
|
---|
361 | private static boolean checkForMissingRole(Relation relation) {
|
---|
362 | List<String> necessaryRoles = new ArrayList<>();
|
---|
363 | necessaryRoles.add(FROM);
|
---|
364 | necessaryRoles.add(VIA);
|
---|
365 | necessaryRoles.add(TO);
|
---|
366 | return !relation.getMemberRoles().containsAll(necessaryRoles);
|
---|
367 | }
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Check if the relation's roles are on appropriate objects
|
---|
371 | *
|
---|
372 | * @param relation A relation with a {@code connectivity} tag.
|
---|
373 | * @param viaWays The number of ways in the relation with the {@code via} role
|
---|
374 | * @param viaNodes The number of nodes in the relation with the {@code via} role
|
---|
375 | * @return Whether the relation is missing one or more of the critical 'from', 'via', or 'to' roles
|
---|
376 | */
|
---|
377 | private boolean mixedViaNodeAndWay(Relation relation, int viaWays, int viaNodes) {
|
---|
378 | String message = "";
|
---|
379 | if (viaNodes > 1) {
|
---|
380 | if (viaWays > 0) {
|
---|
381 | message = tr("Relation should not contain mixed ''via'' ways and nodes");
|
---|
382 | } else {
|
---|
383 | message = tr("Multiple ''via'' roles only allowed with ways");
|
---|
384 | }
|
---|
385 | }
|
---|
386 | if (message.isEmpty()) {
|
---|
387 | return false;
|
---|
388 | } else {
|
---|
389 | errors.add(TestError.builder(this, Severity.WARNING, TOO_MANY_ROLES)
|
---|
390 | .message(message).primitives(relation).build());
|
---|
391 | return true;
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | }
|
---|