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

Last change on this file since 36423 was 36217, checked in by GerdP, 13 months ago

fix #23521: fix some memory leaks

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