| | 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.List; |
| | 7 | |
| | 8 | import org.openstreetmap.josm.data.coor.EastNorth; |
| | 9 | import org.openstreetmap.josm.data.osm.Node; |
| | 10 | import org.openstreetmap.josm.data.osm.Way; |
| | 11 | import org.openstreetmap.josm.data.validation.Severity; |
| | 12 | import org.openstreetmap.josm.data.validation.Test; |
| | 13 | import org.openstreetmap.josm.data.validation.TestError; |
| | 14 | import org.openstreetmap.josm.spi.preferences.Config; |
| | 15 | import org.openstreetmap.josm.tools.Pair; |
| | 16 | |
| | 17 | /** |
| | 18 | * Checks for circular buildings that have a deformed shape. |
| | 19 | * |
| | 20 | * @since xxx |
| | 21 | */ |
| | 22 | public class BrokenCircleShape extends Test { |
| | 23 | /** |
| | 24 | * Constructs a new {@code RightAngleBuildingTest} test. |
| | 25 | */ |
| | 26 | public BrokenCircleShape() { |
| | 27 | super(tr("Broken circle shape"), |
| | 28 | tr("Checks for circular buildings that have a deformed shape.")); |
| | 29 | } |
| | 30 | |
| | 31 | @Override |
| | 32 | public void visit(Way w) { |
| | 33 | if (!w.isUsable() || !w.isClosed() || !isBuilding(w)) |
| | 34 | return; |
| | 35 | |
| | 36 | int count = w.getNodesCount() - 1; |
| | 37 | if (count <= 6) |
| | 38 | return; |
| | 39 | |
| | 40 | /* Identify that the shape has most of nodes organized in a circle. */ |
| | 41 | double circleAngle = 180 * (count - 2) / count; |
| | 42 | |
| | 43 | int matches = 0; |
| | 44 | double maximalAngleDifference = Config.getPref().getDouble("validator.BrokenCircleShape.maximalAngleDifference", 5); |
| | 45 | for (Pair<Double, Node> pair: w.getAngles()) |
| | 46 | if (Math.abs(pair.a - circleAngle) < maximalAngleDifference) |
| | 47 | matches++; |
| | 48 | |
| | 49 | /* Continue if most of the angles are fine. */ |
| | 50 | if (matches < count * 0.6) |
| | 51 | return; |
| | 52 | |
| | 53 | double minRatio = Config.getPref().getDouble("validator.BrokenCircleShape.minimalRatio", 0.1); |
| | 54 | |
| | 55 | /* Remove the last (closing) node of way. **/ |
| | 56 | List<Node> nodes = w.getNodes (); |
| | 57 | nodes.remove (nodes.size () - 1); |
| | 58 | |
| | 59 | double cx = 0; |
| | 60 | double cy = 0; |
| | 61 | |
| | 62 | for (Node n: nodes) { |
| | 63 | cx += n.getEastNorth().east(); |
| | 64 | cy += n.getEastNorth().north(); |
| | 65 | } |
| | 66 | |
| | 67 | Node center = new Node (new EastNorth(cx / count, cy / count)); |
| | 68 | |
| | 69 | double maxDistance = 0; |
| | 70 | double minDistance = Double.MAX_VALUE; |
| | 71 | |
| | 72 | for (Node n: nodes) { |
| | 73 | double distance = center.getEastNorth ().distance (n.getEastNorth ()); |
| | 74 | if (distance > maxDistance) |
| | 75 | maxDistance = distance; |
| | 76 | if (distance < minDistance) |
| | 77 | minDistance = distance; |
| | 78 | } |
| | 79 | |
| | 80 | if ((1.0 - minDistance / maxDistance) > minRatio) |
| | 81 | errors.add(TestError.builder(this, Severity.WARNING, 3901) |
| | 82 | .message(tr("Broken circular shape")) |
| | 83 | .primitives(w) |
| | 84 | .build()); |
| | 85 | } |
| | 86 | } |