source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/OverlappingWays.java@ 12718

Last change on this file since 12718 was 11913, checked in by Don-vip, 7 years ago

sonar - squid:S1192 - String literals should not be duplicated

  • Property svn:eol-style set to native
File size: 8.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.openstreetmap.josm.data.validation.tests.CrossingWays.HIGHWAY;
5import static org.openstreetmap.josm.data.validation.tests.CrossingWays.RAILWAY;
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.HashMap;
13import java.util.HashSet;
14import java.util.List;
15import java.util.Map;
16import java.util.Set;
17import java.util.TreeSet;
18
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.OsmUtils;
22import org.openstreetmap.josm.data.osm.Relation;
23import org.openstreetmap.josm.data.osm.Way;
24import org.openstreetmap.josm.data.osm.WaySegment;
25import org.openstreetmap.josm.data.preferences.CollectionProperty;
26import org.openstreetmap.josm.data.validation.Severity;
27import org.openstreetmap.josm.data.validation.Test;
28import org.openstreetmap.josm.data.validation.TestError;
29import org.openstreetmap.josm.gui.progress.ProgressMonitor;
30import org.openstreetmap.josm.tools.MultiMap;
31import org.openstreetmap.josm.tools.Pair;
32
33/**
34 * Tests if there are overlapping ways.
35 *
36 * @author frsantos
37 */
38public class OverlappingWays extends Test {
39
40 /** Bag of all way segments */
41 private MultiMap<Pair<Node, Node>, WaySegment> nodePairs;
42
43 protected static final int OVERLAPPING_HIGHWAY = 101;
44 protected static final int OVERLAPPING_RAILWAY = 102;
45 protected static final int OVERLAPPING_WAY = 103;
46 protected static final int OVERLAPPING_HIGHWAY_AREA = 111;
47 protected static final int OVERLAPPING_RAILWAY_AREA = 112;
48 protected static final int OVERLAPPING_WAY_AREA = 113;
49 protected static final int OVERLAPPING_AREA = 120;
50 protected static final int DUPLICATE_WAY_SEGMENT = 121;
51
52 protected static final CollectionProperty IGNORED_KEYS = new CollectionProperty(
53 "overlapping-ways.ignored-keys", Arrays.asList("barrier", "building", "historic:building", "man_made"));
54
55 /** Constructor */
56 public OverlappingWays() {
57 super(tr("Overlapping ways"),
58 tr("This test checks that a connection between two nodes "
59 + "is not used by more than one way."));
60 }
61
62 @Override
63 public void startTest(ProgressMonitor monitor) {
64 super.startTest(monitor);
65 nodePairs = new MultiMap<>(1000);
66 }
67
68 private static boolean parentMultipolygonConcernsArea(OsmPrimitive p) {
69 for (Relation r : OsmPrimitive.getFilteredList(p.getReferrers(), Relation.class)) {
70 if (r.concernsArea()) {
71 return true;
72 }
73 }
74 return false;
75 }
76
77 @Override
78 public void endTest() {
79 Map<List<Way>, Set<WaySegment>> seenWays = new HashMap<>(500);
80
81 Collection<TestError> preliminaryErrors = new ArrayList<>();
82 for (Set<WaySegment> duplicated : nodePairs.values()) {
83 int ways = duplicated.size();
84
85 if (ways > 1) {
86 List<OsmPrimitive> prims = new ArrayList<>();
87 List<Way> currentWays = new ArrayList<>();
88 Collection<WaySegment> highlight;
89 int highway = 0;
90 int railway = 0;
91 int area = 0;
92
93 for (WaySegment ws : duplicated) {
94 if (ws.way.hasKey(HIGHWAY)) {
95 highway++;
96 } else if (ws.way.hasKey(RAILWAY)) {
97 railway++;
98 }
99 Boolean ar = OsmUtils.getOsmBoolean(ws.way.get("area"));
100 if (ar != null && ar) {
101 area++;
102 }
103 if (ws.way.concernsArea() || parentMultipolygonConcernsArea(ws.way)) {
104 area++;
105 ways--;
106 }
107
108 prims.add(ws.way);
109 currentWays.add(ws.way);
110 }
111 // These ways not seen before
112 // If two or more of the overlapping ways are highways or railways mark a separate error
113 if ((highlight = seenWays.get(currentWays)) == null) {
114 String errortype;
115 int type;
116
117 if (area > 0) {
118 if (ways == 0 || duplicated.size() == area) {
119 errortype = tr("Areas share segment");
120 type = OVERLAPPING_AREA;
121 } else if (highway == ways) {
122 errortype = tr("Highways share segment with area");
123 type = OVERLAPPING_HIGHWAY_AREA;
124 } else if (railway == ways) {
125 errortype = tr("Railways share segment with area");
126 type = OVERLAPPING_RAILWAY_AREA;
127 } else {
128 errortype = tr("Ways share segment with area");
129 type = OVERLAPPING_WAY_AREA;
130 }
131 } else if (highway == ways) {
132 errortype = tr("Overlapping highways");
133 type = OVERLAPPING_HIGHWAY;
134 } else if (railway == ways) {
135 errortype = tr("Overlapping railways");
136 type = OVERLAPPING_RAILWAY;
137 } else {
138 errortype = tr("Overlapping ways");
139 type = OVERLAPPING_WAY;
140 }
141
142 Severity severity = type < OVERLAPPING_HIGHWAY_AREA ? Severity.WARNING : Severity.OTHER;
143 preliminaryErrors.add(TestError.builder(this, severity, type)
144 .message(errortype)
145 .primitives(prims)
146 .highlightWaySegments(duplicated)
147 .build());
148 seenWays.put(currentWays, duplicated);
149 } else { /* way seen, mark highlight layer only */
150 highlight.addAll(duplicated);
151 }
152 }
153 }
154
155 // see ticket #9598 - only report if at least 3 segments are shared, except for overlapping ways, i.e warnings (see #9820)
156 for (TestError error : preliminaryErrors) {
157 if (error.getSeverity().equals(Severity.WARNING) || error.getHighlighted().size() / error.getPrimitives().size() >= 3) {
158 boolean ignore = false;
159 for (String ignoredKey : IGNORED_KEYS.get()) {
160 if (error.getPrimitives().stream().anyMatch(p -> p.hasKey(ignoredKey))) {
161 ignore = true;
162 break;
163 }
164 }
165 if (!ignore) {
166 errors.add(error);
167 }
168 }
169 }
170
171 super.endTest();
172 nodePairs = null;
173 }
174
175 protected static Set<WaySegment> checkDuplicateWaySegment(Way w) {
176 // test for ticket #4959
177 Set<WaySegment> segments = new TreeSet<>((o1, o2) -> {
178 final List<Node> n1 = Arrays.asList(o1.getFirstNode(), o1.getSecondNode());
179 final List<Node> n2 = Arrays.asList(o2.getFirstNode(), o2.getSecondNode());
180 Collections.sort(n1);
181 Collections.sort(n2);
182 final int first = n1.get(0).compareTo(n2.get(0));
183 final int second = n1.get(1).compareTo(n2.get(1));
184 return first != 0 ? first : second;
185 });
186 final Set<WaySegment> duplicateWaySegments = new HashSet<>();
187
188 for (int i = 0; i < w.getNodesCount() - 1; i++) {
189 final WaySegment segment = new WaySegment(w, i);
190 final boolean wasInSet = !segments.add(segment);
191 if (wasInSet) {
192 duplicateWaySegments.add(segment);
193 }
194 }
195 if (duplicateWaySegments.size() > 1) {
196 return duplicateWaySegments;
197 } else {
198 return null;
199 }
200 }
201
202 @Override
203 public void visit(Way w) {
204
205 final Set<WaySegment> duplicateWaySegment = checkDuplicateWaySegment(w);
206 if (duplicateWaySegment != null) {
207 errors.add(TestError.builder(this, Severity.ERROR, DUPLICATE_WAY_SEGMENT)
208 .message(tr("Way contains segment twice"))
209 .primitives(w)
210 .highlightWaySegments(duplicateWaySegment)
211 .build());
212 return;
213 }
214
215 Node lastN = null;
216 int i = -2;
217 for (Node n : w.getNodes()) {
218 i++;
219 if (lastN == null) {
220 lastN = n;
221 continue;
222 }
223 nodePairs.put(Pair.sort(new Pair<>(lastN, n)),
224 new WaySegment(w, i));
225 lastN = n;
226 }
227 }
228}
Note: See TracBrowser for help on using the repository browser.