[4051] | 1 | // License: GPL. See LICENSE file for details.
|
---|
| 2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.util.ArrayList;
|
---|
| 7 | import java.util.Collection;
|
---|
| 8 | import java.util.HashSet;
|
---|
| 9 | import java.util.LinkedList;
|
---|
| 10 | import java.util.List;
|
---|
| 11 | import java.util.Map;
|
---|
[5783] | 12 | import java.util.Set;
|
---|
[4051] | 13 |
|
---|
| 14 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
| 15 | import org.openstreetmap.josm.command.Command;
|
---|
| 16 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
| 17 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
| 18 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
| 19 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 21 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
| 22 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 23 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
| 24 | import org.openstreetmap.josm.data.osm.Way;
|
---|
| 25 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
| 26 | import org.openstreetmap.josm.data.validation.Test;
|
---|
| 27 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
[4234] | 28 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[4051] | 29 | import org.openstreetmap.josm.tools.MultiMap;
|
---|
[5608] | 30 |
|
---|
[4051] | 31 | /**
|
---|
| 32 | * Tests if there are duplicate relations
|
---|
| 33 | */
|
---|
[5608] | 34 | public class DuplicateRelation extends Test {
|
---|
[4051] | 35 |
|
---|
[5608] | 36 | /**
|
---|
| 37 | * Class to store one relation members and information about it
|
---|
| 38 | */
|
---|
[4874] | 39 | public static class RelMember {
|
---|
[5608] | 40 | /** Role of the relation member */
|
---|
[4051] | 41 | private String role;
|
---|
[5608] | 42 |
|
---|
| 43 | /** Type of the relation member */
|
---|
[4051] | 44 | private OsmPrimitiveType type;
|
---|
[5608] | 45 |
|
---|
| 46 | /** Tags of the relation member */
|
---|
[4051] | 47 | private Map<String, String> tags;
|
---|
[5608] | 48 |
|
---|
| 49 | /** Coordinates of the relation member */
|
---|
[4051] | 50 | private List<LatLon> coor;
|
---|
[5608] | 51 |
|
---|
| 52 | /** ID of the relation member in case it is a {@link Relation} */
|
---|
[6240] | 53 | private long relId;
|
---|
[4051] | 54 |
|
---|
| 55 | @Override
|
---|
| 56 | public int hashCode() {
|
---|
[6240] | 57 | return role.hashCode()+(int)relId+tags.hashCode()+type.hashCode()+coor.hashCode();
|
---|
[4051] | 58 | }
|
---|
| 59 |
|
---|
| 60 | @Override
|
---|
| 61 | public boolean equals(Object obj) {
|
---|
| 62 | if (!(obj instanceof RelMember)) return false;
|
---|
| 63 | RelMember rm = (RelMember) obj;
|
---|
[6240] | 64 | return rm.role.equals(role) && rm.type.equals(type) && rm.relId==relId && rm.tags.equals(tags) && rm.coor.equals(coor);
|
---|
[4051] | 65 | }
|
---|
| 66 |
|
---|
[5608] | 67 | /** Extract and store relation information based on the relation member
|
---|
| 68 | * @param src The relation member to store information about
|
---|
| 69 | */
|
---|
[4051] | 70 | public RelMember(RelationMember src) {
|
---|
[5608] | 71 | role = src.getRole();
|
---|
| 72 | type = src.getType();
|
---|
[6240] | 73 | relId = 0;
|
---|
[7005] | 74 | coor = new ArrayList<>();
|
---|
[4051] | 75 |
|
---|
| 76 | if (src.isNode()) {
|
---|
[5608] | 77 | Node r = src.getNode();
|
---|
| 78 | tags = r.getKeys();
|
---|
[7005] | 79 | coor = new ArrayList<>(1);
|
---|
[4051] | 80 | coor.add(r.getCoor());
|
---|
| 81 | }
|
---|
| 82 | if (src.isWay()) {
|
---|
[5608] | 83 | Way r = src.getWay();
|
---|
| 84 | tags = r.getKeys();
|
---|
[4051] | 85 | List<Node> wNodes = r.getNodes();
|
---|
[7005] | 86 | coor = new ArrayList<>(wNodes.size());
|
---|
[6104] | 87 | for (Node wNode : wNodes) {
|
---|
| 88 | coor.add(wNode.getCoor());
|
---|
[4051] | 89 | }
|
---|
| 90 | }
|
---|
| 91 | if (src.isRelation()) {
|
---|
[5608] | 92 | Relation r = src.getRelation();
|
---|
| 93 | tags = r.getKeys();
|
---|
[6240] | 94 | relId = r.getId();
|
---|
[7005] | 95 | coor = new ArrayList<>();
|
---|
[4051] | 96 | }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[5608] | 100 | /**
|
---|
| 101 | * Class to store relation members
|
---|
| 102 | */
|
---|
[7025] | 103 | private static class RelationMembers {
|
---|
[5608] | 104 | /** List of member objects of the relation */
|
---|
[6240] | 105 | private List<RelMember> members;
|
---|
[5608] | 106 |
|
---|
| 107 | /** Store relation information
|
---|
| 108 | * @param members The list of relation members
|
---|
| 109 | */
|
---|
| 110 | public RelationMembers(List<RelationMember> members) {
|
---|
[7005] | 111 | this.members = new ArrayList<>(members.size());
|
---|
[6104] | 112 | for (RelationMember member : members) {
|
---|
| 113 | this.members.add(new RelMember(member));
|
---|
[4051] | 114 | }
|
---|
| 115 | }
|
---|
[5608] | 116 |
|
---|
[4051] | 117 | @Override
|
---|
| 118 | public int hashCode() {
|
---|
| 119 | return members.hashCode();
|
---|
| 120 | }
|
---|
[5608] | 121 |
|
---|
[4051] | 122 | @Override
|
---|
| 123 | public boolean equals(Object obj) {
|
---|
| 124 | if (!(obj instanceof RelationMembers)) return false;
|
---|
| 125 | RelationMembers rm = (RelationMembers) obj;
|
---|
| 126 | return rm.members.equals(members);
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[5608] | 130 | /**
|
---|
| 131 | * Class to store relation data (keys are usually cleanup and may not be equal to original relation)
|
---|
| 132 | */
|
---|
[4051] | 133 | private class RelationPair {
|
---|
[5608] | 134 | /** Member objects of the relation */
|
---|
[6240] | 135 | private RelationMembers members;
|
---|
[5608] | 136 | /** Tags of the relation */
|
---|
[6240] | 137 | private Map<String, String> keys;
|
---|
[5608] | 138 |
|
---|
| 139 | /** Store relation information
|
---|
| 140 | * @param members The list of relation members
|
---|
| 141 | * @param keys The set of tags of the relation
|
---|
| 142 | */
|
---|
| 143 | public RelationPair(List<RelationMember> members, Map<String, String> keys) {
|
---|
| 144 | this.members = new RelationMembers(members);
|
---|
| 145 | this.keys = keys;
|
---|
[4051] | 146 | }
|
---|
[5608] | 147 |
|
---|
[4051] | 148 | @Override
|
---|
| 149 | public int hashCode() {
|
---|
| 150 | return members.hashCode()+keys.hashCode();
|
---|
| 151 | }
|
---|
[5608] | 152 |
|
---|
[4051] | 153 | @Override
|
---|
| 154 | public boolean equals(Object obj) {
|
---|
| 155 | if (!(obj instanceof RelationPair)) return false;
|
---|
| 156 | RelationPair rp = (RelationPair) obj;
|
---|
| 157 | return rp.members.equals(members) && rp.keys.equals(keys);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[5608] | 161 | /** Code number of completely duplicated relation error */
|
---|
[4869] | 162 | protected static final int DUPLICATE_RELATION = 1901;
|
---|
[5608] | 163 |
|
---|
| 164 | /** Code number of relation with same members error */
|
---|
[4869] | 165 | protected static final int SAME_RELATION = 1902;
|
---|
[4051] | 166 |
|
---|
| 167 | /** MultiMap of all relations */
|
---|
[6240] | 168 | private MultiMap<RelationPair, OsmPrimitive> relations;
|
---|
[4051] | 169 |
|
---|
| 170 | /** MultiMap of all relations, regardless of keys */
|
---|
[6240] | 171 | private MultiMap<List<RelationMember>, OsmPrimitive> relations_nokeys;
|
---|
[4051] | 172 |
|
---|
[5608] | 173 | /** List of keys without useful information */
|
---|
[7005] | 174 | private final Set<String> ignoreKeys = new HashSet<>(OsmPrimitive.getUninterestingKeys());
|
---|
[5608] | 175 |
|
---|
[4051] | 176 | /**
|
---|
[5608] | 177 | * Default constructor
|
---|
[4051] | 178 | */
|
---|
[5608] | 179 | public DuplicateRelation() {
|
---|
[4806] | 180 | super(tr("Duplicated relations"),
|
---|
[4234] | 181 | tr("This test checks that there are no relations with same tags and same members with same roles."));
|
---|
[4051] | 182 | }
|
---|
| 183 |
|
---|
| 184 | @Override
|
---|
[5608] | 185 | public void startTest(ProgressMonitor monitor) {
|
---|
[4234] | 186 | super.startTest(monitor);
|
---|
[7005] | 187 | relations = new MultiMap<>(1000);
|
---|
| 188 | relations_nokeys = new MultiMap<>(1000);
|
---|
[4051] | 189 | }
|
---|
| 190 |
|
---|
| 191 | @Override
|
---|
[5608] | 192 | public void endTest() {
|
---|
[4234] | 193 | super.endTest();
|
---|
[5783] | 194 | for (Set<OsmPrimitive> duplicated : relations.values()) {
|
---|
[5608] | 195 | if (duplicated.size() > 1) {
|
---|
[4051] | 196 | TestError testError = new TestError(this, Severity.ERROR, tr("Duplicated relations"), DUPLICATE_RELATION, duplicated);
|
---|
| 197 | errors.add( testError );
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | relations = null;
|
---|
[5783] | 201 | for (Set<OsmPrimitive> duplicated : relations_nokeys.values()) {
|
---|
[5608] | 202 | if (duplicated.size() > 1) {
|
---|
[4051] | 203 | TestError testError = new TestError(this, Severity.WARNING, tr("Relations with same members"), SAME_RELATION, duplicated);
|
---|
| 204 | errors.add( testError );
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | relations_nokeys = null;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | @Override
|
---|
[5608] | 211 | public void visit(Relation r) {
|
---|
[4290] | 212 | if (!r.isUsable() || r.hasIncompleteMembers())
|
---|
[4051] | 213 | return;
|
---|
[5608] | 214 | List<RelationMember> rMembers = r.getMembers();
|
---|
| 215 | Map<String, String> rkeys = r.getKeys();
|
---|
| 216 | for (String key : ignoreKeys)
|
---|
[5667] | 217 | rkeys.remove(key);
|
---|
[5608] | 218 | RelationPair rKey = new RelationPair(rMembers, rkeys);
|
---|
[4051] | 219 | relations.put(rKey, r);
|
---|
| 220 | relations_nokeys.put(rMembers, r);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | /**
|
---|
| 224 | * Fix the error by removing all but one instance of duplicate relations
|
---|
[5608] | 225 | * @param testError The error to fix, must be of type {@link #DUPLICATE_RELATION}
|
---|
[4051] | 226 | */
|
---|
| 227 | @Override
|
---|
[5608] | 228 | public Command fixError(TestError testError) {
|
---|
[4051] | 229 | if (testError.getCode() == SAME_RELATION) return null;
|
---|
| 230 | Collection<? extends OsmPrimitive> sel = testError.getPrimitives();
|
---|
[7005] | 231 | HashSet<Relation> relFix = new HashSet<>();
|
---|
[4051] | 232 |
|
---|
| 233 | for (OsmPrimitive osm : sel)
|
---|
[5667] | 234 | if (osm instanceof Relation && !osm.isDeleted()) {
|
---|
[6240] | 235 | relFix.add((Relation)osm);
|
---|
[4234] | 236 | }
|
---|
[4051] | 237 |
|
---|
[6240] | 238 | if (relFix.size() < 2)
|
---|
[4051] | 239 | return null;
|
---|
| 240 |
|
---|
| 241 | long idToKeep = 0;
|
---|
[6240] | 242 | Relation relationToKeep = relFix.iterator().next();
|
---|
[4051] | 243 | // Only one relation will be kept - the one with lowest positive ID, if such exist
|
---|
| 244 | // or one "at random" if no such exists. Rest of the relations will be deleted
|
---|
[6240] | 245 | for (Relation w: relFix) {
|
---|
| 246 | if (!w.isNew() && (idToKeep == 0 || w.getId() < idToKeep)) {
|
---|
| 247 | idToKeep = w.getId();
|
---|
| 248 | relationToKeep = w;
|
---|
[4051] | 249 | }
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | // Find the relation that is member of one or more relations. (If any)
|
---|
| 253 | Relation relationWithRelations = null;
|
---|
[6240] | 254 | List<Relation> relRef = null;
|
---|
| 255 | for (Relation w : relFix) {
|
---|
[4051] | 256 | List<Relation> rel = OsmPrimitive.getFilteredList(w.getReferrers(), Relation.class);
|
---|
| 257 | if (!rel.isEmpty()) {
|
---|
| 258 | if (relationWithRelations != null)
|
---|
| 259 | throw new AssertionError("Cannot fix duplicate relations: More than one relation is member of another relation.");
|
---|
| 260 | relationWithRelations = w;
|
---|
[6240] | 261 | relRef = rel;
|
---|
[4051] | 262 | }
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[7005] | 265 | Collection<Command> commands = new LinkedList<>();
|
---|
[4051] | 266 |
|
---|
| 267 | // Fix relations.
|
---|
| 268 | if (relationWithRelations != null && relationToKeep != relationWithRelations) {
|
---|
[6240] | 269 | for (Relation rel : relRef) {
|
---|
[4051] | 270 | Relation newRel = new Relation(rel);
|
---|
| 271 | for (int i = 0; i < newRel.getMembers().size(); ++i) {
|
---|
| 272 | RelationMember m = newRel.getMember(i);
|
---|
| 273 | if (relationWithRelations.equals(m.getMember())) {
|
---|
| 274 | newRel.setMember(i, new RelationMember(m.getRole(), relationToKeep));
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 | commands.add(new ChangeCommand(rel, newRel));
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | //Delete all relations in the list
|
---|
[6240] | 282 | relFix.remove(relationToKeep);
|
---|
| 283 | commands.add(new DeleteCommand(relFix));
|
---|
[4051] | 284 | return new SequenceCommand(tr("Delete duplicate relations"), commands);
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | @Override
|
---|
[5608] | 288 | public boolean isFixable(TestError testError) {
|
---|
| 289 | if (!(testError.getTester() instanceof DuplicateRelation)
|
---|
| 290 | || testError.getCode() == SAME_RELATION) return false;
|
---|
[4051] | 291 |
|
---|
| 292 | // We fix it only if there is no more than one relation that is relation member.
|
---|
| 293 | Collection<? extends OsmPrimitive> sel = testError.getPrimitives();
|
---|
[7005] | 294 | HashSet<Relation> relations = new HashSet<>();
|
---|
[4051] | 295 |
|
---|
| 296 | for (OsmPrimitive osm : sel)
|
---|
[4234] | 297 | if (osm instanceof Relation) {
|
---|
[4051] | 298 | relations.add((Relation)osm);
|
---|
[4234] | 299 | }
|
---|
[4051] | 300 |
|
---|
| 301 | if (relations.size() < 2)
|
---|
| 302 | return false;
|
---|
| 303 |
|
---|
| 304 | int relationsWithRelations = 0;
|
---|
| 305 | for (Relation w : relations) {
|
---|
| 306 | List<Relation> rel = OsmPrimitive.getFilteredList(w.getReferrers(), Relation.class);
|
---|
| 307 | if (!rel.isEmpty()) {
|
---|
| 308 | ++relationsWithRelations;
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 | return (relationsWithRelations <= 1);
|
---|
| 312 | }
|
---|
| 313 | }
|
---|