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

Last change on this file since 4310 was 4126, checked in by bastiK, 13 years ago

memory optimizations for Node & WayPoint (Patch by Gubaer, modified)

The field 'proj' in CachedLatLon is a waste of memory. For the 2 classes where this has the greatest impact, the cache for the projected coordinates is replaced by 2 simple double fields (east & north). On projection change, they have to be invalidated explicitly. This is handled by the DataSet & the GpxLayer.

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