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

Last change on this file since 1617 was 1614, checked in by stoecker, 15 years ago

fix #2642 - patch by Teemu Koskinen - TagCorrector didn't work properly when a way has multiple roles in one relation

File size: 5.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.Collection;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11import java.util.regex.Matcher;
12import java.util.regex.Pattern;
13
14import org.openstreetmap.josm.Main;
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
22public class ReverseWayTagCorrector extends TagCorrector<Way> {
23
24 private static class PrefixSuffixSwitcher {
25
26 private final String a;
27 private final String b;
28 private final Pattern startPattern;
29 private final Pattern endPattern;
30
31 private final String SEPARATOR = "[:_]?";
32
33 public PrefixSuffixSwitcher(String a, String b) {
34 this.a = a;
35 this.b = b;
36 startPattern = Pattern.compile(
37 "^(" + a + "|" + b + ")(" + SEPARATOR + "|$)",
38 Pattern.CASE_INSENSITIVE);
39 endPattern = Pattern.compile(
40 SEPARATOR + "(" + a + "|" + b + ")$",
41 Pattern.CASE_INSENSITIVE);
42 }
43
44 public String apply(String text) {
45 Matcher m = startPattern.matcher(text);
46 if (!m.lookingAt())
47 m = endPattern.matcher(text);
48
49 if (m.lookingAt()) {
50 String leftRight = m.group(1).toLowerCase();
51
52 StringBuilder result = new StringBuilder();
53 result.append(text.substring(0, m.start(1)));
54 result.append(leftRight.equals(a) ? b : a);
55 result.append(text.substring(m.end(1)));
56
57 return result.toString();
58 }
59 return text;
60 }
61 }
62
63 private static PrefixSuffixSwitcher[] prefixSuffixSwitchers =
64 new PrefixSuffixSwitcher[] {
65 new PrefixSuffixSwitcher("left", "right"),
66 new PrefixSuffixSwitcher("forward", "backward")
67 };
68
69 @Override
70 public Collection<Command> execute(Way oldway, Way way) throws UserCancelException {
71 Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap =
72 new HashMap<OsmPrimitive, List<TagCorrection>>();
73
74 ArrayList<OsmPrimitive> primitives = new ArrayList<OsmPrimitive>();
75 primitives.add(way);
76 primitives.addAll(way.nodes);
77
78 for (OsmPrimitive primitive : primitives) {
79 tagCorrectionsMap.put(primitive, new ArrayList<TagCorrection>());
80
81 for (String key : primitive.keySet()) {
82 String newKey = key;
83 String value = primitive.get(key);
84 String newValue = value;
85
86 if (key.equals("oneway")) {
87 if (value.equals("-1"))
88 newValue = OsmUtils.trueval;
89 else {
90 Boolean boolValue = OsmUtils.getOsmBoolean(value);
91 if (boolValue != null && boolValue.booleanValue()) {
92 newValue = "-1";
93 }
94 }
95 } else {
96 for (PrefixSuffixSwitcher prefixSuffixSwitcher : prefixSuffixSwitchers) {
97 newKey = prefixSuffixSwitcher.apply(key);
98 if (!key.equals(newKey))
99 break;
100 }
101 }
102
103 if (!key.equals(newKey) || !value.equals(newValue))
104 tagCorrectionsMap.get(primitive).add(
105 new TagCorrection(key, value, newKey, newValue));
106 }
107 }
108
109 Map<OsmPrimitive, List<RoleCorrection>> roleCorrectionMap =
110 new HashMap<OsmPrimitive, List<RoleCorrection>>();
111 roleCorrectionMap.put(way, new ArrayList<RoleCorrection>());
112
113 for (Relation relation : Main.ds.relations) {
114 int position = 0;
115 for (RelationMember member : relation.members) {
116 if (!member.member.realEqual(oldway, true)
117 || member.role.length() == 0) {
118 position++;
119 continue;
120 }
121
122 boolean found = false;
123 String newRole = null;
124 for (PrefixSuffixSwitcher prefixSuffixSwitcher : prefixSuffixSwitchers) {
125 newRole = prefixSuffixSwitcher.apply(member.role);
126 if (!newRole.equals(member.role)) {
127 found = true;
128 break;
129 }
130 }
131
132 if (found)
133 roleCorrectionMap.get(way).add(
134 new RoleCorrection(relation, position, member, newRole));
135
136 position++;
137 }
138 }
139
140 return applyCorrections(tagCorrectionsMap, roleCorrectionMap,
141 tr("When reversing this way, the following changes to properties "
142 + "of the way and its nodes are suggested in order "
143 + "to maintain data consistency."));
144 }
145}
Note: See TracBrowser for help on using the repository browser.