source: josm/trunk/src/org/openstreetmap/josm/data/osm/DatasetConsistencyTest.java@ 3734

Last change on this file since 3734 was 3336, checked in by stoecker, 14 years ago

#close #5135 - allow undeleting without recreating object - patch by Upliner

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain
File size: 5.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.io.PrintWriter;
5import java.io.StringWriter;
6import java.io.Writer;
7
8import org.openstreetmap.josm.data.coor.LatLon;
9
10/**
11 * This class can be used to run consistency tests on dataset. Any errors found will be written to provided PrintWriter
12 * <br>
13 * Texts here should not be translated because they're not intended for users but for josm developers
14 *
15 */
16public class DatasetConsistencyTest {
17
18 private static final int MAX_ERRORS = 100;
19 private final DataSet dataSet;
20 private final PrintWriter writer;
21 private int errorCount;
22
23 public DatasetConsistencyTest(DataSet dataSet, Writer writer) {
24 this.dataSet = dataSet;
25 this.writer = new PrintWriter(writer);
26 }
27
28 private void printError(String type, String message, Object... args) {
29 errorCount++;
30 if (errorCount <= MAX_ERRORS) {
31 writer.println("[" + type + "] " + String.format(message, args));
32 }
33 }
34
35 public void checkReferrers() {
36 // It's also error when referred primitive's dataset is null but it's already covered by referredPrimitiveNotInDataset check
37 for (Way way:dataSet.getWays()) {
38 if (!way.isDeleted()) {
39 for (Node n:way.getNodes()) {
40 if (n.getDataSet() != null && !n.getReferrers().contains(way)) {
41 printError("WAY NOT IN REFERRERS", "%s is part of %s but is not in referrers", n, way);
42 }
43 }
44 }
45 }
46
47 for (Relation relation:dataSet.getRelations()) {
48 if (!relation.isDeleted()) {
49 for (RelationMember m:relation.getMembers()) {
50 if (m.getMember().getDataSet() != null && !m.getMember().getReferrers().contains(relation)) {
51 printError("RELATION NOT IN REFERRERS", "%s is part of %s but is not in referrers", m.getMember(), relation);
52 }
53 }
54 }
55 }
56 }
57
58 public void checkCompleteWaysWithIncompleteNodes() {
59 for (Way way:dataSet.getWays()) {
60 if (way.isUsable()) {
61 for (Node node:way.getNodes()) {
62 if (node.isIncomplete()) {
63 printError("USABLE HAS INCOMPLETE", "%s is usable but contains incomplete node '%s'", way, node);
64 }
65 }
66 }
67 }
68 }
69
70 public void checkCompleteNodesWithoutCoordinates() {
71 for (Node node:dataSet.getNodes()) {
72 if (!node.isIncomplete() && (node.getCoor() == null || node.getEastNorth() == null)) {
73 printError("COMPLETE WITHOUT COORDINATES", "%s is not incomplete but has null coordinates", node);
74 }
75 }
76 }
77
78 public void searchNodes() {
79 for (Node n:dataSet.getNodes()) {
80 if (!n.isIncomplete() && !n.isDeleted()) {
81 LatLon c = n.getCoor();
82 BBox box = new BBox(new LatLon(c.lat() - 0.0001, c.lon() - 0.0001), new LatLon(c.lat() + 0.0001, c.lon() + 0.0001));
83 if (!dataSet.searchNodes(box).contains(n)) {
84 printError("SEARCH NODES", "%s not found using Dataset.searchNodes()", n);
85 }
86 }
87 }
88 }
89
90 public void searchWays() {
91 for (Way w:dataSet.getWays()) {
92 if (!w.isIncomplete() && !w.isDeleted() && w.getNodesCount() >= 2 && !dataSet.searchWays(w.getBBox()).contains(w)) {
93 printError("SEARCH WAYS", "%s not found using Dataset.searchWays()", w);
94 }
95 }
96 }
97
98 private void checkReferredPrimitive(OsmPrimitive primitive, OsmPrimitive parent) {
99 if (primitive.getDataSet() == null) {
100 printError("NO DATASET", "%s is referenced by %s but not found in dataset", primitive, parent);
101 } else if (dataSet.getPrimitiveById(primitive) == null) {
102 printError("REFERENCED BUT NOT IN DATA", "%s is referenced by %s but not found in dataset", primitive, parent);
103 } else if (dataSet.getPrimitiveById(primitive) != primitive) {
104 printError("DIFFERENT INSTANCE", "%s is different instance that referred by %s", primitive, parent);
105 }
106
107 if (primitive.isDeleted()) {
108 printError("DELETED REFERENCED", "%s refers to deleted primitive %s", parent, primitive);
109 }
110 }
111
112 public void referredPrimitiveNotInDataset() {
113 for (Way way:dataSet.getWays()) {
114 for (Node node:way.getNodes()) {
115 checkReferredPrimitive(node, way);
116 }
117 }
118
119 for (Relation relation:dataSet.getRelations()) {
120 for (RelationMember member:relation.getMembers()) {
121 checkReferredPrimitive(member.getMember(), relation);
122 }
123 }
124 }
125
126
127 public void checkZeroNodesWays() {
128 for (Way way:dataSet.getWays()) {
129 if (way.isUsable() && way.getNodesCount() == 0) {
130 printError("WARN - ZERO NODES", "Way %s has zero nodes", way);
131 } else if (way.isUsable() && way.getNodesCount() == 1) {
132 printError("WARN - NO NODES", "Way %s has only one node", way);
133 }
134 }
135 }
136
137 public void runTest() {
138 try {
139 referredPrimitiveNotInDataset();
140 checkReferrers();
141 checkCompleteWaysWithIncompleteNodes();
142 checkCompleteNodesWithoutCoordinates();
143 searchNodes();
144 searchWays();
145 checkZeroNodesWays();
146 if (errorCount > MAX_ERRORS) {
147 writer.println((errorCount - MAX_ERRORS) + " more...");
148 }
149 } catch (Exception e) {
150 writer.println("Exception during dataset integrity test:");
151 e.printStackTrace(writer);
152 }
153 }
154
155 public static String runTests(DataSet dataSet) {
156 StringWriter writer = new StringWriter();
157 new DatasetConsistencyTest(dataSet, writer).runTest();
158 return writer.toString();
159 }
160
161}
Note: See TracBrowser for help on using the repository browser.