| [32395] | 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| [28693] | 2 | package relcontext.relationfix;
|
|---|
| 3 |
|
|---|
| [28703] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| [28693] | 6 | import java.util.ArrayList;
|
|---|
| [36217] | 7 | import java.util.Collections;
|
|---|
| [28693] | 8 | import java.util.HashMap;
|
|---|
| 9 | import java.util.List;
|
|---|
| 10 | import java.util.Map;
|
|---|
| 11 |
|
|---|
| [36217] | 12 | import org.openstreetmap.josm.command.ChangeMembersCommand;
|
|---|
| 13 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
|---|
| [28693] | 14 | import org.openstreetmap.josm.command.Command;
|
|---|
| 15 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| [36103] | 16 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| [28693] | 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;
|
|---|
| [33708] | 22 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| [36103] | 23 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| [28693] | 24 |
|
|---|
| 25 | public class AssociatedStreetFixer extends RelationFixer {
|
|---|
| 26 |
|
|---|
| [36217] | 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 |
|
|---|
| [30738] | 32 | public AssociatedStreetFixer() {
|
|---|
| 33 | super("associatedStreet");
|
|---|
| 34 | }
|
|---|
| [28693] | 35 |
|
|---|
| [30738] | 36 | @Override
|
|---|
| 37 | public boolean isRelationGood(Relation rel) {
|
|---|
| 38 | for (RelationMember m : rel.getMembers()) {
|
|---|
| [36217] | 39 | if (m.getType() == OsmPrimitiveType.NODE && !HOUSE.equals(m.getRole())) {
|
|---|
| [30738] | 40 | setWarningMessage(tr("Node without ''house'' role found"));
|
|---|
| 41 | return false;
|
|---|
| 42 | }
|
|---|
| [36217] | 43 | if (m.getType() == OsmPrimitiveType.WAY && !(HOUSE.equals(m.getRole()) || STREET.equals(m.getRole()))) {
|
|---|
| [30738] | 44 | setWarningMessage(tr("Way without ''house'' or ''street'' role found"));
|
|---|
| 45 | return false;
|
|---|
| 46 | }
|
|---|
| [36217] | 47 | if (m.getType() == OsmPrimitiveType.RELATION && !HOUSE.equals(m.getRole())) {
|
|---|
| [30738] | 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");
|
|---|
| [32395] | 59 | if (streetName == null) {
|
|---|
| 60 | streetName = "";
|
|---|
| 61 | }
|
|---|
| [30738] | 62 | for (RelationMember m : rel.getMembers()) {
|
|---|
| [36217] | 63 | if (STREET.equals(m.getRole()) && !streetName.equals(m.getWay().get("name"))) {
|
|---|
| [30738] | 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 | }
|
|---|
| [28693] | 74 |
|
|---|
| [28703] | 75 | @Override
|
|---|
| [30738] | 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 (???)
|
|---|
| [36217] | 81 | List<RelationMember> members = source.getMembers();
|
|---|
| [30738] | 82 | boolean fixed = false;
|
|---|
| [28703] | 83 |
|
|---|
| [36217] | 84 | for (int i = 0; i < members.size(); i++) {
|
|---|
| 85 | RelationMember m = members.get(i);
|
|---|
| [30738] | 86 | if (m.isNode()) {
|
|---|
| 87 | Node node = m.getNode();
|
|---|
| [36217] | 88 | if (!HOUSE.equals(m.getRole()) &&
|
|---|
| 89 | (node.hasKey(BUILDING) || node.hasKey(ADDR_HOUSENUMBER))) {
|
|---|
| [30738] | 90 | fixed = true;
|
|---|
| [36217] | 91 | members.set(i, new RelationMember(HOUSE, node));
|
|---|
| [30738] | 92 | }
|
|---|
| 93 | } else if (m.isWay()) {
|
|---|
| 94 | Way way = m.getWay();
|
|---|
| [36217] | 95 | if (!STREET.equals(m.getRole()) && way.hasKey("highway")) {
|
|---|
| [30738] | 96 | fixed = true;
|
|---|
| [36217] | 97 | members.set(i, new RelationMember(STREET, way));
|
|---|
| 98 | } else if (!HOUSE.equals(m.getRole()) &&
|
|---|
| 99 | (way.hasKey(BUILDING) || way.hasKey(ADDR_HOUSENUMBER))) {
|
|---|
| [30738] | 100 | fixed = true;
|
|---|
| [36217] | 101 | members.set(i, new RelationMember(HOUSE, way));
|
|---|
| [30738] | 102 | }
|
|---|
| 103 | } else if (m.isRelation()) {
|
|---|
| 104 | Relation relation = m.getRelation();
|
|---|
| [36217] | 105 | if (!HOUSE.equals(m.getRole()) &&
|
|---|
| 106 | (relation.hasKey(BUILDING) || relation.hasKey(ADDR_HOUSENUMBER) || "multipolygon".equals(relation.get("type")))) {
|
|---|
| [30738] | 107 | fixed = true;
|
|---|
| [36217] | 108 | members.set(i, new RelationMember(HOUSE, relation));
|
|---|
| [30738] | 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| [28703] | 112 |
|
|---|
| [30738] | 113 | // fill relation name
|
|---|
| 114 | Map<String, Integer> streetNames = new HashMap<>();
|
|---|
| [36217] | 115 | for (RelationMember m : members) {
|
|---|
| 116 | if (STREET.equals(m.getRole()) && m.isWay()) {
|
|---|
| [30738] | 117 | String name = m.getWay().get("name");
|
|---|
| [32395] | 118 | if (name == null || name.isEmpty()) {
|
|---|
| 119 | continue;
|
|---|
| 120 | }
|
|---|
| [30738] | 121 | Integer count = streetNames.get(name);
|
|---|
| [32395] | 122 | streetNames.put(name, count != null ? count + 1 : 1);
|
|---|
| [30738] | 123 | }
|
|---|
| [32395] | 124 | }
|
|---|
| [30738] | 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 | }
|
|---|
| [28703] | 133 |
|
|---|
| [36217] | 134 | Map<String, String> nameTag = new HashMap<>();
|
|---|
| 135 | if (!source.hasKey("name") && !commonName.isEmpty()) {
|
|---|
| 136 | nameTag.put("name", commonName);
|
|---|
| [30738] | 137 | }
|
|---|
| [28703] | 138 |
|
|---|
| [30738] | 139 | List<Command> commandList = new ArrayList<>();
|
|---|
| [36217] | 140 | final DataSet ds = Utils.firstNonNull(source.getDataSet(), MainApplication.getLayerManager().getEditDataSet());
|
|---|
| [30738] | 141 | if (fixed) {
|
|---|
| [36217] | 142 | commandList.add(new ChangeMembersCommand(ds, source, members));
|
|---|
| [30738] | 143 | }
|
|---|
| [36217] | 144 | if (!nameTag.isEmpty()) {
|
|---|
| 145 | commandList.add(new ChangePropertyCommand(ds, Collections.singleton(source), nameTag));
|
|---|
| 146 | }
|
|---|
| [28703] | 147 |
|
|---|
| [30738] | 148 | // return results
|
|---|
| [36217] | 149 | if (commandList.isEmpty())
|
|---|
| [30738] | 150 | return null;
|
|---|
| [36102] | 151 | return SequenceCommand.wrapIfNeeded(tr("fix associatedStreet relation"), commandList);
|
|---|
| [30738] | 152 | }
|
|---|
| [28693] | 153 | }
|
|---|