source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/ConnectivityRelations.java@ 17942

Last change on this file since 17942 was 17384, checked in by GerdP, 3 years ago

fix #20182: NumberFormatException in ConnectivityRelations.parseConnectivityTag

  • fix crash with invalid connectivity=right_turn or a single number like connectivity=1
  • remove code which looks for "bw" after "bw" was replaced by -1000.
  • some more cleanup and code simplifications
  • use JOSMTestRules() instead of JOSMFixture

I don't like that method parseConnectivityTag() is public but didn't change it so far. It seems a bit strange to return an empty map for different kinds of problems found in the connectivity tag.

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