Ignore:
Timestamp:
2007-09-24T01:36:24+02:00 (17 years ago)
Author:
framm
Message:

This commit is a manual merge of all changes that have been made to
the intermediate "core_0.5" branch on the main OSM repository,
bevore JOSM was moved to openstreetmap.de.

Changes incorporated here:

r4464@svn.openstreetmap.org
r4466@svn.openstreetmap.org
r4468@svn.openstreetmap.org
r4469@svn.openstreetmap.org
r4479@svn.openstreetmap.org

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branch/0.5/src/org/openstreetmap/josm/data/osm/visitor/MergeVisitor.java

    r298 r329  
    1010
    1111import org.openstreetmap.josm.data.osm.DataSet;
     12import org.openstreetmap.josm.data.osm.Relation;
     13import org.openstreetmap.josm.data.osm.RelationMember;
    1214import org.openstreetmap.josm.data.osm.Node;
    1315import org.openstreetmap.josm.data.osm.OsmPrimitive;
    14 import org.openstreetmap.josm.data.osm.Segment;
    1516import org.openstreetmap.josm.data.osm.Way;
    1617
     
    2526        /**
    2627         * Map from primitives in the database to visited primitives. (Attention: The other way
    27          * round than mergedNodes and mergedSegments)
     28         * round than mergedNodes)
    2829         */
    2930        public Map<OsmPrimitive, OsmPrimitive> conflicts = new HashMap<OsmPrimitive, OsmPrimitive>();
     
    3839         */
    3940        private final Map<Node, Node> mergedNodes = new HashMap<Node, Node>();
    40         /**
    41          * A list of all segments that got replaced with others.
    42          * Key is the segment in the other's dataset and the value is the one that is now
    43          * in ds.segments.
    44          */
    45         private final Map<Segment, Segment> mergedSegments = new HashMap<Segment, Segment>();
    4641
    4742        public MergeVisitor(DataSet ds, DataSet mergeds) {
     
    8176
    8277        /**
    83          * Merge the segment if id matches or if both nodes are the same (and the
    84          * id is zero of either segment). Nodes are the "same" when they @see match
    85          */
    86         public void visit(Segment other) {
    87                 if (mergeAfterId(mergedSegments, ds.segments, other))
    88                         return;
    89 
    90                 Segment my = null;
    91                 for (Segment ls : ds.segments) {
    92                         if (match(other, ls) && ((mergeds == null) || (!mergeds.segments.contains(ls)))) {
    93                                 my = ls;
    94                                 break;
    95                         }
    96                 }
    97                
    98                 if (my == null)
    99                         ds.segments.add(other);
    100                 else if (my.incomplete && !other.incomplete) {
    101                         mergedSegments.put(other, my);
    102                         my.cloneFrom(other);
    103                 } else if (!other.incomplete) {
    104                         mergedSegments.put(other, my);
    105                         mergeCommon(my, other);
    106                         if (my.modified && !other.modified)
    107                                 return;
    108                         if (!match(my.from, other.from)) {
    109                                 my.from = other.from;
    110                                 my.modified = other.modified;
    111                         }
    112                         if (!match(my.to, other.to)) {
    113                                 my.to = other.to;
    114                                 my.modified = other.modified;
    115                         }
    116                 }
    117         }
    118 
     78         * Simply calls cloneFrom() for now.
     79         * Might be useful to keep around to facilitate merge with the relations
     80         * branch.
     81         */
    11982        private <T extends OsmPrimitive> void cloneFromExceptIncomplete(T myOsm, T otherOsm) {
    120                 if (!(myOsm instanceof Way))
    121                         myOsm.cloneFrom(otherOsm);
    122                 else {
    123                         Way my = (Way)myOsm;
    124                         Way other = (Way)otherOsm;
    125                         HashMap<Long, Segment> copy = new HashMap<Long, Segment>();
    126                         for (Segment s : my.segments)
    127                                 copy.put(s.id, s);
    128                         my.cloneFrom(other);
    129                         my.segments.clear();
    130                         for (Segment s : other.segments) {
    131                                 Segment myS = copy.get(s.id);
    132                                 if (s.incomplete && myS != null && !myS.incomplete) {
    133                                         mergedSegments.put(s, myS);
    134                                         my.segments.add(myS);
    135                                 } else
    136                                         my.segments.add(s);
    137                         }
    138                 }
     83                myOsm.cloneFrom(otherOsm);
    13984    }
    14085
    14186        /**
    142          * Merge the way if id matches or if all segments matches and the
     87         * Merge the way if id matches or if all nodes match and the
    14388         * id is zero of either way.
    14489         */
     
    155100                }
    156101                if (my == null) {
    157                         // Add the way and replace any incomplete segments that we already have
    158102                        ds.ways.add(other);
    159                         for (Segment s : other.segments) {
    160                                 if (s.incomplete) {
     103                } else {
     104                        mergeCommon(my, other);
     105                        if (my.modified && !other.modified)
     106                                return;
     107                        boolean same = true;
     108                        Iterator<Node> it = other.nodes.iterator();
     109                        for (Node n : my.nodes) {
     110                                if (!match(n, it.next()))
     111                                        same = false;
     112                        }
     113                        if (!same) {
     114                                my.nodes.clear();
     115                                my.nodes.addAll(other.nodes);
     116                                my.modified = other.modified;
     117                        }
     118                }
     119        }
     120
     121        /**
     122         * Merge the relation if id matches or if all members match and the
     123         * id of either relation is zero.
     124         */
     125        public void visit(Relation other) {
     126                if (mergeAfterId(null, ds.relations, other))
     127                        return;
     128
     129                Relation my = null;
     130                for (Relation e : ds.relations) {
     131                        if (match(other, e) && ((mergeds == null) || (!mergeds.relations.contains(e)))) {
     132                                my = e;
     133                                break;
     134                        }
     135                }
     136               
     137                if (my == null) {
     138                        // Add the relation and replace any incomplete segments that we already have
     139                        ds.relations.add(other);
     140                        // FIXME unclear!
     141                        /*
     142                        for (RelationMember em : other.getMembers()) {
     143                                if (em.member.incomplete) {
    161144                                        for (Segment ourSegment : ds.segments) {
    162145                                                if (ourSegment.id == s.id) {
     
    166149                                        }
    167150                                }
    168                         }
     151                        }*/
    169152                } else {
    170153                        mergeCommon(my, other);
     
    172155                                return;
    173156                        boolean same = true;
    174                         Iterator<Segment> it = other.segments.iterator();
    175                         for (Segment ls : my.segments) {
    176                                 if (!match(ls, it.next()))
     157                        if (other.members.size() != my.members.size()) {
    177158                                        same = false;
    178                         }
     159                        } else {
     160                                for (RelationMember em : my.members) {
     161                                        if (!other.members.contains(em)) {
     162                                                same = false;
     163                                                break;
     164                                        }
     165                                }
     166                        }
     167                        // FIXME Unclear
     168                        /*
    179169                        if (!same) {
    180170                                HashMap<Long, Segment> copy = new HashMap<Long, Segment>();
     
    192182                                my.modified = other.modified;
    193183                        }
     184                        */
    194185                }
    195186        }
     
    200191         */
    201192        public void fixReferences() {
    202                 for (Segment s : ds.segments)
    203                         fixSegment(s);
    204                 for (OsmPrimitive osm : conflicts.values())
    205                         if (osm instanceof Segment)
    206                                 fixSegment((Segment)osm);
    207193                for (Way w : ds.ways)
    208194                        fixWay(w);
     
    214200        private void fixWay(Way w) {
    215201            boolean replacedSomething = false;
    216             LinkedList<Segment> newSegments = new LinkedList<Segment>();
    217             for (Segment ls : w.segments) {
    218                 Segment otherLs = mergedSegments.get(ls);
    219                 newSegments.add(otherLs == null ? ls : otherLs);
    220                 if (otherLs != null)
     202            LinkedList<Node> newNodes = new LinkedList<Node>();
     203            for (Node n : w.nodes) {
     204                Node otherN = mergedNodes.get(n);
     205                newNodes.add(otherN == null ? n : otherN);
     206                if (otherN != null)
    221207                        replacedSomething = true;
    222208            }
    223209            if (replacedSomething) {
    224                 w.segments.clear();
    225                 w.segments.addAll(newSegments);
    226             }
    227             for (Segment ls : w.segments)
    228                 fixSegment(ls);
     210                w.nodes.clear();
     211                w.nodes.addAll(newNodes);
    229212    }
    230 
    231         private void fixSegment(Segment ls) {
    232                
    233             if (mergedNodes.containsKey(ls.from))
    234                 ls.from = mergedNodes.get(ls.from);
    235            
    236             if (mergedNodes.containsKey(ls.to)) 
    237                 ls.to = mergedNodes.get(ls.to);
    238            
    239213    }
    240214
    241215        /**
    242          * @return Whether the nodes matches (in sense of "be mergable").
     216         * @return Whether the nodes match (in sense of "be mergable").
    243217         */
    244218        private boolean match(Node n1, Node n2) {
     
    249223
    250224        /**
    251          * @return Whether the segments matches (in sense of "be mergable").
    252          */
    253         private boolean match(Segment ls1, Segment ls2) {
    254                 if (ls1.id == ls2.id && ls1.id != 0)
    255                         return true;
    256                 //if (ls1.id != 0 && ls2.id != 0)
    257                 //      return false;
    258                 if (ls1.incomplete || ls2.incomplete)
    259                         return false;
    260                 return match(ls1.from, ls2.from) && match(ls1.to, ls2.to);
    261         }
    262 
    263         /**
    264          * @return Whether the ways matches (in sense of "be mergable").
     225         * @return Whether the ways match (in sense of "be mergable").
    265226         */
    266227        private boolean match(Way w1, Way w2) {
    267228                if (w1.id == 0 || w2.id == 0) {
    268                         if (w1.segments.size() != w2.segments.size())
    269                                 return false;
    270                         Iterator<Segment> it = w1.segments.iterator();
    271                         for (Segment ls : w2.segments)
    272                                 if (!match(ls, it.next()))
     229                        if (w1.nodes.size() != w2.nodes.size())
     230                        return false;
     231                        Iterator<Node> it = w1.nodes.iterator();
     232                        for (Node n : w2.nodes)
     233                                if (!match(n, it.next()))
    273234                                        return false;
    274235                        return true;
     
    276237                return w1.id == w2.id;
    277238        }
     239        /**
     240         * @return Whether the relations match (in sense of "be mergable").
     241         */
     242        private boolean match(Relation w1, Relation w2) {
     243                // FIXME this is not perfect yet...
     244                if (w1.id == 0 || w2.id == 0) {
     245                        if (w1.members.size() != w2.members.size())
     246                                return false;
     247                        for (RelationMember em : w1.members) {
     248                                if (!w2.members.contains(em)) {
     249                                        return false;
     250                                }
     251                        }
     252                        return true;
     253                }
     254                return w1.id == w2.id;
     255        }
     256
    278257
    279258        /**
     
    327306                        }
    328307                        if (my.id == other.id && my.id != 0) {
    329                                 if (my instanceof Segment && ((Segment)my).incomplete)
    330                                         return false; // merge always over an incomplete
    331308                                if (my.modified && other.modified) {
    332309                                        conflicts.put(my, other);
Note: See TracChangeset for help on using the changeset viewer.