1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.text.MessageFormat;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.LinkedList;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
13 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
14 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
15 | import org.openstreetmap.josm.data.validation.Test;
|
---|
16 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
17 | import org.openstreetmap.josm.gui.preferences.TaggingPresetPreference;
|
---|
18 | import org.openstreetmap.josm.gui.tagging.TaggingPreset;
|
---|
19 | import org.openstreetmap.josm.gui.tagging.TaggingPreset.PresetType;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Check for wrong relations
|
---|
23 | *
|
---|
24 | */
|
---|
25 | public class RelationChecker extends Test {
|
---|
26 |
|
---|
27 | protected static int ROLE_UNKNOWN = 1701;
|
---|
28 | protected static int ROLE_EMPTY = 1702;
|
---|
29 | protected static int WRONG_TYPE = 1703;
|
---|
30 | protected static int HIGH_COUNT = 1704;
|
---|
31 | protected static int LOW_COUNT = 1705;
|
---|
32 | protected static int ROLE_MISSING = 1706;
|
---|
33 | protected static int RELATION_UNKNOWN = 1707;
|
---|
34 | protected static int RELATION_EMPTY = 1708;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructor
|
---|
38 | */
|
---|
39 | public RelationChecker() {
|
---|
40 | super(tr("Relation checker :"),
|
---|
41 | tr("This plugin checks for errors in relations."));
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public void initialize() throws Exception {
|
---|
46 | initializePresets();
|
---|
47 | }
|
---|
48 |
|
---|
49 | static Collection<TaggingPreset> relationpresets = new LinkedList<TaggingPreset>();
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Reads the presets data.
|
---|
53 | *
|
---|
54 | */
|
---|
55 | public void initializePresets() {
|
---|
56 | Collection<TaggingPreset> presets = TaggingPresetPreference.taggingPresets;
|
---|
57 | if (presets != null) {
|
---|
58 | for (TaggingPreset p : presets) {
|
---|
59 | for (TaggingPreset.Item i : p.data) {
|
---|
60 | if (i instanceof TaggingPreset.Roles) {
|
---|
61 | relationpresets.add(p);
|
---|
62 | break;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public class RoleInfo {
|
---|
70 | int total = 0;
|
---|
71 | int nodes = 0;
|
---|
72 | int ways = 0;
|
---|
73 | int closedways = 0;
|
---|
74 | int openways = 0;
|
---|
75 | int relations = 0;
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public void visit(Relation n) {
|
---|
80 | LinkedList<TaggingPreset.Role> allroles = new LinkedList<TaggingPreset.Role>();
|
---|
81 | for (TaggingPreset p : relationpresets) {
|
---|
82 | boolean matches = true;
|
---|
83 | TaggingPreset.Roles r = null;
|
---|
84 | for (TaggingPreset.Item i : p.data) {
|
---|
85 | if (i instanceof TaggingPreset.Key) {
|
---|
86 | TaggingPreset.Key k = (TaggingPreset.Key) i;
|
---|
87 | if (!k.value.equals(n.get(k.key))) {
|
---|
88 | matches = false;
|
---|
89 | break;
|
---|
90 | }
|
---|
91 | } else if (i instanceof TaggingPreset.Roles) {
|
---|
92 | r = (TaggingPreset.Roles) i;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | if (matches && r != null) {
|
---|
96 | allroles.addAll(r.roles);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | if (allroles.size() == 0) {
|
---|
100 | errors.add( new TestError(this, Severity.WARNING, tr("Relation type is unknown"),
|
---|
101 | RELATION_UNKNOWN, n) );
|
---|
102 | } else {
|
---|
103 | HashMap<String,RoleInfo> map = new HashMap<String, RoleInfo>();
|
---|
104 | for (RelationMember m : n.getMembers()) {
|
---|
105 | String s = "";
|
---|
106 | if (m.hasRole()) {
|
---|
107 | s = m.getRole();
|
---|
108 | }
|
---|
109 | RoleInfo ri = map.get(s);
|
---|
110 | if (ri == null) {
|
---|
111 | ri = new RoleInfo();
|
---|
112 | }
|
---|
113 | ri.total++;
|
---|
114 | if (m.isRelation()) {
|
---|
115 | ri.relations++;
|
---|
116 | } else if(m.isWay()) {
|
---|
117 | ri.ways++;
|
---|
118 | if (m.getWay().isClosed()) {
|
---|
119 | ri.closedways++;
|
---|
120 | } else {
|
---|
121 | ri.openways++;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | else if (m.isNode()) {
|
---|
125 | ri.nodes++;
|
---|
126 | }
|
---|
127 | map.put(s, ri);
|
---|
128 | }
|
---|
129 | if(map.isEmpty()) {
|
---|
130 | errors.add( new TestError(this, Severity.ERROR, tr("Relation is empty"),
|
---|
131 | RELATION_EMPTY, n) );
|
---|
132 | } else {
|
---|
133 | LinkedList<String> done = new LinkedList<String>();
|
---|
134 | for (TaggingPreset.Role r : allroles) {
|
---|
135 | done.add(r.key);
|
---|
136 | String keyname = r.key;
|
---|
137 | if (keyname == "") { //FIXME
|
---|
138 | keyname = tr("<empty>");
|
---|
139 | }
|
---|
140 | RoleInfo ri = map.get(r.key);
|
---|
141 | long count = (ri == null) ? 0 : ri.total;
|
---|
142 | long vc = r.getValidCount(count);
|
---|
143 | if (count != vc) {
|
---|
144 | if (count == 0) {
|
---|
145 | String s = marktr("Role {0} missing");
|
---|
146 | errors.add( new TestError(this, Severity.WARNING, tr("Role verification problem"),
|
---|
147 | tr(s, keyname), MessageFormat.format(s, keyname), ROLE_MISSING, n) );
|
---|
148 | }
|
---|
149 | else if (vc > count) {
|
---|
150 | String s = marktr("Number of {0} roles too low ({1})");
|
---|
151 | errors.add( new TestError(this, Severity.WARNING, tr("Role verification problem"),
|
---|
152 | tr(s, keyname, count), MessageFormat.format(s, keyname, count), LOW_COUNT, n) );
|
---|
153 | } else {
|
---|
154 | String s = marktr("Number of {0} roles too high ({1})");
|
---|
155 | errors.add( new TestError(this, Severity.WARNING, tr("Role verification problem"),
|
---|
156 | tr(s, keyname, count), MessageFormat.format(s, keyname, count), HIGH_COUNT, n) );
|
---|
157 | }
|
---|
158 | }
|
---|
159 | if (ri != null && ((!r.types.contains(PresetType.WAY) && (r.types.contains(PresetType.CLOSEDWAY) ? ri.openways > 0 : ri.ways > 0))
|
---|
160 | || (!r.types.contains(PresetType.NODE) && ri.nodes > 0) || (!r.types.contains(PresetType.RELATION) && ri.relations > 0)))
|
---|
161 | {
|
---|
162 | String s = marktr("Member for role {0} of wrong type");
|
---|
163 | errors.add( new TestError(this, Severity.WARNING, tr("Role verification problem"),
|
---|
164 | tr(s, keyname), MessageFormat.format(s, keyname), WRONG_TYPE, n) );
|
---|
165 | }
|
---|
166 | }
|
---|
167 | for (String key : map.keySet()) {
|
---|
168 | if (!done.contains(key)) {
|
---|
169 | if (key.length() > 0) {
|
---|
170 | String s = marktr("Role {0} unknown");
|
---|
171 | errors.add(new TestError(this, Severity.WARNING, tr("Role verification problem"),
|
---|
172 | tr(s, key), MessageFormat.format(s, key), ROLE_UNKNOWN, n));
|
---|
173 | } else {
|
---|
174 | String s = marktr("Empty role found");
|
---|
175 | errors.add(new TestError(this, Severity.WARNING, tr("Role verification problem"),
|
---|
176 | tr(s), s, ROLE_EMPTY, n));
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|