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.Collections;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.actions.OrthogonalizeAction;
|
---|
9 | import org.openstreetmap.josm.actions.OrthogonalizeAction.InvalidUserInputException;
|
---|
10 | import org.openstreetmap.josm.data.osm.Node;
|
---|
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.progress.ProgressMonitor;
|
---|
16 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
17 | import org.openstreetmap.josm.tools.Logging;
|
---|
18 | import org.openstreetmap.josm.tools.Pair;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Checks for buildings with angles close to right angle.
|
---|
22 | *
|
---|
23 | * @author marxin
|
---|
24 | * @since 13670
|
---|
25 | */
|
---|
26 | public class RightAngleBuildingTest extends Test {
|
---|
27 |
|
---|
28 | /** Maximum angle difference from right angle that is considered as invalid. */
|
---|
29 | protected double maxAngleDelta;
|
---|
30 |
|
---|
31 | /** Minimum angle difference from right angle that is considered as invalid. */
|
---|
32 | protected double minAngleDelta;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Constructs a new {@code RightAngleBuildingTest} test.
|
---|
36 | */
|
---|
37 | public RightAngleBuildingTest() {
|
---|
38 | super(tr("Almost right angle buildings"),
|
---|
39 | tr("Checks for buildings that have angles close to right angle and are not orthogonalized."));
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public void visit(Way w) {
|
---|
44 | if (!w.isUsable() || !w.isClosed() || !isBuilding(w)) return;
|
---|
45 |
|
---|
46 | for (Pair<Double, Node> pair: w.getAngles()) {
|
---|
47 | if (!checkAngle(w, pair.a, pair.b))
|
---|
48 | return;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public void startTest(ProgressMonitor monitor) {
|
---|
54 | super.startTest(monitor);
|
---|
55 | maxAngleDelta = Config.getPref().getDouble("validator.RightAngleBuilding.maximumDelta", 10.0);
|
---|
56 | minAngleDelta = Config.getPref().getDouble("validator.RightAngleBuilding.minimumDelta", 0.1);
|
---|
57 | }
|
---|
58 |
|
---|
59 | private boolean checkAngle(Way w, double angle, Node n) {
|
---|
60 | double difference = Math.abs(angle - 90);
|
---|
61 |
|
---|
62 | if (difference > minAngleDelta && difference < maxAngleDelta) {
|
---|
63 | errors.add(TestError.builder(this, Severity.WARNING, 3701)
|
---|
64 | .message(tr("Building with an almost square angle"))
|
---|
65 | .primitives(w)
|
---|
66 | .highlight(n)
|
---|
67 | .fix(() -> {
|
---|
68 | try {
|
---|
69 | return OrthogonalizeAction.orthogonalize(Collections.singleton(w));
|
---|
70 | } catch (InvalidUserInputException e) {
|
---|
71 | Logging.warn(e);
|
---|
72 | return null;
|
---|
73 | }
|
---|
74 | })
|
---|
75 | .build());
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 |
|
---|
79 | return true;
|
---|
80 | }
|
---|
81 | }
|
---|