| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.data.validation.tests; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.data.validation.tests.CrossingWays.HIGHWAY; |
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 6 | |
|---|
| 7 | import java.util.List; |
|---|
| 8 | |
|---|
| 9 | import org.openstreetmap.josm.data.osm.Node; |
|---|
| 10 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
|---|
| 11 | import org.openstreetmap.josm.data.osm.Relation; |
|---|
| 12 | import org.openstreetmap.josm.data.osm.Way; |
|---|
| 13 | import org.openstreetmap.josm.data.validation.Severity; |
|---|
| 14 | import org.openstreetmap.josm.data.validation.Test; |
|---|
| 15 | import org.openstreetmap.josm.data.validation.TestError; |
|---|
| 16 | import org.openstreetmap.josm.gui.mappaint.ElemStyles; |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * Checks for ways connected to areas. |
|---|
| 20 | * @since 4682 |
|---|
| 21 | */ |
|---|
| 22 | public class WayConnectedToArea extends Test { |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Constructs a new {@code WayConnectedToArea} test. |
|---|
| 26 | */ |
|---|
| 27 | public WayConnectedToArea() { |
|---|
| 28 | super(tr("Way connected to Area"), tr("Checks for ways connected to areas.")); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | @Override |
|---|
| 32 | public void visit(Way w) { |
|---|
| 33 | if (!w.isUsable() || w.isClosed() || !w.hasKey(HIGHWAY)) { |
|---|
| 34 | return; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | List<OsmPrimitive> r = w.firstNode().getReferrers(); |
|---|
| 38 | boolean hasway = r.stream().anyMatch(p -> p != w && p.hasKey(HIGHWAY)); |
|---|
| 39 | if (!hasway) { |
|---|
| 40 | for (OsmPrimitive p : r) { |
|---|
| 41 | testForError(w, w.firstNode(), p); |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | r = w.lastNode().getReferrers(); |
|---|
| 45 | hasway = r.stream().anyMatch(p -> p != w && p.hasKey(HIGHWAY)); |
|---|
| 46 | if (!hasway) { |
|---|
| 47 | for (OsmPrimitive p : r) { |
|---|
| 48 | testForError(w, w.lastNode(), p); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | private void testForError(Way w, Node wayNode, OsmPrimitive p) { |
|---|
| 54 | if (wayNode.isOutsideDownloadArea() |
|---|
| 55 | || wayNode.getReferrers().stream().anyMatch(p1 -> p1.hasTag("route", "ferry"))) { |
|---|
| 56 | return; |
|---|
| 57 | } else if (isArea(p)) { |
|---|
| 58 | addPossibleError(w, wayNode, p, p); |
|---|
| 59 | } else { |
|---|
| 60 | p.referrers(Relation.class) |
|---|
| 61 | .filter(r -> r.isMultipolygon() && isArea(r)) |
|---|
| 62 | .findFirst() |
|---|
| 63 | .ifPresent(r -> addPossibleError(w, wayNode, p, r)); |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | private static boolean isArea(OsmPrimitive p) { |
|---|
| 68 | return p.hasKey("landuse", "natural") && ElemStyles.hasAreaElemStyle(p, false); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | private void addPossibleError(Way w, Node wayNode, OsmPrimitive p, OsmPrimitive area) { |
|---|
| 72 | // Avoid "legal" cases (see #10655) |
|---|
| 73 | if (w.hasKey(HIGHWAY) && wayNode.hasTag("leisure", "slipway") && area.hasTag("natural", "water")) { |
|---|
| 74 | return; |
|---|
| 75 | } |
|---|
| 76 | if (wayNode.hasTag("noexit", "yes")) { |
|---|
| 77 | // Avoid "legal" case (see #17036) |
|---|
| 78 | return; |
|---|
| 79 | } |
|---|
| 80 | errors.add(TestError.builder(this, Severity.WARNING, 2301) |
|---|
| 81 | .message(tr("Way terminates on Area")) |
|---|
| 82 | .primitives(w, p) |
|---|
| 83 | .highlight(wayNode) |
|---|
| 84 | .build()); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|