| 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 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.Collections; |
| 8 | import java.util.Comparator; |
| 9 | import java.util.HashMap; |
| 10 | import java.util.List; |
| 11 | import java.util.Map; |
| 12 | import java.util.Map.Entry; |
| 13 | import java.util.regex.Pattern; |
| 14 | |
| 15 | import org.openstreetmap.josm.data.osm.Node; |
| 16 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| 17 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType; |
| 18 | import org.openstreetmap.josm.data.osm.Relation; |
| 19 | import org.openstreetmap.josm.data.osm.RelationMember; |
| 20 | import org.openstreetmap.josm.data.osm.Way; |
| 21 | import org.openstreetmap.josm.data.validation.Severity; |
| 22 | import org.openstreetmap.josm.data.validation.Test; |
| 23 | import org.openstreetmap.josm.data.validation.TestError; |
| 24 | import org.openstreetmap.josm.tools.Logging; |
| 25 | |
| 26 | /** |
| 27 | * Check for inconsistencies in lane information between relation and members. |
| 28 | */ |
| 29 | public class ConnectivityRelations extends Test { |
| 30 | |
| 31 | protected static final int INCONSISTENT_LANE_COUNT = 3900; |
| 32 | |
| 33 | protected static final int UNKNOWN_CONNECTIVITY_ROLE = 3901; |
| 34 | |
| 35 | protected static final int NO_CONNECTIVITY_TAG = 3902; |
| 36 | |
| 37 | protected static final int MALFORMED_CONNECTIVITY_TAG = 3903; |
| 38 | |
| 39 | protected static final int TOO_MANY_ROLES = 3904; |
| 40 | |
| 41 | protected static final int MISSING_ROLE = 3905; |
| 42 | |
| 43 | protected static final int MEMBER_MISSING_LANES = 3906; |
| 44 | |
| 45 | private static final String CONNECTIVITY_TAG = "connectivity"; |
| 46 | private static final String VIA = "via"; |
| 47 | private static final String TO = "to"; |
| 48 | private static final String FROM = "from"; |
| 49 | private static final int BW = -1000; |
| 50 | private static final Pattern OPTIONAL_LANE_PATTERN = Pattern.compile("\\([0-9-]+\\)"); |
| 51 | private static final Pattern TO_LANE_PATTERN = Pattern.compile("\\p{Zs}*[,:;]\\p{Zs}*"); |
| 52 | |
| 53 | /** |
| 54 | * Constructor |
| 55 | */ |
| 56 | public ConnectivityRelations() { |
| 57 | super(tr("Connectivity Relation Check"), tr("Checks that lane count of relation matches with lanes of members")); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Convert the connectivity tag into a map of values |
| 62 | * |
| 63 | * @param relation A relation with a {@code connectivity} tag. |
| 64 | * @return A Map in the form of {@code Map<Lane From, Map<Lane To, Optional>>} May contain nulls when errors are encountered |
| 65 | * @since xxx |
| 66 | */ |
| 67 | public static Map<Integer, Map<Integer, Boolean>> parseConnectivityTag(Relation relation) { |
| 68 | final String joined = relation.get(CONNECTIVITY_TAG).replaceAll("bw", Integer.toString(BW)); |
| 69 | |
| 70 | if (joined == null) { |
| 71 | return Collections.emptyMap(); |
| 72 | } |
| 73 | |
| 74 | final Map<Integer, Map<Integer, Boolean>> result = new HashMap<>(); |
| 75 | String[] lanes = joined.split("\\|", -1); |
| 76 | for (int i = 0; i < lanes.length; i++) { |
| 77 | String[] lane = lanes[i].split(":", -1); |
| 78 | |
| 79 | int laneNumber; |
| 80 | //Ignore connections from bw, since we cannot derive a lane number from bw |
| 81 | if (!lane[0].equals("bw")) { |
| 82 | laneNumber = Integer.parseInt(lane[0].trim()); |
| 83 | } else { |
| 84 | laneNumber = BW; |
| 85 | } |
| 86 | Map<Integer, Boolean> connections = new HashMap<>(); |
| 87 | String[] toLanes = TO_LANE_PATTERN.split(lane[1]); |
| 88 | for (int j = 0; j < toLanes.length; j++) { |
| 89 | String toLane = toLanes[j].trim(); |
| 90 | try { |
| 91 | if (OPTIONAL_LANE_PATTERN.matcher(toLane).matches()) { |
| 92 | toLane = toLane.replace("(", "").replace(")", "").trim(); |
| 93 | if (!toLane.equals("bw")) { |
| 94 | connections.put(Integer.parseInt(toLane), Boolean.TRUE); |
| 95 | } else |
| 96 | connections.put(BW, Boolean.TRUE); |
| 97 | } else { |
| 98 | if (!toLane.contains("bw")) { |
| 99 | connections.put(Integer.parseInt(toLane), Boolean.FALSE); |
| 100 | } else { |
| 101 | connections.put(BW, Boolean.FALSE); |
| 102 | } |
| 103 | } |
| 104 | } catch (NumberFormatException e) { |
| 105 | Logging.debug(e); |
| 106 | connections.put(null, null); |
| 107 | } |
| 108 | } |
| 109 | result.put(laneNumber, connections); |
| 110 | |
| 111 | } |
| 112 | return result; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void visit(Relation r) { |
| 117 | if (r.hasTag("type", CONNECTIVITY_TAG)) { |
| 118 | if (!r.hasKey(CONNECTIVITY_TAG)) { |
| 119 | errors.add(TestError.builder(this, Severity.WARNING, NO_CONNECTIVITY_TAG) |
| 120 | .message(tr("No connectivity tag in connectivity relation")).primitives(r).build()); |
| 121 | } else if (!r.hasIncompleteMembers()) { |
| 122 | boolean badRole = checkForBadRole(r); |
| 123 | boolean missingRole = checkForMissingRole(r); |
| 124 | if (!badRole && !missingRole) { |
| 125 | checkForInconsistentLanes(r); |
| 126 | } else if (missingRole) { |
| 127 | createMissingRole(r); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private void checkForInconsistentLanes(Relation relation) { |
| 134 | String lanelessRoles = ""; |
| 135 | // Lane count from connectivity tag |
| 136 | Map<Integer, Map<Integer, Boolean>> connTagLanes = parseConnectivityTag(relation); |
| 137 | // Lane count from member tags |
| 138 | Map<String, Integer> roleLanes = new HashMap<>(); |
| 139 | |
| 140 | |
| 141 | for (RelationMember rM : relation.getMembers()) { |
| 142 | // Check lanes |
| 143 | if (rM.getType() == OsmPrimitiveType.WAY) { |
| 144 | OsmPrimitive prim = rM.getMember(); |
| 145 | if (!VIA.equals(rM.getRole())) { |
| 146 | if (prim.hasKey("lanes")) |
| 147 | roleLanes.put(rM.getRole(), Integer.parseInt(prim.get("lanes"))); |
| 148 | else { |
| 149 | String addString = "'" + rM.getRole() + "'"; |
| 150 | if (!lanelessRoles.isEmpty()) { |
| 151 | addString = ", " + addString; |
| 152 | } |
| 153 | lanelessRoles += addString; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if (lanelessRoles.isEmpty()) { |
| 160 | boolean fromCheck = roleLanes.get(FROM) < Collections |
| 161 | .max(connTagLanes.entrySet(), Comparator.comparingInt(Map.Entry::getKey)).getKey(); |
| 162 | boolean toCheck = false; |
| 163 | for (Entry<Integer, Map<Integer, Boolean>> to : connTagLanes.entrySet()) { |
| 164 | if (!to.getValue().containsKey(null)) { |
| 165 | toCheck = roleLanes.get(TO) < Collections |
| 166 | .max(to.getValue().entrySet(), Comparator.comparingInt(Map.Entry::getKey)).getKey(); |
| 167 | } else { |
| 168 | errors.add(TestError.builder(this, Severity.ERROR, MALFORMED_CONNECTIVITY_TAG) |
| 169 | .message(tr("Connectivity tag contains unusual data")).primitives(relation) |
| 170 | .build()); |
| 171 | } |
| 172 | } |
| 173 | if (fromCheck || toCheck) { |
| 174 | errors.add(TestError.builder(this, Severity.WARNING, INCONSISTENT_LANE_COUNT) |
| 175 | .message(tr("Inconsistent lane numbering between relation and members")).primitives(relation) |
| 176 | .build()); |
| 177 | } |
| 178 | } else { |
| 179 | errors.add(TestError.builder(this, Severity.OTHER, MEMBER_MISSING_LANES) |
| 180 | .message(tr("Relation {0} member(s) missing lanes tag", lanelessRoles)).primitives(relation) |
| 181 | .build()); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | private boolean checkForBadRole(Relation relation) { |
| 186 | // Check role names |
| 187 | int viaWays = 0; |
| 188 | int viaNodes = 0; |
| 189 | int toWays = 0; |
| 190 | int fromWays = 0; |
| 191 | for (RelationMember relationMember : relation.getMembers()) { |
| 192 | if (relationMember.getMember() instanceof Way) { |
| 193 | if (relationMember.hasRole(FROM)) |
| 194 | fromWays++; |
| 195 | else if (relationMember.hasRole(TO)) |
| 196 | toWays++; |
| 197 | else if (relationMember.hasRole(VIA)) |
| 198 | viaWays++; |
| 199 | else { |
| 200 | createUnknownRole(relation, relationMember.getMember()); |
| 201 | return true; |
| 202 | } |
| 203 | } else if (relationMember.getMember() instanceof Node) { |
| 204 | if (!relationMember.hasRole(VIA)) { |
| 205 | createUnknownRole(relation, relationMember.getMember()); |
| 206 | return true; |
| 207 | } |
| 208 | viaNodes++; |
| 209 | } |
| 210 | } |
| 211 | return mixedViaNodeAndWay(relation, viaWays, viaNodes, toWays, fromWays); |
| 212 | } |
| 213 | |
| 214 | private boolean checkForMissingRole(Relation relation) { |
| 215 | List<String> necessaryRoles = new ArrayList<>(); |
| 216 | necessaryRoles.add(FROM); |
| 217 | necessaryRoles.add(VIA); |
| 218 | necessaryRoles.add(TO); |
| 219 | |
| 220 | List<String> roleList = new ArrayList<>(); |
| 221 | for (RelationMember relationMember: relation.getMembers()) { |
| 222 | roleList.add(relationMember.getRole()); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | return !roleList.containsAll(necessaryRoles); |
| 227 | } |
| 228 | |
| 229 | private boolean mixedViaNodeAndWay(Relation relation, int viaWays, int viaNodes, int toWays, int fromWays) { |
| 230 | String message = ""; |
| 231 | if ((viaWays != 0 && viaNodes != 0) || viaNodes > 1) { |
| 232 | message = tr("Relation contains {1} {0} roles.", VIA, viaWays + viaNodes); |
| 233 | } else if (toWays != 1) { |
| 234 | message = tr("Relation contains too many {0} roles", TO); |
| 235 | } else if (fromWays != 1) { |
| 236 | message = tr("Relation contains too many {0} roles", FROM); |
| 237 | } |
| 238 | if (message.isEmpty()) { |
| 239 | return false; |
| 240 | } else { |
| 241 | errors.add(TestError.builder(this, Severity.WARNING, TOO_MANY_ROLES) |
| 242 | .message(message).primitives(relation).build()); |
| 243 | return true; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | private void createUnknownRole(Relation relation, OsmPrimitive primitive) { |
| 248 | errors.add(TestError.builder(this, Severity.WARNING, UNKNOWN_CONNECTIVITY_ROLE) |
| 249 | .message(tr("Unkown role in connectivity relation")).primitives(relation).highlight(primitive).build()); |
| 250 | } |
| 251 | |
| 252 | private void createMissingRole(Relation relation) { |
| 253 | errors.add(TestError.builder(this, Severity.WARNING, MISSING_ROLE) |
| 254 | .message(tr("Connectivity relation is missing at least one necessary role")).primitives(relation) |
| 255 | .build()); |
| 256 | } |
| 257 | } |