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.ArrayList;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.QuadBuckets;
|
---|
11 | import org.openstreetmap.josm.data.osm.Way;
|
---|
12 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
13 | import org.openstreetmap.josm.data.validation.Test;
|
---|
14 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
15 | import org.openstreetmap.josm.gui.mappaint.ElemStyles;
|
---|
16 | import org.openstreetmap.josm.tools.Geometry;
|
---|
17 | import org.openstreetmap.josm.tools.Predicate;
|
---|
18 | import org.openstreetmap.josm.tools.Utils;
|
---|
19 |
|
---|
20 | public class OverlappingAreas extends Test {
|
---|
21 |
|
---|
22 | protected static final int OVERLAPPING_AREAS = 2201;
|
---|
23 | protected QuadBuckets<Way> index = new QuadBuckets<Way>();
|
---|
24 |
|
---|
25 | public OverlappingAreas() {
|
---|
26 | super(tr("Overlapping Areas"), tr("This test checks if areas overlap."));
|
---|
27 | }
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public void visit(Way w) {
|
---|
31 | if (w.isUsable() && w.isArea() && ElemStyles.hasAreaElemStyle(w, false)) {
|
---|
32 | index.add(w);
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public void endTest() {
|
---|
38 | for (final Way w : index) {
|
---|
39 | Collection<Way> overlaps = Utils.filter(
|
---|
40 | index.search(w.getBBox()),
|
---|
41 | new Predicate<Way>() {
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public boolean evaluate(Way wi) {
|
---|
45 | if (w.equals(wi))
|
---|
46 | return false;
|
---|
47 | else
|
---|
48 | return Geometry.polygonIntersection(w.getNodes(), wi.getNodes())
|
---|
49 | == Geometry.PolygonIntersection.CROSSING;
|
---|
50 | }
|
---|
51 | });
|
---|
52 | if (!overlaps.isEmpty()) {
|
---|
53 | Collection<Way> overlapsWater = new ArrayList<Way>();
|
---|
54 | Collection<Way> overlapsOther = new ArrayList<Way>();
|
---|
55 |
|
---|
56 | String natural1 = w.get("natural");
|
---|
57 | String landuse1 = w.get("landuse");
|
---|
58 | boolean isWaterArea = "water".equals(natural1) || "wetland".equals(natural1) || "coastline".equals(natural1) || "reservoir".equals(landuse1);
|
---|
59 | boolean isWaterArea2 = false;
|
---|
60 |
|
---|
61 | for (Way wayOther : overlaps) {
|
---|
62 | String natural2 = wayOther.get("natural");
|
---|
63 | String landuse2 = wayOther.get("landuse");
|
---|
64 | boolean isWaterAreaTest = "water".equals(natural2) || "wetland".equals(natural2) || "coastline".equals(natural2) || "reservoir".equals(landuse2);
|
---|
65 |
|
---|
66 | if (!isWaterArea2) {
|
---|
67 | isWaterArea2 = isWaterAreaTest;
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (isWaterArea && isWaterAreaTest) {
|
---|
71 | overlapsWater.add(wayOther);
|
---|
72 | } else {
|
---|
73 | overlapsOther.add(wayOther);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (!overlapsWater.isEmpty()) {
|
---|
78 | errors.add(new TestError(this, Severity.WARNING, tr("Overlapping Water Areas"),
|
---|
79 | OVERLAPPING_AREAS, Collections.singletonList(w), overlapsWater));
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (!overlapsOther.isEmpty()) {
|
---|
83 | errors.add(new TestError(this, Severity.OTHER, tr("Overlapping Areas"),
|
---|
84 | OVERLAPPING_AREAS, Collections.singletonList(w), overlapsOther));
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | super.endTest();
|
---|
90 | }
|
---|
91 |
|
---|
92 | }
|
---|