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

Last change on this file since 14214 was 14214, checked in by Don-vip, 6 years ago

sonarqube - squid:S4551 - Enum values should be compared with "=="

  • Property svn:eol-style set to native
File size: 8.9 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.ListProperty;
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 ListProperty IGNORED_KEYS = new ListProperty(
53 "overlapping-ways.ignored-keys", Arrays.asList(
54 "barrier", "building", "historic:building", "demolished:building",
55 "removed:building", "disused:building", "abandoned:building", "proposed:building", "man_made"));
56
57 /** Constructor */
58 public OverlappingWays() {
59 super(tr("Overlapping ways"),
60 tr("This test checks that a connection between two nodes "
61 + "is not used by more than one way."));
62 }
63
64 @Override
65 public void startTest(ProgressMonitor monitor) {
66 super.startTest(monitor);
67 nodePairs = new MultiMap<>(1000);
68 }
69
70 private static boolean parentMultipolygonConcernsArea(OsmPrimitive p) {
71 for (Relation r : OsmPrimitive.getFilteredList(p.getReferrers(), Relation.class)) {
72 if (r.concernsArea()) {
73 return true;
74 }
75 }
76 return false;
77 }
78
79 @Override
80 public void endTest() {
81 Map<List<Way>, Set<WaySegment>> seenWays = new HashMap<>(500);
82
83 Collection<TestError> preliminaryErrors = new ArrayList<>();
84 for (Set<WaySegment> duplicated : nodePairs.values()) {
85 int ways = duplicated.size();
86
87 if (ways > 1) {
88 List<OsmPrimitive> prims = new ArrayList<>();
89 List<Way> currentWays = new ArrayList<>();
90 Collection<WaySegment> highlight;
91 int highway = 0;
92 int railway = 0;
93 int area = 0;
94
95 for (WaySegment ws : duplicated) {
96 if (ws.way.hasKey(HIGHWAY)) {
97 highway++;
98 } else if (ws.way.hasKey(RAILWAY)) {
99 railway++;
100 }
101 Boolean ar = OsmUtils.getOsmBoolean(ws.way.get("area"));
102 if (ar != null && ar) {
103 area++;
104 }
105 if (ws.way.concernsArea() || parentMultipolygonConcernsArea(ws.way)) {
106 area++;
107 ways--;
108 }
109
110 prims.add(ws.way);
111 currentWays.add(ws.way);
112 }
113 // These ways not seen before
114 // If two or more of the overlapping ways are highways or railways mark a separate error
115 if ((highlight = seenWays.get(currentWays)) == null) {
116 String errortype;
117 int type;
118
119 if (area > 0) {
120 if (ways == 0 || duplicated.size() == area) {
121 errortype = tr("Areas share segment");
122 type = OVERLAPPING_AREA;
123 } else if (highway == ways) {
124 errortype = tr("Highways share segment with area");
125 type = OVERLAPPING_HIGHWAY_AREA;
126 } else if (railway == ways) {
127 errortype = tr("Railways share segment with area");
128 type = OVERLAPPING_RAILWAY_AREA;
129 } else {
130 errortype = tr("Ways share segment with area");
131 type = OVERLAPPING_WAY_AREA;
132 }
133 } else if (highway == ways) {
134 errortype = tr("Overlapping highways");
135 type = OVERLAPPING_HIGHWAY;
136 } else if (railway == ways) {
137 errortype = tr("Overlapping railways");
138 type = OVERLAPPING_RAILWAY;
139 } else {
140 errortype = tr("Overlapping ways");
141 type = OVERLAPPING_WAY;
142 }
143
144 Severity severity = type < OVERLAPPING_HIGHWAY_AREA ? Severity.WARNING : Severity.OTHER;
145 preliminaryErrors.add(TestError.builder(this, severity, type)
146 .message(errortype)
147 .primitives(prims)
148 .highlightWaySegments(duplicated)
149 .build());
150 seenWays.put(currentWays, duplicated);
151 } else { /* way seen, mark highlight layer only */
152 highlight.addAll(duplicated);
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() == Severity.WARNING || error.getHighlighted().size() / error.getPrimitives().size() >= 3) {
160 boolean ignore = false;
161 for (String ignoredKey : IGNORED_KEYS.get()) {
162 if (error.getPrimitives().stream().anyMatch(p -> p.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<>((o1, o2) -> {
180 final List<Node> n1 = Arrays.asList(o1.getFirstNode(), o1.getSecondNode());
181 final List<Node> n2 = Arrays.asList(o2.getFirstNode(), o2.getSecondNode());
182 Collections.sort(n1);
183 Collections.sort(n2);
184 final int first = n1.get(0).compareTo(n2.get(0));
185 final int second = n1.get(1).compareTo(n2.get(1));
186 return first != 0 ? first : second;
187 });
188 final Set<WaySegment> duplicateWaySegments = new HashSet<>();
189
190 for (int i = 0; i < w.getNodesCount() - 1; i++) {
191 final WaySegment segment = new WaySegment(w, i);
192 final boolean wasInSet = !segments.add(segment);
193 if (wasInSet) {
194 duplicateWaySegments.add(segment);
195 }
196 }
197 if (duplicateWaySegments.size() > 1) {
198 return duplicateWaySegments;
199 } else {
200 return null;
201 }
202 }
203
204 @Override
205 public void visit(Way w) {
206
207 final Set<WaySegment> duplicateWaySegment = checkDuplicateWaySegment(w);
208 if (duplicateWaySegment != null) {
209 errors.add(TestError.builder(this, Severity.ERROR, DUPLICATE_WAY_SEGMENT)
210 .message(tr("Way contains segment twice"))
211 .primitives(w)
212 .highlightWaySegments(duplicateWaySegment)
213 .build());
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}
Note: See TracBrowser for help on using the repository browser.