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