| | 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.awt.geom.Area; |
| | 7 | import java.awt.geom.Path2D; |
| | 8 | import java.awt.geom.PathIterator; |
| | 9 | import java.util.ArrayList; |
| | 10 | import java.util.List; |
| | 11 | |
| | 12 | import org.openstreetmap.josm.data.coor.EastNorth; |
| | 13 | import org.openstreetmap.josm.data.osm.Node; |
| | 14 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 15 | import org.openstreetmap.josm.data.osm.Relation; |
| | 16 | import org.openstreetmap.josm.data.osm.Way; |
| | 17 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon; |
| | 18 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache; |
| | 19 | import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper; |
| | 20 | import org.openstreetmap.josm.data.validation.Severity; |
| | 21 | import org.openstreetmap.josm.data.validation.Test; |
| | 22 | import org.openstreetmap.josm.data.validation.TestError; |
| | 23 | import org.openstreetmap.josm.gui.progress.ProgressMonitor; |
| | 24 | import org.openstreetmap.josm.tools.Geometry; |
| | 25 | import org.openstreetmap.josm.tools.Geometry.PolygonIntersection; |
| | 26 | import org.openstreetmap.josm.tools.Pair; |
| | 27 | |
| | 28 | /** |
| | 29 | * Tests if there are overlapping areas |
| | 30 | * |
| | 31 | * @author Gerd Petermann |
| | 32 | * @since xxx |
| | 33 | */ |
| | 34 | public class OverlappingAreas extends Test { |
| | 35 | protected static final int OVERLAPPING_AREA = 3800; // allows insideness |
| | 36 | protected static final int OVERLAPPING_WATER = 3801; |
| | 37 | protected static final int OVERLAPPING_IDENTICAL_NATURAL = 3802; |
| | 38 | protected static final int OVERLAPPING_IDENTICAL_LANDLUSE = 3803; |
| | 39 | protected static final int OVERLAPPING_BUILDINGS = 3804; |
| | 40 | protected static final int OVERLAPPING_BUILDING_RESIDENTIAL= 3805; |
| | 41 | |
| | 42 | private List<Way> areaWays; |
| | 43 | private List<Relation> multipolygons; |
| | 44 | private String reason; |
| | 45 | private int code; |
| | 46 | |
| | 47 | /** Constructor */ |
| | 48 | public OverlappingAreas() { |
| | 49 | super(tr("Overlapping areas"), |
| | 50 | tr("This test checks if two areas intersect in the same layer.")); |
| | 51 | } |
| | 52 | |
| | 53 | @Override |
| | 54 | public void startTest(ProgressMonitor monitor) { |
| | 55 | super.startTest(monitor); |
| | 56 | areaWays = new ArrayList<>(); |
| | 57 | multipolygons = new ArrayList<>(); |
| | 58 | } |
| | 59 | |
| | 60 | @Override |
| | 61 | public void endTest() { |
| | 62 | // way-way overlaps |
| | 63 | for (int i = 0; i < areaWays.size(); i++) { |
| | 64 | Way w1 = areaWays.get(i); |
| | 65 | Area a1 = null; |
| | 66 | for (int j = i + 1; j < areaWays.size(); j++) { |
| | 67 | Way w2 = areaWays.get(j); |
| | 68 | resetErrorDetails(); |
| | 69 | if (boundsIntersect(w1, w2) && !tagsAllowOverlap(w1, w2)) { |
| | 70 | if (a1 == null) { |
| | 71 | a1 = getEastNorthArea(w1); |
| | 72 | } |
| | 73 | checkIntersection(a1, getEastNorthArea(w2), w1, w2); |
| | 74 | } |
| | 75 | } |
| | 76 | } |
| | 77 | for (int i = 0; i < multipolygons.size(); i++) { |
| | 78 | Relation r1 = multipolygons.get(i); |
| | 79 | Area a1 = null; |
| | 80 | // multipolygon -way overlaps |
| | 81 | for (Way way : areaWays) { |
| | 82 | if (way.referrers(Relation.class).anyMatch(parent -> parent == r1)) { |
| | 83 | // ignore members of multipolygon |
| | 84 | continue; |
| | 85 | } |
| | 86 | resetErrorDetails(); |
| | 87 | if (boundsIntersect(r1, way) && !tagsAllowOverlap(r1, way)) { |
| | 88 | if (a1 == null) { |
| | 89 | a1 = getEastNorthArea(r1); |
| | 90 | } |
| | 91 | checkIntersection(a1, getEastNorthArea(way), r1, way); |
| | 92 | } |
| | 93 | } |
| | 94 | for (int j = i+1; j < multipolygons.size(); j++) { |
| | 95 | Relation r2 = multipolygons.get(j); |
| | 96 | resetErrorDetails(); |
| | 97 | if (boundsIntersect(r1, r2) && !tagsAllowOverlap(r1, r2)) { |
| | 98 | if (a1 == null) { |
| | 99 | a1 = getEastNorthArea(r1); |
| | 100 | } |
| | 101 | checkIntersection(a1, getEastNorthArea(r2), r1, r2); |
| | 102 | } |
| | 103 | } |
| | 104 | |
| | 105 | } |
| | 106 | // clean up memory |
| | 107 | areaWays = null; |
| | 108 | multipolygons = null; |
| | 109 | super.endTest(); |
| | 110 | } |
| | 111 | |
| | 112 | private static boolean boundsIntersect(OsmPrimitive p1, OsmPrimitive p2) { |
| | 113 | return p1.getBBox().intersects(p2.getBBox()); |
| | 114 | } |
| | 115 | |
| | 116 | private static Area getEastNorthArea(OsmPrimitive p) { |
| | 117 | if (p instanceof Way) { |
| | 118 | return Geometry.getArea(((Way) p).getNodes()); |
| | 119 | } |
| | 120 | Multipolygon mp = MultipolygonCache.getInstance().get((Relation) p); |
| | 121 | Path2D path = new Path2D.Double(); |
| | 122 | path.setWindingRule(Path2D.WIND_EVEN_ODD); |
| | 123 | for (Multipolygon.PolyData pd : mp.getCombinedPolygons()) { |
| | 124 | path.append(pd.get(), false); |
| | 125 | } |
| | 126 | return new Area(path); |
| | 127 | |
| | 128 | } |
| | 129 | |
| | 130 | /** |
| | 131 | * Check intersection between two areas. If empty or extremely small ignore it, else add error |
| | 132 | * with highlighted area. |
| | 133 | * @param a1 the first area (EastNorth) |
| | 134 | * @param a2 the second area (EastNorth) |
| | 135 | * @param p1 primitive describing 1st polygon |
| | 136 | * @param p2 primitive describing 2nd polygon |
| | 137 | */ |
| | 138 | private void checkIntersection(Area a1, Area a2, OsmPrimitive p1, OsmPrimitive p2) { |
| | 139 | Pair<PolygonIntersection, Area> pair = Geometry.polygonIntersectionResult(a1, a2, Geometry.INTERSECTION_EPS_EAST_NORTH); |
| | 140 | switch (pair.a) { |
| | 141 | case OUTSIDE: |
| | 142 | return; |
| | 143 | case FIRST_INSIDE_SECOND: |
| | 144 | if (code == OVERLAPPING_AREA || code == OVERLAPPING_BUILDING_RESIDENTIAL && isBuilding(p1)) |
| | 145 | return; |
| | 146 | if (code == OVERLAPPING_WATER && p2.hasTag("natural","wetland")) |
| | 147 | return; |
| | 148 | break; |
| | 149 | case SECOND_INSIDE_FIRST: |
| | 150 | if (code == OVERLAPPING_AREA || code == OVERLAPPING_BUILDING_RESIDENTIAL && isBuilding(p2)) |
| | 151 | return; |
| | 152 | if (code == OVERLAPPING_WATER && p1.hasTag("natural","wetland")) |
| | 153 | return; |
| | 154 | break; |
| | 155 | case CROSSING: |
| | 156 | break; |
| | 157 | } |
| | 158 | |
| | 159 | // we have an intersection, calculate the elements to highlight |
| | 160 | PathIterator pit = pair.b.getPathIterator(null); |
| | 161 | double[] res = new double[6]; |
| | 162 | List<List<Node>> hilite = new ArrayList<>(); |
| | 163 | List<Node> nodes = new ArrayList<>(); |
| | 164 | while (!pit.isDone()) { |
| | 165 | int type = pit.currentSegment(res); |
| | 166 | Node n = new Node(new EastNorth(res[0], res[1])); |
| | 167 | switch (type) { |
| | 168 | case PathIterator.SEG_MOVETO: |
| | 169 | if (!nodes.isEmpty()) { |
| | 170 | hilite.add(nodes); |
| | 171 | } |
| | 172 | nodes = new ArrayList<>(); |
| | 173 | nodes.add(n); |
| | 174 | break; |
| | 175 | case PathIterator.SEG_LINETO: |
| | 176 | nodes.add(n); |
| | 177 | break; |
| | 178 | case PathIterator.SEG_CLOSE: |
| | 179 | if (!nodes.isEmpty()) { |
| | 180 | hilite.add(nodes); |
| | 181 | nodes = new ArrayList<>(); |
| | 182 | } |
| | 183 | break; |
| | 184 | default: |
| | 185 | break; |
| | 186 | } |
| | 187 | pit.next(); |
| | 188 | } |
| | 189 | if (nodes.size() > 1) { |
| | 190 | hilite.add(nodes); |
| | 191 | } |
| | 192 | |
| | 193 | switch (code) { |
| | 194 | case OVERLAPPING_WATER: |
| | 195 | reason = tr("Overlapping Water Areas"); |
| | 196 | break; |
| | 197 | case OVERLAPPING_IDENTICAL_NATURAL: |
| | 198 | reason = tr("Overlapping identical natural areas"); |
| | 199 | break; |
| | 200 | case OVERLAPPING_IDENTICAL_LANDLUSE: |
| | 201 | reason = tr("Overlapping identical landuses"); |
| | 202 | break; |
| | 203 | case OVERLAPPING_BUILDINGS: |
| | 204 | if (pair.a == PolygonIntersection.CROSSING) |
| | 205 | reason = tr("Overlapping buildings"); |
| | 206 | else |
| | 207 | reason = tr("Building inside building"); |
| | 208 | break; |
| | 209 | case OVERLAPPING_BUILDING_RESIDENTIAL: |
| | 210 | reason = tr("Overlapping building/residential area"); |
| | 211 | break; |
| | 212 | default: |
| | 213 | reason = tr("Overlapping area"); |
| | 214 | |
| | 215 | } |
| | 216 | errors.add(TestError |
| | 217 | .builder(this, code == OVERLAPPING_AREA ? Severity.OTHER : Severity.WARNING, code) |
| | 218 | .message("OA: " + reason).primitives(p1, p2) |
| | 219 | .highlightNodePairs(hilite).build()); |
| | 220 | } |
| | 221 | |
| | 222 | /** |
| | 223 | * Check if the two objects are allowed to overlap regarding tags. |
| | 224 | * @param p1 1st primitive |
| | 225 | * @param p2 2nd primitive |
| | 226 | * @return true if the tags of the objects allow that the areas overlap. |
| | 227 | */ |
| | 228 | private boolean tagsAllowOverlap(OsmPrimitive p1, OsmPrimitive p2) { |
| | 229 | if (!inSameLayer(p1, p2)) { |
| | 230 | return true; |
| | 231 | } |
| | 232 | if (isWaterArea(p1) && isWaterArea(p2)) { |
| | 233 | code = OVERLAPPING_WATER; |
| | 234 | return false; |
| | 235 | } |
| | 236 | if (isSetAndEqual(p1, p2, "natural")) { |
| | 237 | code = OVERLAPPING_IDENTICAL_NATURAL; |
| | 238 | return false; |
| | 239 | } |
| | 240 | if (isSetAndEqual(p1, p2, "landuse")) { |
| | 241 | code = OVERLAPPING_IDENTICAL_LANDLUSE; |
| | 242 | return false; |
| | 243 | } |
| | 244 | if (isBuilding(p1) && isBuilding(p2)) { |
| | 245 | code = OVERLAPPING_BUILDINGS; |
| | 246 | return false; |
| | 247 | } |
| | 248 | if (isBuilding(p1) && isResidentialArea(p2) || isBuilding(p2) && isResidentialArea(p1)) { |
| | 249 | code = OVERLAPPING_BUILDING_RESIDENTIAL; |
| | 250 | return false; |
| | 251 | } |
| | 252 | if (ValidatorPrefHelper.PREF_OTHER.get() && p1.concernsArea() && p2.concernsArea()) { |
| | 253 | code = OVERLAPPING_AREA; |
| | 254 | return false; |
| | 255 | } |
| | 256 | return true; |
| | 257 | } |
| | 258 | |
| | 259 | private static boolean isWaterArea(OsmPrimitive p) { |
| | 260 | return p.hasTag("natural", "water", "wetland") || p.hasTag("landuse","reservoir"); |
| | 261 | } |
| | 262 | |
| | 263 | private static boolean isSetAndEqual(OsmPrimitive p1, OsmPrimitive p2, String key) { |
| | 264 | String v1 = p1.get(key); |
| | 265 | String v2 = p2.get(key); |
| | 266 | return v1 != null && v1.equals(v2); |
| | 267 | } |
| | 268 | |
| | 269 | private void resetErrorDetails() { |
| | 270 | reason = null; |
| | 271 | code = -1; |
| | 272 | } |
| | 273 | |
| | 274 | @Override |
| | 275 | public void visit(Way w) { |
| | 276 | if (w.isArea()) { |
| | 277 | areaWays.add(w); |
| | 278 | } |
| | 279 | } |
| | 280 | |
| | 281 | @Override |
| | 282 | public void visit(Relation r) { |
| | 283 | if (r.isMultipolygon()) { |
| | 284 | multipolygons.add(r); |
| | 285 | } |
| | 286 | } |
| | 287 | } |