| | 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.Collections; |
| | 7 | import java.util.Comparator; |
| | 8 | import java.util.HashMap; |
| | 9 | import java.util.Map; |
| | 10 | import java.util.Map.Entry; |
| | 11 | import java.util.regex.Pattern; |
| | 12 | |
| | 13 | import org.openstreetmap.josm.data.osm.Node; |
| | 14 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 15 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType; |
| | 16 | import org.openstreetmap.josm.data.osm.Relation; |
| | 17 | import org.openstreetmap.josm.data.osm.RelationMember; |
| | 18 | import org.openstreetmap.josm.data.osm.Way; |
| | 19 | import org.openstreetmap.josm.data.validation.Severity; |
| | 20 | import org.openstreetmap.josm.data.validation.Test; |
| | 21 | import org.openstreetmap.josm.data.validation.TestError; |
| | 22 | import org.openstreetmap.josm.tools.Logging; |
| | 23 | |
| | 24 | /** |
| | 25 | * Check for inconsistencies in lane information between relation and members. |
| | 26 | */ |
| | 27 | public class ConnectivityRelations extends Test { |
| | 28 | |
| | 29 | protected static final int INCONSISTENT_LANE_COUNT = 3900; |
| | 30 | |
| | 31 | protected static final int UNKNOWN_CONNECTIVITY_ROLE = INCONSISTENT_LANE_COUNT + 1; |
| | 32 | |
| | 33 | protected static final int NO_CONNECTIVITY_TAG = INCONSISTENT_LANE_COUNT + 2; |
| | 34 | |
| | 35 | protected static final int TOO_MANY_ROLES = INCONSISTENT_LANE_COUNT + 3; |
| | 36 | |
| | 37 | private static final String CONNECTIVITY_TAG = "connectivity"; |
| | 38 | private static final String VIA = "via"; |
| | 39 | private static final String TO = "to"; |
| | 40 | private static final String FROM = "from"; |
| | 41 | private static final Pattern OPTIONAL_LANE_PATTERN = Pattern.compile("\\([0-9]+\\)"); |
| | 42 | private static final Pattern TO_LANE_PATTERN = Pattern.compile("\\p{Zs}*[,:;]\\p{Zs}*"); |
| | 43 | |
| | 44 | /** |
| | 45 | * Constructor |
| | 46 | */ |
| | 47 | public ConnectivityRelations() { |
| | 48 | super(tr("Connectivity Relation Check"), tr("Checks that lane count of relation matches with lanes of members")); |
| | 49 | } |
| | 50 | |
| | 51 | /** |
| | 52 | * Convert the connectivity tag into a map of values |
| | 53 | * |
| | 54 | * @param relation A relation with a {@code connectivity} tag. |
| | 55 | * @return A Map in the form of {@code Map<Lane From, Map<Lane To, Optional>>} May contain nulls when errors are encountered |
| | 56 | * @since xxx |
| | 57 | */ |
| | 58 | public static Map<Integer, Map<Integer, Boolean>> parseConnectivityTag(Relation relation) { |
| | 59 | final String joined = relation.get(CONNECTIVITY_TAG); |
| | 60 | |
| | 61 | if (joined == null) { |
| | 62 | return Collections.emptyMap(); |
| | 63 | } |
| | 64 | |
| | 65 | final Map<Integer, Map<Integer, Boolean>> result = new HashMap<>(); |
| | 66 | String[] lanes = joined.split("\\|", -1); |
| | 67 | for (int i = 0; i < lanes.length; i++) { |
| | 68 | String[] lane = lanes[i].split(":", -1); |
| | 69 | int laneNumber = Integer.parseInt(lane[0].trim()); |
| | 70 | Map<Integer, Boolean> connections = new HashMap<>(); |
| | 71 | String[] toLanes = TO_LANE_PATTERN.split(lane[1]); |
| | 72 | for (int j = 0; j < toLanes.length; j++) { |
| | 73 | String toLane = toLanes[j].trim(); |
| | 74 | try { |
| | 75 | if (OPTIONAL_LANE_PATTERN.matcher(toLane).matches()) { |
| | 76 | toLane = toLane.replace("(", "").replace(")", "").trim(); |
| | 77 | connections.put(Integer.parseInt(toLane), Boolean.TRUE); |
| | 78 | } else { |
| | 79 | connections.put(Integer.parseInt(toLane), Boolean.FALSE); |
| | 80 | } |
| | 81 | } catch (NumberFormatException e) { |
| | 82 | Logging.debug(e); |
| | 83 | connections.put(null, null); |
| | 84 | } |
| | 85 | |
| | 86 | } |
| | 87 | result.put(laneNumber, connections); |
| | 88 | } |
| | 89 | return result; |
| | 90 | } |
| | 91 | |
| | 92 | @Override |
| | 93 | public void visit(Relation r) { |
| | 94 | if (r.hasTag("type", CONNECTIVITY_TAG)) { |
| | 95 | if (!r.hasKey(CONNECTIVITY_TAG)) { |
| | 96 | errors.add(TestError.builder(this, Severity.WARNING, NO_CONNECTIVITY_TAG) |
| | 97 | .message(tr("No connectivity tag in connectivity relation")).primitives(r).build()); |
| | 98 | } else if (!r.hasIncompleteMembers()) { |
| | 99 | boolean badRole = checkForBadRole(r); |
| | 100 | if (!badRole) |
| | 101 | checkForInconsistentLanes(r); |
| | 102 | } |
| | 103 | } |
| | 104 | } |
| | 105 | |
| | 106 | private void checkForInconsistentLanes(Relation relation) { |
| | 107 | // Lane count from connectivity tag |
| | 108 | Map<Integer, Map<Integer, Boolean>> connTagLanes = parseConnectivityTag(relation); |
| | 109 | // Lane count from member tags |
| | 110 | Map<String, Integer> roleLanes = new HashMap<>(); |
| | 111 | |
| | 112 | for (RelationMember rM : relation.getMembers()) { |
| | 113 | // Check lanes |
| | 114 | if (rM.getType() == OsmPrimitiveType.WAY) { |
| | 115 | OsmPrimitive prim = rM.getMember(); |
| | 116 | if (prim.hasKey("lanes") && !VIA.equals(rM.getRole())) { |
| | 117 | roleLanes.put(rM.getRole(), Integer.parseInt(prim.get("lanes"))); |
| | 118 | } |
| | 119 | } |
| | 120 | } |
| | 121 | boolean fromCheck = roleLanes.get(FROM) < Collections |
| | 122 | .max(connTagLanes.entrySet(), Comparator.comparingInt(Map.Entry::getKey)).getKey(); |
| | 123 | boolean toCheck = false; |
| | 124 | for (Entry<Integer, Map<Integer, Boolean>> to : connTagLanes.entrySet()) { |
| | 125 | toCheck = roleLanes.get(TO) < Collections |
| | 126 | .max(to.getValue().entrySet(), Comparator.comparingInt(Map.Entry::getKey)).getKey(); |
| | 127 | } |
| | 128 | if (fromCheck || toCheck) { |
| | 129 | errors.add(TestError.builder(this, Severity.WARNING, INCONSISTENT_LANE_COUNT) |
| | 130 | .message(tr("Inconsistent lane numbering between relation and members")).primitives(relation) |
| | 131 | .build()); |
| | 132 | } |
| | 133 | } |
| | 134 | |
| | 135 | private boolean checkForBadRole(Relation relation) { |
| | 136 | // Check role names |
| | 137 | int viaWays = 0; |
| | 138 | int viaNodes = 0; |
| | 139 | int toWays = 0; |
| | 140 | int fromWays = 0; |
| | 141 | for (RelationMember relationMember : relation.getMembers()) { |
| | 142 | if (relationMember.getMember() instanceof Way) { |
| | 143 | if (relationMember.hasRole(FROM)) |
| | 144 | fromWays++; |
| | 145 | else if (relationMember.hasRole(TO)) |
| | 146 | toWays++; |
| | 147 | else if (relationMember.hasRole(VIA)) |
| | 148 | viaWays++; |
| | 149 | else { |
| | 150 | createUnknownRole(relation, relationMember.getMember()); |
| | 151 | return true; |
| | 152 | } |
| | 153 | } else if (relationMember.getMember() instanceof Node) { |
| | 154 | if (!relationMember.hasRole(VIA)) { |
| | 155 | createUnknownRole(relation, relationMember.getMember()); |
| | 156 | return true; |
| | 157 | } |
| | 158 | viaNodes++; |
| | 159 | } |
| | 160 | } |
| | 161 | return mixedViaNodeAndWay(relation, viaWays, viaNodes, toWays, fromWays); |
| | 162 | } |
| | 163 | |
| | 164 | private boolean mixedViaNodeAndWay(Relation relation, int viaWays, int viaNodes, int toWays, int fromWays) { |
| | 165 | String message = ""; |
| | 166 | if ((viaWays != 0 && viaNodes != 0) || viaNodes > 1) { |
| | 167 | message = tr("Relation contains {1} {0} roles.", VIA, viaWays + viaNodes); |
| | 168 | } else if (toWays != 1) { |
| | 169 | message = tr("Relation contains too many {0} roles", TO); |
| | 170 | } else if (fromWays != 1) { |
| | 171 | message = tr("Relation contains too many {0} roles", FROM); |
| | 172 | } |
| | 173 | if (message.isEmpty()) { |
| | 174 | return false; |
| | 175 | } else { |
| | 176 | errors.add(TestError.builder(this, Severity.WARNING, TOO_MANY_ROLES) |
| | 177 | .message(message).primitives(relation).build()); |
| | 178 | return true; |
| | 179 | } |
| | 180 | } |
| | 181 | |
| | 182 | private void createUnknownRole(Relation relation, OsmPrimitive primitive) { |
| | 183 | errors.add(TestError.builder(this, Severity.WARNING, UNKNOWN_CONNECTIVITY_ROLE) |
| | 184 | .message(tr("Unkown role in connectivity relation")).primitives(relation).highlight(primitive).build()); |
| | 185 | } |
| | 186 | } |