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

Last change on this file since 13670 was 13670, checked in by Don-vip, 6 years ago

fix #16189 - Add "almost square check" for buildings (patch by marxin, modified)

  • Property svn:eol-style set to native
File size: 16.2 KB
RevLine 
[8378]1// License: GPL. For details, see LICENSE file.
[3669]2package org.openstreetmap.josm.data.validation;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
[11594]6import java.awt.GraphicsEnvironment;
[3669]7import java.io.File;
8import java.io.FileNotFoundException;
9import java.io.IOException;
10import java.io.PrintWriter;
[7082]11import java.nio.charset.StandardCharsets;
[7315]12import java.nio.file.Files;
13import java.nio.file.Path;
14import java.nio.file.Paths;
[3669]15import java.util.ArrayList;
[5548]16import java.util.Arrays;
[3669]17import java.util.Collection;
[10297]18import java.util.Collections;
[12667]19import java.util.EnumMap;
[3669]20import java.util.HashMap;
[12667]21import java.util.List;
[3669]22import java.util.Map;
[6691]23import java.util.SortedMap;
24import java.util.TreeMap;
[3669]25import java.util.TreeSet;
[12667]26import java.util.function.Predicate;
27import java.util.stream.Collectors;
[3669]28
29import javax.swing.JOptionPane;
30
31import org.openstreetmap.josm.Main;
[12649]32import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
[5644]33import org.openstreetmap.josm.data.validation.tests.Addresses;
[7574]34import org.openstreetmap.josm.data.validation.tests.ApiCapabilitiesTest;
[6192]35import org.openstreetmap.josm.data.validation.tests.BarriersEntrances;
[3669]36import org.openstreetmap.josm.data.validation.tests.Coastlines;
[6605]37import org.openstreetmap.josm.data.validation.tests.ConditionalKeys;
[3669]38import org.openstreetmap.josm.data.validation.tests.CrossingWays;
39import org.openstreetmap.josm.data.validation.tests.DuplicateNode;
[4126]40import org.openstreetmap.josm.data.validation.tests.DuplicateRelation;
[3669]41import org.openstreetmap.josm.data.validation.tests.DuplicateWay;
42import org.openstreetmap.josm.data.validation.tests.DuplicatedWayNodes;
[5902]43import org.openstreetmap.josm.data.validation.tests.Highways;
[7489]44import org.openstreetmap.josm.data.validation.tests.InternetTags;
[6592]45import org.openstreetmap.josm.data.validation.tests.Lanes;
[8320]46import org.openstreetmap.josm.data.validation.tests.LongSegment;
[6506]47import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
[3669]48import org.openstreetmap.josm.data.validation.tests.MultipolygonTest;
49import org.openstreetmap.josm.data.validation.tests.NameMismatch;
[6370]50import org.openstreetmap.josm.data.validation.tests.OpeningHourTest;
[3669]51import org.openstreetmap.josm.data.validation.tests.OverlappingWays;
[5300]52import org.openstreetmap.josm.data.validation.tests.PowerLines;
[9933]53import org.openstreetmap.josm.data.validation.tests.PublicTransportRouteTest;
[3669]54import org.openstreetmap.josm.data.validation.tests.RelationChecker;
[13670]55import org.openstreetmap.josm.data.validation.tests.RightAngleBuildingTest;
[3669]56import org.openstreetmap.josm.data.validation.tests.SelfIntersectingWay;
57import org.openstreetmap.josm.data.validation.tests.SimilarNamedWays;
58import org.openstreetmap.josm.data.validation.tests.TagChecker;
59import org.openstreetmap.josm.data.validation.tests.TurnrestrictionTest;
60import org.openstreetmap.josm.data.validation.tests.UnclosedWays;
61import org.openstreetmap.josm.data.validation.tests.UnconnectedWays;
62import org.openstreetmap.josm.data.validation.tests.UntaggedNode;
63import org.openstreetmap.josm.data.validation.tests.UntaggedWay;
[4682]64import org.openstreetmap.josm.data.validation.tests.WayConnectedToArea;
[3669]65import org.openstreetmap.josm.data.validation.tests.WronglyOrderedWays;
[12636]66import org.openstreetmap.josm.gui.MainApplication;
[4126]67import org.openstreetmap.josm.gui.layer.ValidatorLayer;
[5548]68import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
[13647]69import org.openstreetmap.josm.gui.util.GuiHelper;
[12846]70import org.openstreetmap.josm.spi.preferences.Config;
[12667]71import org.openstreetmap.josm.tools.AlphanumComparator;
[12620]72import org.openstreetmap.josm.tools.Logging;
[5874]73import org.openstreetmap.josm.tools.Utils;
[3669]74
75/**
[6472]76 * A OSM data validator.
[3669]77 *
[6830]78 * @author Francisco R. Santos <frsantos@gmail.com>
[3669]79 */
[10759]80public final class OsmValidator {
[3669]81
[10759]82 private OsmValidator() {
83 // Hide default constructor for utilities classes
84 }
85
[11852]86 private static volatile ValidatorLayer errorLayer;
[3669]87
[3946]88 /** Grid detail, multiplier of east,north values for valuable cell sizing */
[11852]89 private static double griddetail;
[3946]90
[12542]91 private static final Collection<String> ignoredErrors = new TreeSet<>();
[3669]92
93 /**
[10297]94 * All registered tests
[3669]95 */
[12542]96 private static final Collection<Class<? extends Test>> allTests = new ArrayList<>();
97 private static final Map<String, Test> allTestsMap = new HashMap<>();
[10297]98
99 /**
100 * All available tests in core
101 */
[3669]102 @SuppressWarnings("unchecked")
[10297]103 private static final Class<Test>[] CORE_TEST_CLASSES = new Class[] {
[6593]104 /* FIXME - unique error numbers for tests aren't properly unique - ignoring will not work as expected */
[4126]105 DuplicateNode.class, // ID 1 .. 99
106 OverlappingWays.class, // ID 101 .. 199
107 UntaggedNode.class, // ID 201 .. 299
108 UntaggedWay.class, // ID 301 .. 399
109 SelfIntersectingWay.class, // ID 401 .. 499
110 DuplicatedWayNodes.class, // ID 501 .. 599
[6581]111 CrossingWays.Ways.class, // ID 601 .. 699
112 CrossingWays.Boundaries.class, // ID 601 .. 699
113 CrossingWays.Barrier.class, // ID 601 .. 699
[11136]114 CrossingWays.SelfCrossing.class, // ID 601 .. 699
[4126]115 SimilarNamedWays.class, // ID 701 .. 799
116 Coastlines.class, // ID 901 .. 999
117 WronglyOrderedWays.class, // ID 1001 .. 1099
118 UnclosedWays.class, // ID 1101 .. 1199
119 TagChecker.class, // ID 1201 .. 1299
[6515]120 UnconnectedWays.UnconnectedHighways.class, // ID 1301 .. 1399
121 UnconnectedWays.UnconnectedRailways.class, // ID 1301 .. 1399
122 UnconnectedWays.UnconnectedWaterways.class, // ID 1301 .. 1399
123 UnconnectedWays.UnconnectedNaturalOrLanduse.class, // ID 1301 .. 1399
124 UnconnectedWays.UnconnectedPower.class, // ID 1301 .. 1399
[4126]125 DuplicateWay.class, // ID 1401 .. 1499
126 NameMismatch.class, // ID 1501 .. 1599
127 MultipolygonTest.class, // ID 1601 .. 1699
128 RelationChecker.class, // ID 1701 .. 1799
129 TurnrestrictionTest.class, // ID 1801 .. 1899
130 DuplicateRelation.class, // ID 1901 .. 1999
[4682]131 WayConnectedToArea.class, // ID 2301 .. 2399
[5300]132 PowerLines.class, // ID 2501 .. 2599
[5644]133 Addresses.class, // ID 2601 .. 2699
[5902]134 Highways.class, // ID 2701 .. 2799
[6192]135 BarriersEntrances.class, // ID 2801 .. 2899
[6506]136 OpeningHourTest.class, // 2901 .. 2999
137 MapCSSTagChecker.class, // 3000 .. 3099
[6592]138 Lanes.class, // 3100 .. 3199
[6605]139 ConditionalKeys.class, // 3200 .. 3299
[7489]140 InternetTags.class, // 3300 .. 3399
[7574]141 ApiCapabilitiesTest.class, // 3400 .. 3499
[8320]142 LongSegment.class, // 3500 .. 3599
[9933]143 PublicTransportRouteTest.class, // 3600 .. 3699
[13670]144 RightAngleBuildingTest.class, // 3700 .. 3799
[3669]145 };
[6830]146
[12390]147 /**
148 * Adds a test to the list of available tests
149 * @param testClass The test class
150 */
[10297]151 public static void addTest(Class<? extends Test> testClass) {
[12542]152 allTests.add(testClass);
[10297]153 try {
[12542]154 allTestsMap.put(testClass.getName(), testClass.getConstructor().newInstance());
[10297]155 } catch (ReflectiveOperationException e) {
[12620]156 Logging.error(e);
[10297]157 }
158 }
159
[6472]160 static {
[10297]161 for (Class<? extends Test> testClass : CORE_TEST_CLASSES) {
162 addTest(testClass);
[6472]163 }
164 }
[3669]165
[5902]166 /**
[10759]167 * Initializes {@code OsmValidator}.
[5902]168 */
[10759]169 public static void initialize() {
[3707]170 checkValidatorDir();
[3946]171 initializeGridDetail();
[3669]172 loadIgnoredErrors(); //FIXME: load only when needed
173 }
174
175 /**
[7335]176 * Returns the validator directory.
[3707]177 *
[7335]178 * @return The validator directory
[3707]179 */
[6472]180 public static String getValidatorDir() {
[13647]181 File dir = new File(Config.getDirs().getUserDataDirectory(true), "validator");
182 try {
183 return dir.getAbsolutePath();
184 } catch (SecurityException e) {
185 Logging.log(Logging.LEVEL_ERROR, null, e);
186 return dir.getPath();
187 }
[3707]188 }
189
190 /**
[10212]191 * Check if validator directory exists (store ignored errors file)
[3669]192 */
[8870]193 private static void checkValidatorDir() {
[10212]194 File pathDir = new File(getValidatorDir());
[13647]195 try {
196 if (!pathDir.exists()) {
197 Utils.mkDirs(pathDir);
198 }
199 } catch (SecurityException e) {
200 Logging.log(Logging.LEVEL_ERROR, "Unable to check validator directory", e);
[3669]201 }
202 }
203
[8870]204 private static void loadIgnoredErrors() {
[12542]205 ignoredErrors.clear();
[12649]206 if (ValidatorPrefHelper.PREF_USE_IGNORE.get()) {
[7834]207 Path path = Paths.get(getValidatorDir()).resolve("ignorederrors");
[13647]208 try {
209 if (path.toFile().exists()) {
210 try {
211 ignoredErrors.addAll(Files.readAllLines(path, StandardCharsets.UTF_8));
212 } catch (FileNotFoundException e) {
213 Logging.debug(Logging.getErrorMessage(e));
214 } catch (IOException e) {
215 Logging.error(e);
216 }
[3669]217 }
[13647]218 } catch (SecurityException e) {
219 Logging.log(Logging.LEVEL_ERROR, "Unable to load ignored errors", e);
[3669]220 }
221 }
222 }
223
[12390]224 /**
225 * Adds an ignored error
226 * @param s The ignore group / sub group name
227 * @see TestError#getIgnoreGroup()
228 * @see TestError#getIgnoreSubGroup()
229 */
[3669]230 public static void addIgnoredError(String s) {
[12542]231 ignoredErrors.add(s);
[3669]232 }
233
[12390]234 /**
235 * Check if a error should be ignored
236 * @param s The ignore group / sub group name
237 * @return <code>true</code> to ignore that error
238 */
[3669]239 public static boolean hasIgnoredError(String s) {
[12542]240 return ignoredErrors.contains(s);
[3669]241 }
242
[12390]243 /**
244 * Saves the names of the ignored errors to a file
245 */
[3669]246 public static void saveIgnoredErrors() {
[10952]247 try (PrintWriter out = new PrintWriter(new File(getValidatorDir(), "ignorederrors"), StandardCharsets.UTF_8.name())) {
[12542]248 for (String e : ignoredErrors) {
[3669]249 out.println(e);
[3671]250 }
[5874]251 } catch (IOException e) {
[12620]252 Logging.error(e);
[3669]253 }
254 }
255
[11852]256 /**
257 * Initializes error layer.
258 */
[10214]259 public static synchronized void initializeErrorLayer() {
[12649]260 if (!ValidatorPrefHelper.PREF_LAYER.get())
[3669]261 return;
262 if (errorLayer == null) {
263 errorLayer = new ValidatorLayer();
[12636]264 MainApplication.getLayerManager().addLayer(errorLayer);
[3669]265 }
266 }
267
[6529]268 /**
[11852]269 * Resets error layer.
270 * @since 11852
271 */
272 public static synchronized void resetErrorLayer() {
273 errorLayer = null;
274 }
275
276 /**
[6529]277 * Gets a map from simple names to all tests.
[6691]278 * @return A map of all tests, indexed and sorted by the name of their Java class
[6472]279 */
[6691]280 public static SortedMap<String, Test> getAllTestsMap() {
[12542]281 applyPrefs(allTestsMap, false);
282 applyPrefs(allTestsMap, true);
283 return new TreeMap<>(allTestsMap);
[3669]284 }
[6830]285
[6670]286 /**
287 * Returns the instance of the given test class.
[9246]288 * @param <T> testClass type
[6670]289 * @param testClass The class of test to retrieve
290 * @return the instance of the given test class, if any, or {@code null}
291 * @since 6670
292 */
293 @SuppressWarnings("unchecked")
294 public static <T extends Test> T getTest(Class<T> testClass) {
295 if (testClass == null) {
296 return null;
297 }
[12542]298 return (T) allTestsMap.get(testClass.getName());
[6670]299 }
[3669]300
301 private static void applyPrefs(Map<String, Test> tests, boolean beforeUpload) {
[12846]302 for (String testName : Config.getPref().getList(beforeUpload
[12649]303 ? ValidatorPrefHelper.PREF_SKIP_TESTS_BEFORE_UPLOAD : ValidatorPrefHelper.PREF_SKIP_TESTS)) {
[3669]304 Test test = tests.get(testName);
305 if (test != null) {
306 if (beforeUpload) {
[6593]307 test.testBeforeUpload = false;
[3669]308 } else {
[6593]309 test.enabled = false;
[3669]310 }
311 }
312 }
313 }
314
[12390]315 /**
316 * Gets all tests that are possible
317 * @return The tests
318 */
[3669]319 public static Collection<Test> getTests() {
320 return getAllTestsMap().values();
321 }
322
[12390]323 /**
324 * Gets all tests that are run
325 * @param beforeUpload To get the ones that are run before upload
326 * @return The tests
327 */
[3669]328 public static Collection<Test> getEnabledTests(boolean beforeUpload) {
329 Collection<Test> enabledTests = getTests();
[7005]330 for (Test t : new ArrayList<>(enabledTests)) {
[3671]331 if (beforeUpload ? t.testBeforeUpload : t.enabled) {
[3669]332 continue;
[3671]333 }
[3669]334 enabledTests.remove(t);
335 }
336 return enabledTests;
337 }
338
339 /**
340 * Gets the list of all available test classes
341 *
[10297]342 * @return A collection of the test classes
[3669]343 */
[10297]344 public static Collection<Class<? extends Test>> getAllAvailableTestClasses() {
[12542]345 return Collections.unmodifiableCollection(allTests);
[3669]346 }
347
348 /**
[3946]349 * Initialize grid details based on current projection system. Values based on
[6830]350 * the original value fixed for EPSG:4326 (10000) using heuristics (that is, test&amp;error
[3946]351 * until most bugs were discovered while keeping the processing time reasonable)
352 */
[10765]353 public static void initializeGridDetail() {
[5548]354 String code = Main.getProjection().toCode();
355 if (Arrays.asList(ProjectionPreference.wgs84.allCodes()).contains(code)) {
[11100]356 OsmValidator.griddetail = 10_000;
[5548]357 } else if (Arrays.asList(ProjectionPreference.mercator.allCodes()).contains(code)) {
[3946]358 OsmValidator.griddetail = 0.01;
[5548]359 } else if (Arrays.asList(ProjectionPreference.lambert.allCodes()).contains(code)) {
[3946]360 OsmValidator.griddetail = 0.1;
[5547]361 } else {
362 OsmValidator.griddetail = 1.0;
[3946]363 }
364 }
365
[11852]366 /**
367 * Returns grid detail, multiplier of east,north values for valuable cell sizing
368 * @return grid detail
369 * @since 11852
370 */
371 public static double getGridDetail() {
372 return griddetail;
373 }
374
[8840]375 private static boolean testsInitialized;
[6779]376
[3946]377 /**
[6779]378 * Initializes all tests if this operations hasn't been performed already.
379 */
380 public static synchronized void initializeTests() {
381 if (!testsInitialized) {
[12620]382 Logging.debug("Initializing validator tests");
[6779]383 final long startTime = System.currentTimeMillis();
384 initializeTests(getTests());
385 testsInitialized = true;
[12620]386 if (Logging.isDebugEnabled()) {
[6852]387 final long elapsedTime = System.currentTimeMillis() - startTime;
[12620]388 Logging.debug("Initializing validator tests completed in {0}", Utils.getDurationString(elapsedTime));
[6852]389 }
[6779]390 }
391 }
392
393 /**
[3669]394 * Initializes all tests
395 * @param allTests The tests to initialize
396 */
[6670]397 public static void initializeTests(Collection<? extends Test> allTests) {
[3669]398 for (Test test : allTests) {
399 try {
400 if (test.enabled) {
401 test.initialize();
402 }
[11746]403 } catch (Exception e) { // NOPMD
[13647]404 String message = tr("Error initializing test {0}:\n {1}", test.getClass().getSimpleName(), e);
405 Logging.error(message);
[11594]406 if (!GraphicsEnvironment.isHeadless()) {
[13647]407 GuiHelper.runInEDT(() -> {
408 JOptionPane.showMessageDialog(Main.parent, message, tr("Error"), JOptionPane.ERROR_MESSAGE);
409 });
[11594]410 }
[3669]411 }
412 }
413 }
414
[12667]415 /**
416 * Groups the given collection of errors by severity, then message, then description.
417 * @param errors list of errors to group
418 * @param filterToUse optional filter
419 * @return collection of errors grouped by severity, then message, then description
420 * @since 12667
421 */
422 public static Map<Severity, Map<String, Map<String, List<TestError>>>> getErrorsBySeverityMessageDescription(
423 Collection<TestError> errors, Predicate<? super TestError> filterToUse) {
424 return errors.stream().filter(filterToUse).collect(
425 Collectors.groupingBy(TestError::getSeverity, () -> new EnumMap<>(Severity.class),
426 Collectors.groupingBy(TestError::getMessage, () -> new TreeMap<>(AlphanumComparator.getInstance()),
427 Collectors.groupingBy(e -> e.getDescription() == null ? "" : e.getDescription(),
428 () -> new TreeMap<>(AlphanumComparator.getInstance()),
429 Collectors.toList()
430 ))));
431 }
[3669]432}
Note: See TracBrowser for help on using the repository browser.