source: josm/trunk/src/org/openstreetmap/josm/data/validation/OsmValidator.java@ 8320

Last change on this file since 8320 was 8320, checked in by stoecker, 9 years ago

fix #11405 - warn about very long segments (>15km for now)

  • Property svn:eol-style set to native
File size: 13.5 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.data.validation;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.FileNotFoundException;
8import java.io.FileOutputStream;
9import java.io.IOException;
10import java.io.OutputStreamWriter;
11import java.io.PrintWriter;
12import java.nio.charset.StandardCharsets;
13import java.nio.file.Files;
14import java.nio.file.Path;
15import java.nio.file.Paths;
16import java.util.ArrayList;
17import java.util.Arrays;
18import java.util.Collection;
19import java.util.HashMap;
20import java.util.Map;
21import java.util.SortedMap;
22import java.util.TreeMap;
23import java.util.TreeSet;
24
25import javax.swing.JOptionPane;
26
27import org.openstreetmap.josm.Main;
28import org.openstreetmap.josm.actions.ValidateAction;
29import org.openstreetmap.josm.data.validation.tests.Addresses;
30import org.openstreetmap.josm.data.validation.tests.ApiCapabilitiesTest;
31import org.openstreetmap.josm.data.validation.tests.BarriersEntrances;
32import org.openstreetmap.josm.data.validation.tests.Coastlines;
33import org.openstreetmap.josm.data.validation.tests.ConditionalKeys;
34import org.openstreetmap.josm.data.validation.tests.CrossingWays;
35import org.openstreetmap.josm.data.validation.tests.DuplicateNode;
36import org.openstreetmap.josm.data.validation.tests.DuplicateRelation;
37import org.openstreetmap.josm.data.validation.tests.DuplicateWay;
38import org.openstreetmap.josm.data.validation.tests.DuplicatedWayNodes;
39import org.openstreetmap.josm.data.validation.tests.Highways;
40import org.openstreetmap.josm.data.validation.tests.InternetTags;
41import org.openstreetmap.josm.data.validation.tests.Lanes;
42import org.openstreetmap.josm.data.validation.tests.LongSegment;
43import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
44import org.openstreetmap.josm.data.validation.tests.MultipolygonTest;
45import org.openstreetmap.josm.data.validation.tests.NameMismatch;
46import org.openstreetmap.josm.data.validation.tests.OpeningHourTest;
47import org.openstreetmap.josm.data.validation.tests.OverlappingWays;
48import org.openstreetmap.josm.data.validation.tests.PowerLines;
49import org.openstreetmap.josm.data.validation.tests.RelationChecker;
50import org.openstreetmap.josm.data.validation.tests.SelfIntersectingWay;
51import org.openstreetmap.josm.data.validation.tests.SimilarNamedWays;
52import org.openstreetmap.josm.data.validation.tests.TagChecker;
53import org.openstreetmap.josm.data.validation.tests.TurnrestrictionTest;
54import org.openstreetmap.josm.data.validation.tests.UnclosedWays;
55import org.openstreetmap.josm.data.validation.tests.UnconnectedWays;
56import org.openstreetmap.josm.data.validation.tests.UntaggedNode;
57import org.openstreetmap.josm.data.validation.tests.UntaggedWay;
58import org.openstreetmap.josm.data.validation.tests.WayConnectedToArea;
59import org.openstreetmap.josm.data.validation.tests.WronglyOrderedWays;
60import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
61import org.openstreetmap.josm.gui.layer.Layer;
62import org.openstreetmap.josm.gui.layer.OsmDataLayer;
63import org.openstreetmap.josm.gui.layer.ValidatorLayer;
64import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
65import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
66import org.openstreetmap.josm.tools.Utils;
67
68/**
69 * A OSM data validator.
70 *
71 * @author Francisco R. Santos <frsantos@gmail.com>
72 */
73public class OsmValidator implements LayerChangeListener {
74
75 public static volatile ValidatorLayer errorLayer = null;
76
77 /** The validate action */
78 public ValidateAction validateAction = new ValidateAction();
79
80 /** Grid detail, multiplier of east,north values for valuable cell sizing */
81 public static double griddetail;
82
83 public static final Collection<String> ignoredErrors = new TreeSet<>();
84
85 /**
86 * All available tests
87 * TODO: is there any way to find out automatically all available tests?
88 */
89 @SuppressWarnings("unchecked")
90 private static final Class<Test>[] allAvailableTests = new Class[] {
91 /* FIXME - unique error numbers for tests aren't properly unique - ignoring will not work as expected */
92 DuplicateNode.class, // ID 1 .. 99
93 OverlappingWays.class, // ID 101 .. 199
94 UntaggedNode.class, // ID 201 .. 299
95 UntaggedWay.class, // ID 301 .. 399
96 SelfIntersectingWay.class, // ID 401 .. 499
97 DuplicatedWayNodes.class, // ID 501 .. 599
98 CrossingWays.Ways.class, // ID 601 .. 699
99 CrossingWays.Boundaries.class, // ID 601 .. 699
100 CrossingWays.Barrier.class, // ID 601 .. 699
101 SimilarNamedWays.class, // ID 701 .. 799
102 Coastlines.class, // ID 901 .. 999
103 WronglyOrderedWays.class, // ID 1001 .. 1099
104 UnclosedWays.class, // ID 1101 .. 1199
105 TagChecker.class, // ID 1201 .. 1299
106 UnconnectedWays.UnconnectedHighways.class, // ID 1301 .. 1399
107 UnconnectedWays.UnconnectedRailways.class, // ID 1301 .. 1399
108 UnconnectedWays.UnconnectedWaterways.class, // ID 1301 .. 1399
109 UnconnectedWays.UnconnectedNaturalOrLanduse.class, // ID 1301 .. 1399
110 UnconnectedWays.UnconnectedPower.class, // ID 1301 .. 1399
111 DuplicateWay.class, // ID 1401 .. 1499
112 NameMismatch.class, // ID 1501 .. 1599
113 MultipolygonTest.class, // ID 1601 .. 1699
114 RelationChecker.class, // ID 1701 .. 1799
115 TurnrestrictionTest.class, // ID 1801 .. 1899
116 DuplicateRelation.class, // ID 1901 .. 1999
117 WayConnectedToArea.class, // ID 2301 .. 2399
118 PowerLines.class, // ID 2501 .. 2599
119 Addresses.class, // ID 2601 .. 2699
120 Highways.class, // ID 2701 .. 2799
121 BarriersEntrances.class, // ID 2801 .. 2899
122 OpeningHourTest.class, // 2901 .. 2999
123 MapCSSTagChecker.class, // 3000 .. 3099
124 Lanes.class, // 3100 .. 3199
125 ConditionalKeys.class, // 3200 .. 3299
126 InternetTags.class, // 3300 .. 3399
127 ApiCapabilitiesTest.class, // 3400 .. 3499
128 LongSegment.class, // 3500 .. 3599
129 };
130
131 private static Map<String, Test> allTestsMap;
132 static {
133 allTestsMap = new HashMap<>();
134 for (Class<Test> testClass : allAvailableTests) {
135 try {
136 allTestsMap.put(testClass.getName(), testClass.newInstance());
137 } catch (Exception e) {
138 Main.error(e);
139 }
140 }
141 }
142
143 /**
144 * Constructs a new {@code OsmValidator}.
145 */
146 public OsmValidator() {
147 checkValidatorDir();
148 initializeGridDetail();
149 loadIgnoredErrors(); //FIXME: load only when needed
150 }
151
152 /**
153 * Returns the validator directory.
154 *
155 * @return The validator directory
156 */
157 public static String getValidatorDir() {
158 return new File(Main.pref.getUserDataDirectory(), "validator").getAbsolutePath();
159 }
160
161 /**
162 * Check if plugin directory exists (store ignored errors file)
163 */
164 private void checkValidatorDir() {
165 try {
166 File pathDir = new File(getValidatorDir());
167 if (!pathDir.exists()) {
168 pathDir.mkdirs();
169 }
170 } catch (Exception e){
171 Main.error(e);
172 }
173 }
174
175 private void loadIgnoredErrors() {
176 ignoredErrors.clear();
177 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
178 Path path = Paths.get(getValidatorDir()).resolve("ignorederrors");
179 if (Files.exists(path)) {
180 try {
181 ignoredErrors.addAll(Files.readAllLines(path, StandardCharsets.UTF_8));
182 } catch (final FileNotFoundException e) {
183 Main.debug(Main.getErrorMessage(e));
184 } catch (final IOException e) {
185 Main.error(e);
186 }
187 }
188 }
189 }
190
191 public static void addIgnoredError(String s) {
192 ignoredErrors.add(s);
193 }
194
195 public static boolean hasIgnoredError(String s) {
196 return ignoredErrors.contains(s);
197 }
198
199 public static void saveIgnoredErrors() {
200 try (PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
201 new File(getValidatorDir(), "ignorederrors")), StandardCharsets.UTF_8), false)) {
202 for (String e : ignoredErrors) {
203 out.println(e);
204 }
205 } catch (IOException e) {
206 Main.error(e);
207 }
208 }
209
210 public static void initializeErrorLayer() {
211 if (!Main.pref.getBoolean(ValidatorPreference.PREF_LAYER, true))
212 return;
213 if (errorLayer == null) {
214 errorLayer = new ValidatorLayer();
215 Main.main.addLayer(errorLayer);
216 }
217 }
218
219 /**
220 * Gets a map from simple names to all tests.
221 * @return A map of all tests, indexed and sorted by the name of their Java class
222 */
223 public static SortedMap<String, Test> getAllTestsMap() {
224 applyPrefs(allTestsMap, false);
225 applyPrefs(allTestsMap, true);
226 return new TreeMap<>(allTestsMap);
227 }
228
229 /**
230 * Returns the instance of the given test class.
231 * @param testClass The class of test to retrieve
232 * @return the instance of the given test class, if any, or {@code null}
233 * @since 6670
234 */
235 @SuppressWarnings("unchecked")
236 public static <T extends Test> T getTest(Class<T> testClass) {
237 if (testClass == null) {
238 return null;
239 }
240 return (T) allTestsMap.get(testClass.getName());
241 }
242
243 private static void applyPrefs(Map<String, Test> tests, boolean beforeUpload) {
244 for(String testName : Main.pref.getCollection(beforeUpload
245 ? ValidatorPreference.PREF_SKIP_TESTS_BEFORE_UPLOAD : ValidatorPreference.PREF_SKIP_TESTS)) {
246 Test test = tests.get(testName);
247 if (test != null) {
248 if (beforeUpload) {
249 test.testBeforeUpload = false;
250 } else {
251 test.enabled = false;
252 }
253 }
254 }
255 }
256
257 public static Collection<Test> getTests() {
258 return getAllTestsMap().values();
259 }
260
261 public static Collection<Test> getEnabledTests(boolean beforeUpload) {
262 Collection<Test> enabledTests = getTests();
263 for (Test t : new ArrayList<>(enabledTests)) {
264 if (beforeUpload ? t.testBeforeUpload : t.enabled) {
265 continue;
266 }
267 enabledTests.remove(t);
268 }
269 return enabledTests;
270 }
271
272 /**
273 * Gets the list of all available test classes
274 *
275 * @return An array of the test classes
276 */
277 public static Class<Test>[] getAllAvailableTests() {
278 return Utils.copyArray(allAvailableTests);
279 }
280
281 /**
282 * Initialize grid details based on current projection system. Values based on
283 * the original value fixed for EPSG:4326 (10000) using heuristics (that is, test&amp;error
284 * until most bugs were discovered while keeping the processing time reasonable)
285 */
286 public static final void initializeGridDetail() {
287 String code = Main.getProjection().toCode();
288 if (Arrays.asList(ProjectionPreference.wgs84.allCodes()).contains(code)) {
289 OsmValidator.griddetail = 10000;
290 } else if (Arrays.asList(ProjectionPreference.mercator.allCodes()).contains(code)) {
291 OsmValidator.griddetail = 0.01;
292 } else if (Arrays.asList(ProjectionPreference.lambert.allCodes()).contains(code)) {
293 OsmValidator.griddetail = 0.1;
294 } else {
295 OsmValidator.griddetail = 1.0;
296 }
297 }
298
299 private static boolean testsInitialized = false;
300
301 /**
302 * Initializes all tests if this operations hasn't been performed already.
303 */
304 public static synchronized void initializeTests() {
305 if (!testsInitialized) {
306 Main.debug("Initializing validator tests");
307 final long startTime = System.currentTimeMillis();
308 initializeTests(getTests());
309 testsInitialized = true;
310 if (Main.isDebugEnabled()) {
311 final long elapsedTime = System.currentTimeMillis() - startTime;
312 Main.debug("Initializing validator tests completed in " + Utils.getDurationString(elapsedTime));
313 }
314 }
315 }
316
317 /**
318 * Initializes all tests
319 * @param allTests The tests to initialize
320 */
321 public static void initializeTests(Collection<? extends Test> allTests) {
322 for (Test test : allTests) {
323 try {
324 if (test.enabled) {
325 test.initialize();
326 }
327 } catch (Exception e) {
328 Main.error(e);
329 JOptionPane.showMessageDialog(Main.parent,
330 tr("Error initializing test {0}:\n {1}", test.getClass()
331 .getSimpleName(), e),
332 tr("Error"),
333 JOptionPane.ERROR_MESSAGE);
334 }
335 }
336 }
337
338 /* -------------------------------------------------------------------------- */
339 /* interface LayerChangeListener */
340 /* -------------------------------------------------------------------------- */
341 @Override
342 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
343 }
344
345 @Override
346 public void layerAdded(Layer newLayer) {
347 }
348
349 @Override
350 public void layerRemoved(Layer oldLayer) {
351 if (oldLayer == errorLayer) {
352 errorLayer = null;
353 return;
354 }
355 if (Main.map.mapView.getLayersOfType(OsmDataLayer.class).isEmpty()) {
356 if (errorLayer != null) {
357 Main.main.removeLayer(errorLayer);
358 }
359 }
360 }
361}
Note: See TracBrowser for help on using the repository browser.