| 1 | // License: GPL. See LICENSE file for details. |
| 2 | package org.openstreetmap.josm.plugins.validator.tests; |
| 3 | |
| 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
| 5 | |
| 6 | import java.util.Collection; |
| 7 | import java.util.HashSet; |
| 8 | import java.util.LinkedList; |
| 9 | import java.util.List; |
| 10 | import java.util.Map; |
| 11 | import java.util.Vector; |
| 12 | |
| 13 | import org.openstreetmap.josm.Main; |
| 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.Relation; |
| 22 | import org.openstreetmap.josm.data.osm.RelationMember; |
| 23 | import org.openstreetmap.josm.data.osm.Way; |
| 24 | import org.openstreetmap.josm.gui.progress.ProgressMonitor; |
| 25 | import org.openstreetmap.josm.plugins.validator.Severity; |
| 26 | import org.openstreetmap.josm.plugins.validator.Test; |
| 27 | import org.openstreetmap.josm.plugins.validator.TestError; |
| 28 | import org.openstreetmap.josm.plugins.validator.util.Bag; |
| 29 | /** |
| 30 | * Tests if there are duplicate relations |
| 31 | */ |
| 32 | public class DuplicateRelation extends Test |
| 33 | { |
| 34 | |
| 35 | private static class RelationPair { |
| 36 | public List<RelationMember> members; |
| 37 | public Map<String, String> keys; |
| 38 | public RelationPair(List<RelationMember> _members,Map<String, String> _keys) { |
| 39 | members=_members; |
| 40 | keys=_keys; |
| 41 | } |
| 42 | @Override |
| 43 | public int hashCode() { |
| 44 | return members.hashCode()+keys.hashCode(); |
| 45 | } |
| 46 | @Override |
| 47 | public boolean equals(Object obj) { |
| 48 | if (!(obj instanceof RelationPair)) return false; |
| 49 | RelationPair rp = (RelationPair) obj; |
| 50 | return rp.members.equals(members) && rp.keys.equals(keys); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | protected static int DUPLICATE_RELATION = 2001; |
| 55 | protected static int SAME_RELATION = 2002; |
| 56 | |
| 57 | /** Bag of all relations */ |
| 58 | Bag<RelationPair, OsmPrimitive> relations; |
| 59 | |
| 60 | /** Bag of all relations, regardless of keys */ |
| 61 | Bag<List<RelationMember>, OsmPrimitive> relations_nokeys; |
| 62 | |
| 63 | /** |
| 64 | * Constructor |
| 65 | */ |
| 66 | public DuplicateRelation() |
| 67 | { |
| 68 | super(tr("Duplicated relations")+".", |
| 69 | tr("This test checks that there are no relations with same tags and same members with same roles.")); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | @Override |
| 74 | public void startTest(ProgressMonitor monitor) |
| 75 | { |
| 76 | super.startTest(monitor); |
| 77 | relations = new Bag<RelationPair, OsmPrimitive>(1000); |
| 78 | relations_nokeys = new Bag<List<RelationMember>, OsmPrimitive>(1000); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void endTest() |
| 83 | { |
| 84 | super.endTest(); |
| 85 | for(List<OsmPrimitive> duplicated : relations.values() ) |
| 86 | { |
| 87 | if( duplicated.size() > 1) |
| 88 | { |
| 89 | TestError testError = new TestError(this, Severity.ERROR, tr("Duplicated relations"), DUPLICATE_RELATION, duplicated); |
| 90 | errors.add( testError ); |
| 91 | } |
| 92 | } |
| 93 | relations = null; |
| 94 | for(List<OsmPrimitive> duplicated : relations_nokeys.values() ) |
| 95 | { |
| 96 | if( duplicated.size() > 1) |
| 97 | { |
| 98 | TestError testError = new TestError(this, Severity.WARNING, tr("Relations with same members"), SAME_RELATION, duplicated); |
| 99 | errors.add( testError ); |
| 100 | } |
| 101 | } |
| 102 | relations_nokeys = null; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void visit(Relation r) |
| 107 | { |
| 108 | if( !r.isUsable() ) |
| 109 | return; |
| 110 | List<RelationMember> rMembers=r.getMembers(); |
| 111 | Map<String, String> rkeys=r.getKeys(); |
| 112 | rkeys.remove("created_by"); |
| 113 | RelationPair rKey=new RelationPair(rMembers,rkeys); |
| 114 | relations.add(rKey, r); |
| 115 | relations_nokeys.add(rMembers, r); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Fix the error by removing all but one instance of duplicate relations |
| 120 | */ |
| 121 | @Override |
| 122 | public Command fixError(TestError testError) |
| 123 | { |
| 124 | if (testError.getCode() == SAME_RELATION) return null; |
| 125 | Collection<? extends OsmPrimitive> sel = testError.getPrimitives(); |
| 126 | HashSet<Relation> rel_fix = new HashSet<Relation>(); |
| 127 | |
| 128 | for (OsmPrimitive osm : sel) |
| 129 | if (osm instanceof Relation) |
| 130 | rel_fix.add((Relation)osm); |
| 131 | |
| 132 | if( rel_fix.size() < 2 ) |
| 133 | return null; |
| 134 | |
| 135 | long idToKeep = 0; |
| 136 | Relation relationToKeep = rel_fix.iterator().next(); |
| 137 | // Only one relation will be kept - the one with lowest positive ID, if such exist |
| 138 | // or one "at random" if no such exists. Rest of the relations will be deleted |
| 139 | for (Relation w: rel_fix) { |
| 140 | if (!w.isNew()) { |
| 141 | if (idToKeep == 0 || w.getId() < idToKeep) { |
| 142 | idToKeep = w.getId(); |
| 143 | relationToKeep = w; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Find the relation that is member of one or more relations. (If any) |
| 149 | Relation relationWithRelations = null; |
| 150 | List<Relation> rel_ref = null; |
| 151 | for (Relation w : rel_fix) { |
| 152 | List<Relation> rel = OsmPrimitive.getFilteredList(w.getReferrers(), Relation.class); |
| 153 | if (!rel.isEmpty()) { |
| 154 | if (relationWithRelations != null) |
| 155 | throw new AssertionError("Cannot fix duplicate relations: More than one relation is member of another relation."); |
| 156 | relationWithRelations = w; |
| 157 | rel_ref = rel; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | Collection<Command> commands = new LinkedList<Command>(); |
| 162 | |
| 163 | // Fix relations. |
| 164 | if (relationWithRelations != null && relationToKeep != relationWithRelations) { |
| 165 | for (Relation rel : rel_ref) { |
| 166 | Relation newRel = new Relation(rel); |
| 167 | for (int i = 0; i < newRel.getMembers().size(); ++i) { |
| 168 | RelationMember m = newRel.getMember(i); |
| 169 | if (relationWithRelations.equals(m.getMember())) { |
| 170 | newRel.setMember(i, new RelationMember(m.getRole(), relationToKeep)); |
| 171 | } |
| 172 | } |
| 173 | commands.add(new ChangeCommand(rel, newRel)); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | //Delete all relations in the list |
| 178 | rel_fix.remove(relationToKeep); |
| 179 | commands.add(new DeleteCommand(rel_fix)); |
| 180 | return new SequenceCommand(tr("Delete duplicate relations"), commands); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public boolean isFixable(TestError testError) |
| 185 | { |
| 186 | if (!(testError.getTester() instanceof DuplicateRelation)) |
| 187 | return false; |
| 188 | |
| 189 | if (testError.getCode() == SAME_RELATION) return false; |
| 190 | |
| 191 | // We fix it only if there is no more than one relation that is relation member. |
| 192 | Collection<? extends OsmPrimitive> sel = testError.getPrimitives(); |
| 193 | HashSet<Relation> relations = new HashSet<Relation>(); |
| 194 | |
| 195 | for (OsmPrimitive osm : sel) |
| 196 | if (osm instanceof Relation) |
| 197 | relations.add((Relation)osm); |
| 198 | |
| 199 | if (relations.size() < 2) |
| 200 | return false; |
| 201 | |
| 202 | int relationsWithRelations = 0; |
| 203 | for (Relation w : relations) { |
| 204 | List<Relation> rel = OsmPrimitive.getFilteredList(w.getReferrers(), Relation.class); |
| 205 | if (!rel.isEmpty()) { |
| 206 | ++relationsWithRelations; |
| 207 | } |
| 208 | } |
| 209 | return (relationsWithRelations <= 1); |
| 210 | } |
| 211 | } |