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

Last change on this file since 6203 was 6203, checked in by Don-vip, 11 years ago

fix #9024 - bbox/bounds memory optimizations (modified patch by shinigami) + javadoc

  • Property svn:eol-style set to native
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.isVisible() && (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 if (c != null) {
83 BBox box = c.toBBox(0.0001);
84 if (!dataSet.searchNodes(box).contains(n)) {
85 printError("SEARCH NODES", "%s not found using Dataset.searchNodes()", n);
86 }
87 }
88 }
89 }
90 }
91
92 public void searchWays() {
93 for (Way w:dataSet.getWays()) {
94 if (!w.isIncomplete() && !w.isDeleted() && w.getNodesCount() >= 2 && !dataSet.searchWays(w.getBBox()).contains(w)) {
95 printError("SEARCH WAYS", "%s not found using Dataset.searchWays()", w);
96 }
97 }
98 }
99
100 private void checkReferredPrimitive(OsmPrimitive primitive, OsmPrimitive parent) {
101 if (primitive.getDataSet() == null) {
102 printError("NO DATASET", "%s is referenced by %s but not found in dataset", primitive, parent);
103 } else if (dataSet.getPrimitiveById(primitive) == null) {
104 printError("REFERENCED BUT NOT IN DATA", "%s is referenced by %s but not found in dataset", primitive, parent);
105 } else if (dataSet.getPrimitiveById(primitive) != primitive) {
106 printError("DIFFERENT INSTANCE", "%s is different instance that referred by %s", primitive, parent);
107 }
108
109 if (primitive.isDeleted()) {
110 printError("DELETED REFERENCED", "%s refers to deleted primitive %s", parent, primitive);
111 }
112 }
113
114 public void referredPrimitiveNotInDataset() {
115 for (Way way:dataSet.getWays()) {
116 for (Node node:way.getNodes()) {
117 checkReferredPrimitive(node, way);
118 }
119 }
120
121 for (Relation relation:dataSet.getRelations()) {
122 for (RelationMember member:relation.getMembers()) {
123 checkReferredPrimitive(member.getMember(), relation);
124 }
125 }
126 }
127
128
129 public void checkZeroNodesWays() {
130 for (Way way:dataSet.getWays()) {
131 if (way.isUsable() && way.getNodesCount() == 0) {
132 printError("WARN - ZERO NODES", "Way %s has zero nodes", way);
133 } else if (way.isUsable() && way.getNodesCount() == 1) {
134 printError("WARN - NO NODES", "Way %s has only one node", way);
135 }
136 }
137 }
138
139 public void runTest() {
140 try {
141 referredPrimitiveNotInDataset();
142 checkReferrers();
143 checkCompleteWaysWithIncompleteNodes();
144 checkCompleteNodesWithoutCoordinates();
145 searchNodes();
146 searchWays();
147 checkZeroNodesWays();
148 if (errorCount > MAX_ERRORS) {
149 writer.println((errorCount - MAX_ERRORS) + " more...");
150 }
151 } catch (Exception e) {
152 writer.println("Exception during dataset integrity test:");
153 e.printStackTrace(writer);
154 }
155 }
156
157 public static String runTests(DataSet dataSet) {
158 StringWriter writer = new StringWriter();
159 new DatasetConsistencyTest(dataSet, writer).runTest();
160 return writer.toString();
161 }
162
163}
Note: See TracBrowser for help on using the repository browser.