source: josm/trunk/src/org/openstreetmap/josm/corrector/ReverseWayTagCorrector.java@ 5599

Last change on this file since 5599 was 5599, checked in by simon04, 11 years ago

fix #8188 - oneway:*=* not warning, if way direction changed

  • Property svn:eol-style set to native
File size: 8.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.corrector;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Arrays;
8import java.util.Collection;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12import java.util.regex.Matcher;
13import java.util.regex.Pattern;
14
15import org.openstreetmap.josm.command.Command;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.OsmUtils;
18import org.openstreetmap.josm.data.osm.Relation;
19import org.openstreetmap.josm.data.osm.RelationMember;
20import org.openstreetmap.josm.data.osm.Way;
21
22/**
23 * A ReverseWayTagCorrector handles necessary corrections of tags
24 * when a way is reversed. E.g. oneway=yes needs to be changed
25 * to oneway=-1 and vice versa.
26 *
27 * The Corrector offers the automatic resolution in an dialog
28 * for the user to confirm.
29 */
30
31public class ReverseWayTagCorrector extends TagCorrector<Way> {
32
33 private static class PrefixSuffixSwitcher {
34
35 private static final String SEPARATOR = "[:_]?";
36
37 private final String a;
38 private final String b;
39 private final Pattern startPattern;
40 private final Pattern endPattern;
41
42 public PrefixSuffixSwitcher(String a, String b) {
43 this.a = a;
44 this.b = b;
45 startPattern = Pattern.compile(
46 "^(" + a + "|" + b + ")(" + SEPARATOR + "|$)",
47 Pattern.CASE_INSENSITIVE);
48 endPattern = Pattern.compile("^.*" +
49 SEPARATOR + "(" + a + "|" + b + ")$",
50 Pattern.CASE_INSENSITIVE);
51 }
52
53 public String apply(String text) {
54 Matcher m = startPattern.matcher(text);
55 if (!m.lookingAt()) {
56 m = endPattern.matcher(text);
57 }
58
59 if (m.lookingAt()) {
60 String leftRight = m.group(1).toLowerCase();
61
62 StringBuilder result = new StringBuilder();
63 result.append(text.substring(0, m.start(1)));
64 result.append(leftRight.equals(a) ? b : a);
65 result.append(text.substring(m.end(1)));
66
67 return result.toString();
68 }
69 return text;
70 }
71 }
72
73 private static final PrefixSuffixSwitcher[] prefixSuffixSwitchers =
74 new PrefixSuffixSwitcher[] {
75 new PrefixSuffixSwitcher("left", "right"),
76 new PrefixSuffixSwitcher("forward", "backward"),
77 new PrefixSuffixSwitcher("forwards", "backwards"),
78 new PrefixSuffixSwitcher("up", "down"),
79 new PrefixSuffixSwitcher("east", "west"),
80 new PrefixSuffixSwitcher("north", "south"),
81 };
82
83 /**
84 * Tests whether way can be reversed without semantic change, i.e., whether tags have to be changed.
85 * Looks for keys like oneway, oneway:bicycle, cycleway:right:oneway, left/right.
86 * @param way
87 * @return false if tags should be changed to keep semantic, true otherwise.
88 */
89 public static boolean isReversible(Way way) {
90 for (String key : way.keySet()) {
91 for (String k : Arrays.asList("oneway", "incline", "direction")) {
92 if (key.startsWith(k) || key.endsWith(k)) {
93 return false;
94 }
95 }
96 for (PrefixSuffixSwitcher prefixSuffixSwitcher : prefixSuffixSwitchers) {
97 if (!key.equals(prefixSuffixSwitcher.apply(key))) {
98 return false;
99 }
100 }
101 }
102 return true;
103 }
104
105 public static List<Way> irreversibleWays(List<Way> ways) {
106 List<Way> newWays = new ArrayList<Way>(ways);
107 for (Way way : ways) {
108 if (isReversible(way)) {
109 newWays.remove(way);
110 }
111 }
112 return newWays;
113 }
114
115 public String invertNumber(String value) {
116 Pattern pattern = Pattern.compile("^([+-]?)(\\d.*)$", Pattern.CASE_INSENSITIVE);
117 Matcher matcher = pattern.matcher(value);
118 if (!matcher.matches()) return value;
119 String sign = matcher.group(1);
120 String rest = matcher.group(2);
121 sign = sign.equals("-") ? "" : "-";
122 return sign + rest;
123 }
124
125 @Override
126 public Collection<Command> execute(Way oldway, Way way) throws UserCancelException {
127 Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap =
128 new HashMap<OsmPrimitive, List<TagCorrection>>();
129
130 ArrayList<TagCorrection> tagCorrections = new ArrayList<TagCorrection>();
131 for (String key : way.keySet()) {
132 String newKey = key;
133 String value = way.get(key);
134 String newValue = value;
135
136 if (key.startsWith("oneway") || key.endsWith("oneway")) {
137 if (OsmUtils.isReversed(value)) {
138 newValue = OsmUtils.trueval;
139 } else if (OsmUtils.isTrue(value)) {
140 newValue = OsmUtils.reverseval;
141 }
142 } else if (key.startsWith("incline") || key.endsWith("incline")
143 || key.startsWith("direction") || key.endsWith("direction")) {
144 PrefixSuffixSwitcher switcher = new PrefixSuffixSwitcher("up", "down");
145 newValue = switcher.apply(value);
146 if (newValue.equals(value)) {
147 newValue = invertNumber(value);
148 }
149 } else if (!ignoreKeyForPrefixSuffixCorrection(key)) {
150 for (PrefixSuffixSwitcher prefixSuffixSwitcher : prefixSuffixSwitchers) {
151 newKey = prefixSuffixSwitcher.apply(key);
152 if (!key.equals(newKey)) {
153 break;
154 }
155 newValue = prefixSuffixSwitcher.apply(value);
156 if (!value.equals(newValue)) {
157 break;
158 }
159 }
160 }
161
162 boolean needsCorrection = !key.equals(newKey);
163 if (way.get(newKey) != null && way.get(newKey).equals(newValue)) {
164 needsCorrection = false;
165 }
166 if (!value.equals(newValue)) {
167 needsCorrection = true;
168 }
169
170 if (needsCorrection) {
171 tagCorrections.add(new TagCorrection(key, value, newKey, newValue));
172 }
173 }
174 if (!tagCorrections.isEmpty()) {
175 tagCorrectionsMap.put(way, tagCorrections);
176 }
177
178 Map<OsmPrimitive, List<RoleCorrection>> roleCorrectionMap =
179 new HashMap<OsmPrimitive, List<RoleCorrection>>();
180 ArrayList<RoleCorrection> roleCorrections = new ArrayList<RoleCorrection>();
181
182 Collection<OsmPrimitive> referrers = oldway.getReferrers();
183 for (OsmPrimitive referrer: referrers) {
184 if (! (referrer instanceof Relation)) {
185 continue;
186 }
187 Relation relation = (Relation)referrer;
188 int position = 0;
189 for (RelationMember member : relation.getMembers()) {
190 if (!member.getMember().hasEqualSemanticAttributes(oldway)
191 || !member.hasRole()) {
192 position++;
193 continue;
194 }
195
196 boolean found = false;
197 String newRole = null;
198 for (PrefixSuffixSwitcher prefixSuffixSwitcher : prefixSuffixSwitchers) {
199 newRole = prefixSuffixSwitcher.apply(member.getRole());
200 if (!newRole.equals(member.getRole())) {
201 found = true;
202 break;
203 }
204 }
205
206 if (found) {
207 roleCorrections.add(new RoleCorrection(relation, position, member, newRole));
208 }
209
210 position++;
211 }
212 }
213 if (!roleCorrections.isEmpty()) {
214 roleCorrectionMap.put(way, roleCorrections);
215 }
216
217 return applyCorrections(tagCorrectionsMap, roleCorrectionMap,
218 tr("When reversing this way, the following changes to properties "
219 + "of the way and its nodes are suggested in order "
220 + "to maintain data consistency."));
221 }
222
223 private static boolean ignoreKeyForPrefixSuffixCorrection(String key) {
224 return key.contains("name") || key.equals("tiger:county")
225 || key.equalsIgnoreCase("fixme") || key.startsWith("note");
226 }
227}
Note: See TracBrowser for help on using the repository browser.