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