source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/LongSegment.java@ 12841

Last change on this file since 12841 was 12841, checked in by bastiK, 7 years ago

see #15229 - fix deprecations caused by [12840]

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.util.Collections;
8import java.util.HashSet;
9import java.util.Set;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.coor.LatLon;
13import org.openstreetmap.josm.data.osm.Node;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.Way;
16import org.openstreetmap.josm.data.osm.WaySegment;
17import org.openstreetmap.josm.data.validation.Severity;
18import org.openstreetmap.josm.data.validation.Test;
19import org.openstreetmap.josm.data.validation.TestError;
20import org.openstreetmap.josm.gui.progress.ProgressMonitor;
21
22/**
23 * Checks for very long segments.
24 *
25 * @since 8320
26 */
27public class LongSegment extends Test {
28
29 /** Long segment error */
30 protected static final int LONG_SEGMENT = 3501;
31 /** Maximum segment length for this test */
32 protected int maxlength;
33 /** set of visited ways. Tracking this increases performance when checking single nodes. */
34 private Set<Way> visitedWays;
35
36 /** set of way segments that have been reported */
37 protected Set<WaySegment> reported;
38
39 /**
40 * Constructor
41 */
42 public LongSegment() {
43 super(tr("Long segments"),
44 tr("This tests for long way segments, which are usually errors."));
45 }
46
47 @Override
48 public void visit(Node n) {
49 // Test all way segments around this node.
50 // If there is an error in the unchanged part of the way, we do not need to warn the user about it.
51 for (Way way : n.getParentWays()) {
52 if (ignoreWay(way)) {
53 continue;
54 }
55 // Do not simply use index of - a node may be in a way multiple times
56 for (int i = 0; i < way.getNodesCount(); i++) {
57 if (n == way.getNode(i)) {
58 if (i > 0) {
59 visitWaySegment(way, i - 1);
60 }
61 if (i < way.getNodesCount() - 1) {
62 visitWaySegment(way, i);
63 }
64 }
65 }
66 }
67 }
68
69 @Override
70 public void visit(Way w) {
71 if (ignoreWay(w)) {
72 return;
73 }
74 visitedWays.add(w);
75
76 testWay(w);
77 }
78
79 private void testWay(Way w) {
80 for (int i = 0; i < w.getNodesCount() - 1; i++) {
81 visitWaySegment(w, i);
82 }
83 }
84
85 private boolean ignoreWay(Way w) {
86 return visitedWays.contains(w) || w.hasTag("route", "ferry");
87 }
88
89 private void visitWaySegment(Way w, int i) {
90 LatLon coor1 = w.getNode(i).getCoor();
91 LatLon coor2 = w.getNode(i + 1).getCoor();
92
93 if (coor1 != null && coor2 != null) {
94 Double length = coor1.greatCircleDistance(coor2);
95 if (length > maxlength) {
96 addErrorForSegment(new WaySegment(w, i), length / 1000.0);
97 }
98 }
99 }
100
101 private void addErrorForSegment(WaySegment waySegment, Double length) {
102 if (reported.add(waySegment)) {
103 errors.add(TestError.builder(this, Severity.WARNING, LONG_SEGMENT)
104 .message(tr("Long segments"), marktr("Very long segment of {0} kilometers"), length.intValue())
105 .primitives(waySegment.way)
106 .highlightWaySegments(Collections.singleton(waySegment))
107 .build());
108 }
109 }
110
111 @Override
112 public void startTest(ProgressMonitor monitor) {
113 super.startTest(monitor);
114 maxlength = Main.pref.getInt("validator.maximum.segment.length", 15_000);
115 reported = new HashSet<>();
116 visitedWays = new HashSet<>();
117 }
118
119 @Override
120 public void endTest() {
121 super.endTest();
122 // free memory
123 visitedWays = null;
124 reported = null;
125 }
126
127 @Override
128 public boolean isPrimitiveUsable(OsmPrimitive p) {
129 return p.isUsable() && (isUsableWay(p) || isUsableNode(p));
130 }
131
132 private static boolean isUsableNode(OsmPrimitive p) {
133 // test changed nodes - ways referred by them may not be checked automatically.
134 return p instanceof Node && p.isDrawable();
135 }
136
137 private static boolean isUsableWay(OsmPrimitive p) {
138 // test only Ways with at least 2 nodes
139 return p instanceof Way && ((Way) p).getNodesCount() > 1;
140 }
141}
Note: See TracBrowser for help on using the repository browser.