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

Last change on this file since 4481 was 4448, checked in by simon04, 13 years ago

see #4582 - validator test for overlapping areas

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