Changeset 22411 in osm for applications


Ignore:
Timestamp:
2010-07-21T19:55:15+02:00 (14 years ago)
Author:
upliner
Message:

reverter: Fix the bug with relation tags and prettify the dataset merger code

Location:
applications/editors/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/reverter/src/reverter/DataSetToCmd.java

    r21807 r22411  
    88
    99import org.openstreetmap.josm.command.ChangeCommand;
    10 import org.openstreetmap.josm.command.ChangeNodesCommand;
    1110import org.openstreetmap.josm.command.Command;
    1211import org.openstreetmap.josm.data.conflict.Conflict;
    1312import org.openstreetmap.josm.data.conflict.ConflictCollection;
    14 import org.openstreetmap.josm.data.osm.*;
     13import org.openstreetmap.josm.data.osm.DataSet;
     14import org.openstreetmap.josm.data.osm.Node;
     15import org.openstreetmap.josm.data.osm.OsmPrimitive;
     16import org.openstreetmap.josm.data.osm.Relation;
     17import org.openstreetmap.josm.data.osm.RelationMember;
     18import org.openstreetmap.josm.data.osm.Way;
    1519
    1620/**
     
    3842    }
    3943
    40     /**
    41      * Merges a primitive <code>other</code> of type <P> onto my primitives.
    42      *
    43      * @param <P>  the type of the other primitive
    44      * @param source  the other primitive
    45      */
    46     private void mergePrimitive(OsmPrimitive source) {
    47         if (source.isIncomplete()) return;
    48         if (!source.isVisible()) return;
    49         OsmPrimitive target = getMergeTarget(source);
    50         if (target.getVersion() == 0)
    51             throw new IllegalStateException(tr("Target of type {0} with id {1} has invalid version",
    52                     target.getType(), target.getUniqueId()));
    53         OsmPrimitive newTarget;
    54         switch(target.getType()) {
    55         case NODE: newTarget = new Node((Node)target); break;
    56         case WAY: newTarget = new Way((Way)target); break;
    57         case RELATION: newTarget = new Relation((Relation)target); break;
    58         default: throw new AssertionError();
    59         }
    60         newTarget.mergeFrom(source);
    61         newTarget.setOsmId(target.getId(), (int)target.getVersion());
    62         newTarget.setVisible(target.isVisible());
    63         newTarget.setDeleted(false);
    64         cmds.add(new ChangeCommand(target,newTarget));
    65     }
    66 
    6744    private OsmPrimitive getMergeTarget(OsmPrimitive mergeSource) {
    6845        OsmPrimitive p = targetDataSet.getPrimitiveById(mergeSource.getId(), mergeSource.getType());
     
    7249        return p;
    7350    }
    74    
    75     /**
    76      * Postprocess the dataset and fix all merged references to point to the actual
    77      * data.
    78      */
    79     public void fixReferences() {
    80         for (Way w : sourceDataSet.getWays()) {
    81                 mergeNodeList(w);
    82         }
    83         for (Relation r : sourceDataSet.getRelations()) {
    84                 mergeRelationMembers(r);
    85         }
     51
     52    private void mergePrimitive(OsmPrimitive source, OsmPrimitive target, OsmPrimitive newTarget) {
     53        newTarget.mergeFrom(source);
     54        newTarget.setOsmId(target.getId(), (int)target.getVersion());
     55        newTarget.setVisible(target.isVisible());
     56        newTarget.setDeleted(false);
    8657    }
    8758
    8859    /**
    89      * Merges the node list of a source way onto its target way.
     60     * Merges the source node onto its target node.
     61     *
     62     * @param source the source way
     63     */
     64    private void mergeNode(Node source) {
     65        if (source.isIncomplete()) return;
     66        if (!source.isVisible()) return;
     67        Node target = (Node)getMergeTarget(source);
     68
     69        Node newTarget = new Node(target);
     70        mergePrimitive(source, target, newTarget);
     71        cmds.add(new ChangeCommand(target,newTarget));
     72    }
     73
     74    /**
     75     * Merges the source way onto its target way.
    9076     *
    9177     * @param source the source way
     
    9480     *
    9581     */
    96     private void mergeNodeList(Way source) throws IllegalStateException {
     82    private void mergeWay(Way source) throws IllegalStateException {
    9783        if (source.isIncomplete()) return;
    9884        if (!source.isVisible()) return;
     
    11197            newNodes.add(targetNode);
    11298        }
    113         cmds.add(new ChangeNodesCommand(target,newNodes));
     99        Way newTarget = new Way(target);
     100        mergePrimitive(source, target, newTarget);
     101        newTarget.setNodes(newNodes);
     102        cmds.add(new ChangeCommand(target,newTarget));
    114103    }
    115104
    116105    /**
    117      * Merges the relation members of a source relation onto the corresponding target relation.
     106     * Merges the source relation onto the corresponding target relation.
    118107     * @param source the source relation
    119108     * @throws IllegalStateException thrown if there is no corresponding target relation
     
    121110     * members in source
    122111     */
    123     private void mergeRelationMembers(Relation source) throws IllegalStateException {
     112    private void mergeRelation(Relation source) throws IllegalStateException {
    124113        if (source.isIncomplete()) return;
    125114        if (!source.isVisible()) return;
     
    143132            newMembers.add(new RelationMember(sourceMember.getRole(), targetMember));
    144133        }
    145         Relation newRelation = new Relation(target);
     134        Relation newRelation = new Relation(target);
     135        mergePrimitive(source, target, newRelation);
    146136        newRelation.setMembers(newMembers);
    147137        cmds.add(new ChangeCommand(target,newRelation));
     
    149139    private void merge() {
    150140        for (Node node: sourceDataSet.getNodes()) {
    151             mergePrimitive(node);
     141            mergeNode(node);
    152142        }
    153143        for (Way way: sourceDataSet.getWays()) {
    154             mergePrimitive(way);
     144            mergeWay(way);
    155145        }
    156146        for (Relation relation: sourceDataSet.getRelations()) {
    157             mergePrimitive(relation);
     147            mergeRelation(relation);
    158148        }
    159         fixReferences();
    160149    }
    161150
Note: See TracChangeset for help on using the changeset viewer.