source: osm/applications/editors/josm/plugins/reltoolbox/src/relcontext/relationfix/AssociatedStreetFixer.java@ 30738

Last change on this file since 30738 was 30738, checked in by donvip, 10 years ago

[josm_plugins] fix java 7 warnings / global usage of try-with-resource

File size: 6.1 KB
Line 
1package relcontext.relationfix;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.util.ArrayList;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Map;
9
10import org.openstreetmap.josm.command.ChangeCommand;
11import org.openstreetmap.josm.command.Command;
12import org.openstreetmap.josm.command.SequenceCommand;
13import org.openstreetmap.josm.data.osm.Node;
14import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
15import org.openstreetmap.josm.data.osm.Relation;
16import org.openstreetmap.josm.data.osm.RelationMember;
17import org.openstreetmap.josm.data.osm.Way;
18
19public class AssociatedStreetFixer extends RelationFixer {
20
21 public AssociatedStreetFixer() {
22 super("associatedStreet");
23 }
24
25 @Override
26 public boolean isRelationGood(Relation rel) {
27 for (RelationMember m : rel.getMembers()) {
28 if (m.getType().equals(OsmPrimitiveType.NODE) && !"house".equals(m.getRole())) {
29 setWarningMessage(tr("Node without ''house'' role found"));
30 return false;
31 }
32 if (m.getType().equals(OsmPrimitiveType.WAY) && !("house".equals(m.getRole()) || "street".equals(m.getRole()))) {
33 setWarningMessage(tr("Way without ''house'' or ''street'' role found"));
34 return false;
35 }
36 if (m.getType().equals(OsmPrimitiveType.RELATION) && !"house".equals(m.getRole())) {
37 setWarningMessage(tr("Relation without ''house'' role found"));
38 return false;
39 }
40 }
41 // relation should have name
42 if (!rel.hasKey("name")) {
43 setWarningMessage(tr("Relation does not have name"));
44 return false;
45 }
46 // check that all street members have same name as relation (???)
47 String streetName = rel.get("name");
48 if (streetName == null) streetName = "";
49 for (RelationMember m : rel.getMembers()) {
50 if ("street".equals(m.getRole()) && !streetName.equals(m.getWay().get("name"))) {
51 String anotherName = m.getWay().get("name");
52 if (anotherName != null && !anotherName.isEmpty()) {
53 setWarningMessage(tr("Relation has streets with different names"));
54 return false;
55 }
56 }
57 }
58 clearWarningMessage();
59 return true;
60 }
61
62 @Override
63 public Command fixRelation(Relation source) {
64 // any way with highway tag -> street
65 // any way/point/relation with addr:housenumber=* or building=* or type=multipolygon -> house
66 // name - check which name is most used in street members and add to relation
67 // copy this name to the other street members (???)
68 Relation rel = new Relation(source);
69 boolean fixed = false;
70
71 for (int i = 0; i < rel.getMembersCount(); i++) {
72 RelationMember m = rel.getMember(i);
73
74 if (m.isNode()) {
75 Node node = m.getNode();
76 if (!"house".equals(m.getRole()) &&
77 (node.hasKey("building") || node.hasKey("addr:housenumber"))) {
78 fixed = true;
79 rel.setMember(i, new RelationMember("house", node));
80 }
81 } else if (m.isWay()) {
82 Way way = m.getWay();
83 if (!"street".equals(m.getRole()) && way.hasKey("highway")) {
84 fixed = true;
85 rel.setMember(i, new RelationMember("street", way));
86 } else if (!"house".equals(m.getRole()) &&
87 (way.hasKey("building") || way.hasKey("addr:housenumber"))) {
88 fixed = true;
89 rel.setMember(i, new RelationMember("house", way));
90 }
91 } else if (m.isRelation()) {
92 Relation relation = m.getRelation();
93 if (!"house".equals(m.getRole()) &&
94 (relation.hasKey("building") || relation.hasKey("addr:housenumber") || "multipolygon".equals(relation.get("type")))) {
95 fixed = true;
96 rel.setMember(i, new RelationMember("house", relation));
97 }
98 }
99 }
100
101 // fill relation name
102 Map<String, Integer> streetNames = new HashMap<>();
103 for (RelationMember m : rel.getMembers())
104 if ("street".equals(m.getRole()) && m.isWay()) {
105 String name = m.getWay().get("name");
106 if (name == null || name.isEmpty()) continue;
107
108 Integer count = streetNames.get(name);
109
110 streetNames.put(name, count != null? count + 1 : 1);
111 }
112 String commonName = "";
113 Integer commonCount = 0;
114 for (Map.Entry<String, Integer> entry : streetNames.entrySet()) {
115 if (entry.getValue() > commonCount) {
116 commonCount = entry.getValue();
117 commonName = entry.getKey();
118 }
119 }
120
121 if (!rel.hasKey("name") && !commonName.isEmpty()) {
122 fixed = true;
123 rel.put("name", commonName);
124 } else {
125 commonName = ""; // set empty common name - if we already have name on relation, do not overwrite it
126 }
127
128 List<Command> commandList = new ArrayList<>();
129 if (fixed) {
130 commandList.add(new ChangeCommand(source, rel));
131 }
132
133 /*if (!commonName.isEmpty())
134 // fill common name to streets
135 for (RelationMember m : rel.getMembers())
136 if ("street".equals(m.getRole()) && m.isWay()) {
137 String name = m.getWay().get("name");
138 if (commonName.equals(name)) continue;
139
140 // TODO: ask user if he really wants to overwrite street name??
141
142 Way oldWay = m.getWay();
143 Way newWay = new Way(oldWay);
144 newWay.put("name", commonName);
145
146 commandList.add(new ChangeCommand(oldWay, newWay));
147 }
148 */
149 // return results
150 if (commandList.size() == 0)
151 return null;
152 if (commandList.size() == 1)
153 return commandList.get(0);
154 return new SequenceCommand(tr("fix associatedStreet relation"), commandList);
155 }
156}
Note: See TracBrowser for help on using the repository browser.