001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.validation.tests; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.util.ArrayList; 007import java.util.Arrays; 008import java.util.Collection; 009import java.util.Collections; 010import java.util.Comparator; 011import java.util.HashMap; 012import java.util.HashSet; 013import java.util.List; 014import java.util.Map; 015import java.util.Set; 016import java.util.TreeSet; 017 018import org.openstreetmap.josm.data.osm.Node; 019import org.openstreetmap.josm.data.osm.OsmPrimitive; 020import org.openstreetmap.josm.data.osm.OsmUtils; 021import org.openstreetmap.josm.data.osm.Relation; 022import org.openstreetmap.josm.data.osm.Way; 023import org.openstreetmap.josm.data.osm.WaySegment; 024import org.openstreetmap.josm.data.preferences.CollectionProperty; 025import org.openstreetmap.josm.data.validation.Severity; 026import org.openstreetmap.josm.data.validation.Test; 027import org.openstreetmap.josm.data.validation.TestError; 028import org.openstreetmap.josm.gui.progress.ProgressMonitor; 029import org.openstreetmap.josm.tools.MultiMap; 030import org.openstreetmap.josm.tools.Pair; 031import org.openstreetmap.josm.tools.Predicates; 032import org.openstreetmap.josm.tools.Utils; 033 034/** 035 * Tests if there are overlapping ways. 036 * 037 * @author frsantos 038 */ 039public class OverlappingWays extends Test { 040 041 /** Bag of all way segments */ 042 private MultiMap<Pair<Node, Node>, WaySegment> nodePairs; 043 044 protected static final int OVERLAPPING_HIGHWAY = 101; 045 protected static final int OVERLAPPING_RAILWAY = 102; 046 protected static final int OVERLAPPING_WAY = 103; 047 protected static final int OVERLAPPING_HIGHWAY_AREA = 111; 048 protected static final int OVERLAPPING_RAILWAY_AREA = 112; 049 protected static final int OVERLAPPING_WAY_AREA = 113; 050 protected static final int OVERLAPPING_AREA = 120; 051 protected static final int DUPLICATE_WAY_SEGMENT = 121; 052 053 protected static final CollectionProperty IGNORED_KEYS = new CollectionProperty( 054 "overlapping-ways.ignored-keys", Arrays.asList("barrier", "building", "historic:building", "man_made")); 055 056 /** Constructor */ 057 public OverlappingWays() { 058 super(tr("Overlapping ways"), 059 tr("This test checks that a connection between two nodes " 060 + "is not used by more than one way.")); 061 } 062 063 @Override 064 public void startTest(ProgressMonitor monitor) { 065 super.startTest(monitor); 066 nodePairs = new MultiMap<>(1000); 067 } 068 069 private boolean parentMultipolygonConcernsArea(OsmPrimitive p) { 070 for (Relation r : OsmPrimitive.getFilteredList(p.getReferrers(), Relation.class)) { 071 if (r.concernsArea()) { 072 return true; 073 } 074 } 075 return false; 076 } 077 078 @Override 079 public void endTest() { 080 Map<List<Way>, Set<WaySegment>> seenWays = new HashMap<>(500); 081 082 Collection<TestError> preliminaryErrors = new ArrayList<>(); 083 for (Set<WaySegment> duplicated : nodePairs.values()) { 084 int ways = duplicated.size(); 085 086 if (ways > 1) { 087 List<OsmPrimitive> prims = new ArrayList<>(); 088 List<Way> currentWays = new ArrayList<>(); 089 Collection<WaySegment> highlight; 090 int highway = 0; 091 int railway = 0; 092 int area = 0; 093 094 for (WaySegment ws : duplicated) { 095 if (ws.way.get("highway") != null) { 096 highway++; 097 } else if (ws.way.get("railway") != null) { 098 railway++; 099 } 100 Boolean ar = OsmUtils.getOsmBoolean(ws.way.get("area")); 101 if (ar != null && ar) { 102 area++; 103 } 104 if (ws.way.concernsArea() || parentMultipolygonConcernsArea(ws.way)) { 105 area++; 106 ways--; 107 } 108 109 prims.add(ws.way); 110 currentWays.add(ws.way); 111 } 112 /* These ways not seen before 113 * If two or more of the overlapping ways are 114 * highways or railways mark a separate error 115 */ 116 if ((highlight = seenWays.get(currentWays)) == null) { 117 String errortype; 118 int type; 119 120 if (area > 0) { 121 if (ways == 0 || duplicated.size() == area) { 122 errortype = tr("Areas share segment"); 123 type = OVERLAPPING_AREA; 124 } else if (highway == ways) { 125 errortype = tr("Highways share segment with area"); 126 type = OVERLAPPING_HIGHWAY_AREA; 127 } else if (railway == ways) { 128 errortype = tr("Railways share segment with area"); 129 type = OVERLAPPING_RAILWAY_AREA; 130 } else { 131 errortype = tr("Ways share segment with area"); 132 type = OVERLAPPING_WAY_AREA; 133 } 134 } else if (highway == ways) { 135 errortype = tr("Overlapping highways"); 136 type = OVERLAPPING_HIGHWAY; 137 } else if (railway == ways) { 138 errortype = tr("Overlapping railways"); 139 type = OVERLAPPING_RAILWAY; 140 } else { 141 errortype = tr("Overlapping ways"); 142 type = OVERLAPPING_WAY; 143 } 144 145 preliminaryErrors.add(new TestError(this, 146 type < OVERLAPPING_HIGHWAY_AREA ? Severity.WARNING : Severity.OTHER, 147 errortype, type, prims, duplicated)); 148 seenWays.put(currentWays, duplicated); 149 } else { /* way seen, mark highlight layer only */ 150 for (WaySegment ws : duplicated) { 151 highlight.add(ws); 152 } 153 } 154 } 155 } 156 157 // see ticket #9598 - only report if at least 3 segments are shared, except for overlapping ways, i.e warnings (see #9820) 158 for (TestError error : preliminaryErrors) { 159 if (error.getSeverity().equals(Severity.WARNING) || error.getHighlighted().size() / error.getPrimitives().size() >= 3) { 160 boolean ignore = false; 161 for (String ignoredKey : IGNORED_KEYS.get()) { 162 if (Utils.exists(error.getPrimitives(), Predicates.hasKey(ignoredKey))) { 163 ignore = true; 164 break; 165 } 166 } 167 if (!ignore) { 168 errors.add(error); 169 } 170 } 171 } 172 173 super.endTest(); 174 nodePairs = null; 175 } 176 177 protected static Set<WaySegment> checkDuplicateWaySegment(Way w) { 178 // test for ticket #4959 179 Set<WaySegment> segments = new TreeSet<>(new Comparator<WaySegment>() { 180 @Override 181 public int compare(WaySegment o1, WaySegment o2) { 182 final List<Node> n1 = Arrays.asList(o1.getFirstNode(), o1.getSecondNode()); 183 final List<Node> n2 = Arrays.asList(o2.getFirstNode(), o2.getSecondNode()); 184 Collections.sort(n1); 185 Collections.sort(n2); 186 final int first = n1.get(0).compareTo(n2.get(0)); 187 final int second = n1.get(1).compareTo(n2.get(1)); 188 return first != 0 ? first : second; 189 } 190 }); 191 final Set<WaySegment> duplicateWaySegments = new HashSet<>(); 192 193 for (int i = 0; i < w.getNodesCount() - 1; i++) { 194 final WaySegment segment = new WaySegment(w, i); 195 final boolean wasInSet = !segments.add(segment); 196 if (wasInSet) { 197 duplicateWaySegments.add(segment); 198 } 199 } 200 if (duplicateWaySegments.size() > 1) { 201 return duplicateWaySegments; 202 } else { 203 return null; 204 } 205 } 206 207 @Override 208 public void visit(Way w) { 209 210 final Set<WaySegment> duplicateWaySegment = checkDuplicateWaySegment(w); 211 if (duplicateWaySegment != null) { 212 errors.add(new TestError(this, Severity.ERROR, tr("Way contains segment twice"), 213 DUPLICATE_WAY_SEGMENT, Collections.singleton(w), duplicateWaySegment)); 214 return; 215 } 216 217 Node lastN = null; 218 int i = -2; 219 for (Node n : w.getNodes()) { 220 i++; 221 if (lastN == null) { 222 lastN = n; 223 continue; 224 } 225 nodePairs.put(Pair.sort(new Pair<>(lastN, n)), 226 new WaySegment(w, i)); 227 lastN = n; 228 } 229 } 230}