Changeset 2998 in josm for trunk/src


Ignore:
Timestamp:
2010-02-16T20:36:17+01:00 (10 years ago)
Author:
mjulius
Message:

fixes #4539 - Exception copying layer
in DataSet.clone() substitute way nodes and relation members before adding to the new dataset

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r2982 r2998  
    559559    @Override public DataSet clone() {
    560560        DataSet ds = new DataSet();
     561        HashMap<OsmPrimitive, OsmPrimitive> primitivesMap = new HashMap<OsmPrimitive, OsmPrimitive>();
    561562        for (Node n : nodes) {
    562             ds.addPrimitive(new Node(n));
     563            Node newNode = new Node(n);
     564            primitivesMap.put(n, newNode);
     565            ds.addPrimitive(newNode);
    563566        }
    564567        for (Way w : ways) {
    565             ds.addPrimitive(new Way(w));
    566         }
    567         for (Relation e : relations) {
    568             ds.addPrimitive(new Relation(e));
     568            Way newWay = new Way(w);
     569            primitivesMap.put(w, newWay);
     570            List<Node> newNodes = new ArrayList<Node>();
     571            for (Node n: w.getNodes()) {
     572                newNodes.add((Node)primitivesMap.get(n));
     573            }
     574            newWay.setNodes(newNodes);
     575            ds.addPrimitive(newWay);
     576        }
     577        // Because relations can have other relations as members we first clone all relations
     578        // and then get the cloned members
     579        for (Relation r : relations) {
     580            Relation newRelation = new Relation(r);
     581            primitivesMap.put(r, newRelation);
     582        }
     583        for (Relation r : relations) {
     584            Relation newRelation = (Relation)primitivesMap.get(r);
     585            List<RelationMember> newMembers = new ArrayList<RelationMember>();
     586            for (RelationMember rm: r.getMembers()) {
     587                newMembers.add(new RelationMember(rm.getRole(), primitivesMap.get(rm.getMember())));
     588            }
     589            newRelation.setMembers(newMembers);
     590            ds.addPrimitive(newRelation);
    569591        }
    570592        for (DataSource source : dataSources) {
Note: See TracChangeset for help on using the changeset viewer.