| | 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.HashSet; |
| | 7 | import java.util.List; |
| | 8 | import java.util.Set; |
| | 9 | |
| | 10 | import org.openstreetmap.josm.data.osm.Node; |
| | 11 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 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.progress.ProgressMonitor; |
| | 17 | |
| | 18 | /** |
| | 19 | * Checks for buildings that have a points shared with a different objec |
| | 20 | */ |
| | 21 | public class BuildingSharingPointWith extends Test { |
| | 22 | static final String HIGHWAY = "highway"; |
| | 23 | |
| | 24 | private Set<Way> visitedWays; |
| | 25 | |
| | 26 | /** |
| | 27 | * Constructs a new {@code BuildingSharingPointWith} test. |
| | 28 | */ |
| | 29 | public BuildingSharingPointWith() { |
| | 30 | super(tr("Building sharing points"), tr("Checks for building nodes being shared.")); |
| | 31 | } |
| | 32 | |
| | 33 | @Override |
| | 34 | public void visit(Way w) { |
| | 35 | if (!w.isUsable() || !w.isClosed() || !isBuilding(w)) return; |
| | 36 | |
| | 37 | visitedWays.add(w); |
| | 38 | |
| | 39 | Set<OsmPrimitive> visitedPrimitives = new HashSet<>(); |
| | 40 | for (Node n: w.getNodes()) { |
| | 41 | List<OsmPrimitive> r = n.getReferrers(); |
| | 42 | |
| | 43 | for (OsmPrimitive p : r) { |
| | 44 | if (p != w && !visitedWays.contains(p) |
| | 45 | && !visitedPrimitives.contains(p) |
| | 46 | && isConflictCandidate(p)) |
| | 47 | { |
| | 48 | addError(w, p, n); |
| | 49 | visitedPrimitives.add(p); |
| | 50 | } |
| | 51 | } |
| | 52 | } |
| | 53 | } |
| | 54 | |
| | 55 | @Override |
| | 56 | public void startTest(ProgressMonitor monitor) { |
| | 57 | super.startTest(monitor); |
| | 58 | |
| | 59 | visitedWays = new HashSet<>(); |
| | 60 | } |
| | 61 | |
| | 62 | @Override |
| | 63 | public void endTest() { |
| | 64 | super.endTest(); |
| | 65 | |
| | 66 | visitedWays = null; |
| | 67 | } |
| | 68 | |
| | 69 | private boolean isConflictCandidate(OsmPrimitive p) { |
| | 70 | return isBuilding(p) || isResidentialArea(p) || p.hasTag(HIGHWAY); |
| | 71 | } |
| | 72 | |
| | 73 | private String getMessage(OsmPrimitive p) { |
| | 74 | if (isBuilding(p)) |
| | 75 | return tr("Building sharing point with a building"); |
| | 76 | else if(isResidentialArea(p)) |
| | 77 | return tr("Building sharing point with a residential area"); |
| | 78 | else if (p.hasTag(HIGHWAY)) |
| | 79 | return tr("Building sharing point with a highway"); |
| | 80 | else |
| | 81 | throw new IllegalArgumentException(); |
| | 82 | } |
| | 83 | |
| | 84 | private void addError(Way building, OsmPrimitive conflicting, Node sharedPoint) { |
| | 85 | errors.add(TestError.builder(this, Severity.WARNING, 3801) |
| | 86 | .message(getMessage(conflicting)) |
| | 87 | .primitives(building, sharedPoint) |
| | 88 | .highlight(building) |
| | 89 | .build()); |
| | 90 | } |
| | 91 | } |