1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package relcontext.relationfix;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.command.ChangeMembersCommand;
|
---|
13 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
14 | import org.openstreetmap.josm.command.Command;
|
---|
15 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
19 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
20 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
22 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
23 | import org.openstreetmap.josm.tools.Utils;
|
---|
24 |
|
---|
25 | public 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 | }
|
---|