Changeset 36217 in osm for applications/editors/josm/plugins/reltoolbox/src/relcontext/relationfix/AssociatedStreetFixer.java
- Timestamp:
- 2024-03-03T10:25:25+01:00 (9 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/reltoolbox/src/relcontext/relationfix/AssociatedStreetFixer.java
r36103 r36217 5 5 6 6 import java.util.ArrayList; 7 import java.util.Collections; 7 8 import java.util.HashMap; 8 9 import java.util.List; 9 10 import java.util.Map; 10 11 11 import org.openstreetmap.josm.command.ChangeCommand; 12 import org.openstreetmap.josm.command.ChangeMembersCommand; 13 import org.openstreetmap.josm.command.ChangePropertyCommand; 12 14 import org.openstreetmap.josm.command.Command; 13 15 import org.openstreetmap.josm.command.SequenceCommand; … … 23 25 public class AssociatedStreetFixer extends RelationFixer { 24 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 25 32 public AssociatedStreetFixer() { 26 33 super("associatedStreet"); … … 30 37 public boolean isRelationGood(Relation rel) { 31 38 for (RelationMember m : rel.getMembers()) { 32 if (m.getType() .equals(OsmPrimitiveType.NODE) && !"house".equals(m.getRole())) {39 if (m.getType() == OsmPrimitiveType.NODE && !HOUSE.equals(m.getRole())) { 33 40 setWarningMessage(tr("Node without ''house'' role found")); 34 41 return false; 35 42 } 36 if (m.getType() .equals(OsmPrimitiveType.WAY) && !("house".equals(m.getRole()) || "street".equals(m.getRole()))) {43 if (m.getType() == OsmPrimitiveType.WAY && !(HOUSE.equals(m.getRole()) || STREET.equals(m.getRole()))) { 37 44 setWarningMessage(tr("Way without ''house'' or ''street'' role found")); 38 45 return false; 39 46 } 40 if (m.getType() .equals(OsmPrimitiveType.RELATION) && !"house".equals(m.getRole())) {47 if (m.getType() == OsmPrimitiveType.RELATION && !HOUSE.equals(m.getRole())) { 41 48 setWarningMessage(tr("Relation without ''house'' role found")); 42 49 return false; … … 54 61 } 55 62 for (RelationMember m : rel.getMembers()) { 56 if ( "street".equals(m.getRole()) && !streetName.equals(m.getWay().get("name"))) {63 if (STREET.equals(m.getRole()) && !streetName.equals(m.getWay().get("name"))) { 57 64 String anotherName = m.getWay().get("name"); 58 65 if (anotherName != null && !anotherName.isEmpty()) { … … 72 79 // name - check which name is most used in street members and add to relation 73 80 // copy this name to the other street members (???) 74 Relation rel = new Relation(source);81 List<RelationMember> members = source.getMembers(); 75 82 boolean fixed = false; 76 83 77 for (int i = 0; i < rel.getMembersCount(); i++) { 78 RelationMember m = rel.getMember(i); 79 84 for (int i = 0; i < members.size(); i++) { 85 RelationMember m = members.get(i); 80 86 if (m.isNode()) { 81 87 Node node = m.getNode(); 82 if (! "house".equals(m.getRole()) &&83 (node.hasKey( "building") || node.hasKey("addr:housenumber"))) {88 if (!HOUSE.equals(m.getRole()) && 89 (node.hasKey(BUILDING) || node.hasKey(ADDR_HOUSENUMBER))) { 84 90 fixed = true; 85 rel.setMember(i, new RelationMember("house", node));91 members.set(i, new RelationMember(HOUSE, node)); 86 92 } 87 93 } else if (m.isWay()) { 88 94 Way way = m.getWay(); 89 if (! "street".equals(m.getRole()) && way.hasKey("highway")) {95 if (!STREET.equals(m.getRole()) && way.hasKey("highway")) { 90 96 fixed = true; 91 rel.setMember(i, new RelationMember("street", way));92 } else if (! "house".equals(m.getRole()) &&93 (way.hasKey( "building") || way.hasKey("addr:housenumber"))) {97 members.set(i, new RelationMember(STREET, way)); 98 } else if (!HOUSE.equals(m.getRole()) && 99 (way.hasKey(BUILDING) || way.hasKey(ADDR_HOUSENUMBER))) { 94 100 fixed = true; 95 rel.setMember(i, new RelationMember("house", way));101 members.set(i, new RelationMember(HOUSE, way)); 96 102 } 97 103 } else if (m.isRelation()) { 98 104 Relation relation = m.getRelation(); 99 if (! "house".equals(m.getRole()) &&100 (relation.hasKey( "building") || relation.hasKey("addr:housenumber") || "multipolygon".equals(relation.get("type")))) {105 if (!HOUSE.equals(m.getRole()) && 106 (relation.hasKey(BUILDING) || relation.hasKey(ADDR_HOUSENUMBER) || "multipolygon".equals(relation.get("type")))) { 101 107 fixed = true; 102 rel.setMember(i, new RelationMember("house", relation));108 members.set(i, new RelationMember(HOUSE, relation)); 103 109 } 104 110 } … … 107 113 // fill relation name 108 114 Map<String, Integer> streetNames = new HashMap<>(); 109 for (RelationMember m : rel.getMembers()) {110 if ( "street".equals(m.getRole()) && m.isWay()) {115 for (RelationMember m : members) { 116 if (STREET.equals(m.getRole()) && m.isWay()) { 111 117 String name = m.getWay().get("name"); 112 118 if (name == null || name.isEmpty()) { 113 119 continue; 114 120 } 115 116 121 Integer count = streetNames.get(name); 117 118 122 streetNames.put(name, count != null ? count + 1 : 1); 119 123 } … … 128 132 } 129 133 130 if (!rel.hasKey("name") && !commonName.isEmpty()) { 131 fixed = true; 132 rel.put("name", commonName); 133 } else { 134 commonName = ""; // set empty common name - if we already have name on relation, do not overwrite it 134 Map<String, String> nameTag = new HashMap<>(); 135 if (!source.hasKey("name") && !commonName.isEmpty()) { 136 nameTag.put("name", commonName); 135 137 } 136 138 137 139 List<Command> commandList = new ArrayList<>(); 140 final DataSet ds = Utils.firstNonNull(source.getDataSet(), MainApplication.getLayerManager().getEditDataSet()); 138 141 if (fixed) { 139 final DataSet ds = Utils.firstNonNull(source.getDataSet(), MainApplication.getLayerManager().getEditDataSet()); 140 commandList.add(new ChangeCommand(ds, source, rel)); 142 commandList.add(new ChangeMembersCommand(ds, source, members)); 143 } 144 if (!nameTag.isEmpty()) { 145 commandList.add(new ChangePropertyCommand(ds, Collections.singleton(source), nameTag)); 141 146 } 142 147 143 /*if (!commonName.isEmpty())144 // fill common name to streets145 for (RelationMember m : rel.getMembers())146 if ("street".equals(m.getRole()) && m.isWay()) {147 String name = m.getWay().get("name");148 if (commonName.equals(name)) continue;149 150 // TODO: ask user if he really wants to overwrite street name??151 152 Way oldWay = m.getWay();153 Way newWay = new Way(oldWay);154 newWay.put("name", commonName);155 156 commandList.add(new ChangeCommand(MainApplication.getLayerManager().getEditDataSet(), oldWay, newWay));157 }158 */159 148 // return results 160 if (commandList.isEmpty()) {149 if (commandList.isEmpty()) 161 150 return null; 162 }163 151 return SequenceCommand.wrapIfNeeded(tr("fix associatedStreet relation"), commandList); 164 152 }
Note:
See TracChangeset
for help on using the changeset viewer.