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

Last change on this file since 2958 was 2958, checked in by jttt, 14 years ago

Fix #4468 Status -> OutOfMemory after copying layer

  • Property svn:mime-type set to text/plain
File size: 5.6 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 for (Way way:dataSet.getWays()) {
37 if (!way.isDeleted()) {
38 for (Node n:way.getNodes()) {
39 if (!n.getReferrers().contains(way)) {
40 printError("WAY NOT IN REFERRERS", "%s is part of %s but is not in referrers", n, way);
41 }
42 }
43 }
44 }
45
46 for (Relation relation:dataSet.getRelations()) {
47 if (!relation.isDeleted()) {
48 for (RelationMember m:relation.getMembers()) {
49 if (!m.getMember().getReferrers().contains(relation)) {
50 printError("RELATION NOT IN REFERRERS", "%s is part of %s but is not in referrers", m.getMember(), relation);
51 }
52 }
53 }
54 }
55 }
56
57 public void checkCompleteWaysWithIncompleteNodes() {
58 for (Way way:dataSet.getWays()) {
59 if (way.isUsable()) {
60 for (Node node:way.getNodes()) {
61 if (node.isIncomplete()) {
62 printError("USABLE HAS INCOMPLETE", "%s is usable but contains incomplete node '%s'", way, node);
63 }
64 }
65 }
66 }
67 }
68
69 public void checkCompleteNodesWithoutCoordinates() {
70 for (Node node:dataSet.getNodes()) {
71 if (!node.isIncomplete() && (node.getCoor() == null || node.getEastNorth() == null)) {
72 printError("COMPLETE WITHOUT COORDINATES", "%s is not incomplete but has null coordinates", node);
73 }
74 }
75 }
76
77 public void searchNodes() {
78 for (Node n:dataSet.getNodes()) {
79 if (!n.isIncomplete() && !n.isDeleted()) {
80 LatLon c = n.getCoor();
81 BBox box = new BBox(new LatLon(c.lat() - 0.0001, c.lon() - 0.0001), new LatLon(c.lat() + 0.0001, c.lon() + 0.0001));
82 if (!dataSet.searchNodes(box).contains(n)) {
83 printError("SEARCH NODES", "%s not found using Dataset.searchNodes()", n);
84 }
85 }
86 }
87 }
88
89 public void searchWays() {
90 for (Way w:dataSet.getWays()) {
91 if (!w.isIncomplete() && !w.isDeleted() && w.getNodesCount() >= 2 && !dataSet.searchWays(w.getBBox()).contains(w)) {
92 printError("SEARCH WAYS", "%s not found using Dataset.searchWays()", w);
93 }
94 }
95 }
96
97 private void checkReferredPrimitive(OsmPrimitive primitive, OsmPrimitive parent) {
98 if (dataSet.getPrimitiveById(primitive) == null) {
99 printError("REFERENCED BUT NOT IN DATA", "%s is referenced by %s but not found in dataset", primitive, parent);
100 }
101 if (dataSet.getPrimitiveById(primitive) != primitive) {
102 printError("DIFFERENT INSTANCE", "%s is different instance that referred by %s", primitive, parent);
103 }
104 if (primitive.isDeleted()) {
105 printError("DELETED REFERENCED", "%s refers to deleted primitive %s", parent, primitive);
106 }
107 }
108
109 public void referredPrimitiveNotInDataset() {
110 for (Way way:dataSet.getWays()) {
111 for (Node node:way.getNodes()) {
112 checkReferredPrimitive(node, way);
113 }
114 }
115
116 for (Relation relation:dataSet.getRelations()) {
117 for (RelationMember member:relation.getMembers()) {
118 checkReferredPrimitive(member.getMember(), relation);
119 }
120 }
121 }
122
123
124 public void checkZeroNodesWays() {
125 for (Way way:dataSet.getWays()) {
126 if (way.isUsable() && way.getNodesCount() == 0) {
127 printError("WARN - ZERO NODES", "Way %s has zero nodes", way);
128 } else if (way.isUsable() && way.getNodesCount() == 1) {
129 printError("WARN - NO NODES", "Way %s has only one node", way);
130 }
131 }
132 }
133
134 public void runTest() {
135 try {
136 checkReferrers();
137 checkCompleteWaysWithIncompleteNodes();
138 checkCompleteNodesWithoutCoordinates();
139 searchNodes();
140 searchWays();
141 referredPrimitiveNotInDataset();
142 checkZeroNodesWays();
143 if (errorCount > MAX_ERRORS) {
144 writer.println((errorCount - MAX_ERRORS) + " more...");
145 }
146 } catch (Exception e) {
147 writer.println("Exception during dataset integrity test:");
148 e.printStackTrace(writer);
149 }
150 }
151
152 public static String runTests(DataSet dataSet) {
153 StringWriter writer = new StringWriter();
154 new DatasetConsistencyTest(dataSet, writer).runTest();
155 return writer.toString();
156 }
157
158}
Note: See TracBrowser for help on using the repository browser.