1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.io.PrintWriter;
|
---|
5 | import java.io.StringWriter;
|
---|
6 | import java.io.Writer;
|
---|
7 |
|
---|
8 | import 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 | */
|
---|
16 | public class DatasetConsistencyTest {
|
---|
17 |
|
---|
18 | private final DataSet dataSet;
|
---|
19 | private final PrintWriter writer;
|
---|
20 |
|
---|
21 | public DatasetConsistencyTest(DataSet dataSet, Writer writer) {
|
---|
22 | this.dataSet = dataSet;
|
---|
23 | this.writer = new PrintWriter(writer);
|
---|
24 | }
|
---|
25 |
|
---|
26 | private void checkReferrers() {
|
---|
27 | for (Way way:dataSet.getWays()) {
|
---|
28 | if (!way.isDeleted()) {
|
---|
29 | for (Node n:way.getNodes()) {
|
---|
30 | if (!n.getReferrers().contains(way)) {
|
---|
31 | writer.println(String.format("%s is part of %s but is not in referrers", n, way));
|
---|
32 | }
|
---|
33 | }
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | for (Relation relation:dataSet.getRelations()) {
|
---|
38 | if (!relation.isDeleted()) {
|
---|
39 | for (RelationMember m:relation.getMembers()) {
|
---|
40 | if (!m.getMember().getReferrers().contains(relation)) {
|
---|
41 | writer.println(String.format("%s is part of %s but is not in referrers", m.getMember(), relation));
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | private void checkCompleteWaysWithIncompleteNodes() {
|
---|
49 | for (Way way:dataSet.getWays()) {
|
---|
50 | if (!way.incomplete) {
|
---|
51 | for (Node node:way.getNodes()) {
|
---|
52 | if (node.incomplete) {
|
---|
53 | writer.println(String.format("%s is complete but contains incomplete node '%s'", way, node));
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void checkCompleteNodesWithoutCoordinates() {
|
---|
61 | for (Node node:dataSet.getNodes()) {
|
---|
62 | if (!node.incomplete && (node.getCoor() == null || node.getEastNorth() == null)) {
|
---|
63 | writer.println(String.format("%s is not incomplete but has null coordinates", node));
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | private void searchNodes() {
|
---|
69 | for (Node n:dataSet.getNodes()) {
|
---|
70 | if (!n.incomplete) {
|
---|
71 | LatLon c = n.getCoor();
|
---|
72 | BBox box = new BBox(new LatLon(c.lat() - 0.0001, c.lon() - 0.0001), new LatLon(c.lat() + 0.0001, c.lon() + 0.0001));
|
---|
73 | if (!dataSet.searchNodes(box).contains(n)) {
|
---|
74 | writer.println(String.format("%s not found using Dataset.searchNodes()", n));
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void searchWays() {
|
---|
81 | for (Way w:dataSet.getWays()) {
|
---|
82 | if (!w.incomplete && !dataSet.searchWays(w.getBBox()).contains(w)) {
|
---|
83 | writer.println(String.format("%s not found using Dataset.searchWays()", w));
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | private void checkReferredPrimitive(OsmPrimitive primitive, OsmPrimitive parent) {
|
---|
89 | if (dataSet.getPrimitiveById(primitive) == null) {
|
---|
90 | writer.println(String.format("%s is referenced by %s but not found in dataset", primitive, parent));
|
---|
91 | }
|
---|
92 | if (dataSet.getPrimitiveById(primitive) != primitive) {
|
---|
93 | writer.println(String.format("%s is different instance that reffered by %s", primitive, parent));
|
---|
94 | }
|
---|
95 | if (primitive.isDeleted()) {
|
---|
96 | writer.println(String.format("%s reffers to deleted primitive %s", parent, primitive));
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | private void refferedPrimitiveNotInDataset() {
|
---|
101 | for (Way way:dataSet.getWays()) {
|
---|
102 | for (Node node:way.getNodes()) {
|
---|
103 | checkReferredPrimitive(node, way);
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | for (Relation relation:dataSet.getRelations()) {
|
---|
108 | for (RelationMember member:relation.getMembers()) {
|
---|
109 | checkReferredPrimitive(member.getMember(), relation);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void checkZeroNodesWays() {
|
---|
115 | for (Way way:dataSet.getWays()) {
|
---|
116 | if (way.isUsable() && way.getNodesCount() == 0) {
|
---|
117 | writer.println(String.format("Way %s has zero nodes", way));
|
---|
118 | } else if (way.isUsable() && way.getNodesCount() == 1) {
|
---|
119 | writer.println(String.format("Way %s has only one node", way));
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void runTest() {
|
---|
125 | try {
|
---|
126 | checkReferrers();
|
---|
127 | checkCompleteWaysWithIncompleteNodes();
|
---|
128 | checkCompleteNodesWithoutCoordinates();
|
---|
129 | searchNodes();
|
---|
130 | searchWays();
|
---|
131 | refferedPrimitiveNotInDataset();
|
---|
132 | checkZeroNodesWays();
|
---|
133 | } catch (Exception e) {
|
---|
134 | writer.println("Exception during dataset integrity test:");
|
---|
135 | e.printStackTrace(writer);
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | public static String runTests(DataSet dataSet) {
|
---|
140 | StringWriter writer = new StringWriter();
|
---|
141 | new DatasetConsistencyTest(dataSet, writer).runTest();
|
---|
142 | return writer.toString();
|
---|
143 | }
|
---|
144 |
|
---|
145 | }
|
---|