Ignore:
Timestamp:
2012-03-02T06:02:28+01:00 (13 years ago)
Author:
joshdoe
Message:

utilsplugin2,conflation: refactor for clarity

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/dumbutils/ReplaceGeometryAction.java

    r27937 r27971  
    8080     * Replace or upgrade a node to a way or multipolygon
    8181     *
    82      * @param node
    83      * @param target
     82     * @param subjectNode node to be replaced
     83     * @param referenceObject object with greater spatial quality
    8484     */
    85     public boolean replaceNode(Node node, OsmPrimitive target) {
    86         if (!OsmPrimitive.getFilteredList(node.getReferrers(), Way.class).isEmpty()) {
     85    public boolean replaceNode(Node subjectNode, OsmPrimitive referenceObject) {
     86        if (!OsmPrimitive.getFilteredList(subjectNode.getReferrers(), Way.class).isEmpty()) {
    8787            JOptionPane.showMessageDialog(Main.parent, tr("Node belongs to way(s), cannot replace."),
    8888                    TITLE, JOptionPane.INFORMATION_MESSAGE);
     
    9090        }
    9191
    92         if (target instanceof Relation && !((Relation) target).isMultipolygon()) {
     92        if (referenceObject instanceof Relation && !((Relation) referenceObject).isMultipolygon()) {
    9393            JOptionPane.showMessageDialog(Main.parent, tr("Relation is not a multipolygon, cannot be used as a replacement."),
    9494                    TITLE, JOptionPane.INFORMATION_MESSAGE);
     
    9898        Node nodeToReplace = null;
    9999        // see if we need to replace a node in the replacement way to preserve connection in history
    100         if (!node.isNew()) {
     100        if (!subjectNode.isNew()) {
    101101            // Prepare a list of nodes that are not important
    102102            Collection<Node> nodePool = new HashSet<Node>();
    103             if (target instanceof Way) {
    104                 nodePool.addAll(getUnimportantNodes((Way) target));
    105             } else if (target instanceof Relation) {
    106                 for (RelationMember member : ((Relation) target).getMembers()) {
     103            if (referenceObject instanceof Way) {
     104                nodePool.addAll(getUnimportantNodes((Way) referenceObject));
     105            } else if (referenceObject instanceof Relation) {
     106                for (RelationMember member : ((Relation) referenceObject).getMembers()) {
    107107                    if ((member.getRole().equals("outer") || member.getRole().equals("inner"))
    108108                            && member.isWay()) {
     
    115115                assert false;
    116116            }
    117             nodeToReplace = findNearestNode(node, nodePool);
     117            nodeToReplace = findNearestNode(subjectNode, nodePool);
    118118        }
    119119
    120120        List<Command> commands = new ArrayList<Command>();
    121         AbstractMap<String, String> nodeTags = (AbstractMap<String, String>) node.getKeys();
     121        AbstractMap<String, String> nodeTags = (AbstractMap<String, String>) subjectNode.getKeys();
    122122
    123123        // merge tags
    124         Collection<Command> tagResolutionCommands = getTagConflictResolutionCommands(node, target);
     124        Collection<Command> tagResolutionCommands = getTagConflictResolutionCommands(subjectNode, referenceObject);
    125125        if (tagResolutionCommands == null) {
    126126            // user canceled tag merge dialog
     
    135135            List<Node> wayNodes = parentWay.getNodes();
    136136            int idx = wayNodes.indexOf(nodeToReplace);
    137             wayNodes.set(idx, node);
     137            wayNodes.set(idx, subjectNode);
    138138            if (idx == 0 && parentWay.isClosed()) {
    139139                // node is at start/end of way
    140                 wayNodes.set(wayNodes.size() - 1, node);
     140                wayNodes.set(wayNodes.size() - 1, subjectNode);
    141141            }
    142142            commands.add(new ChangeNodesCommand(parentWay, wayNodes));
    143             commands.add(new MoveCommand(node, nodeToReplace.getCoor()));
     143            commands.add(new MoveCommand(subjectNode, nodeToReplace.getCoor()));
    144144            commands.add(new DeleteCommand(nodeToReplace));
    145145
     
    147147            if (!nodeTags.isEmpty()) {
    148148                for (String key : nodeTags.keySet()) {
    149                     commands.add(new ChangePropertyCommand(node, key, null));
     149                    commands.add(new ChangePropertyCommand(subjectNode, key, null));
    150150                }
    151151
     
    153153        } else {
    154154            // no node to replace, so just delete the original node
    155             commands.add(new DeleteCommand(node));
    156         }
    157 
    158         getCurrentDataSet().setSelected(target);
     155            commands.add(new DeleteCommand(subjectNode));
     156        }
     157
     158        getCurrentDataSet().setSelected(referenceObject);
    159159
    160160        Main.main.undoRedo.add(new SequenceCommand(
    161                 tr("Replace geometry for node {0}", node.getDisplayName(DefaultNameFormatter.getInstance())),
     161                tr("Replace geometry for node {0}", subjectNode.getDisplayName(DefaultNameFormatter.getInstance())),
    162162                commands));
    163163        return true;
     
    184184            }
    185185        }
    186         Way geometry = selection.get(idxNew);
    187         Way way = selection.get(1 - idxNew);
    188        
    189         if( !overrideNewCheck && (way.isNew() || !geometry.isNew()) ) {
     186        Way referenceWay = selection.get(idxNew);
     187        Way subjectWay = selection.get(1 - idxNew);
     188       
     189        if( !overrideNewCheck && (subjectWay.isNew() || !referenceWay.isNew()) ) {
    190190            JOptionPane.showMessageDialog(Main.parent,
    191191                    tr("Please select one way that exists in the database and one new way with correct geometry."),
     
    193193            return false;
    194194        }
     195        return replaceWayWithWay(subjectWay, referenceWay);
     196    }
     197   
     198    public static boolean replaceWayWithWay(Way subjectWay, Way referenceWay) {
    195199
    196200        Area a = getCurrentDataSet().getDataSourceArea();
    197         if (!isInArea(way, a) || !isInArea(geometry, a)) {
     201        if (!isInArea(subjectWay, a) || !isInArea(referenceWay, a)) {
    198202            JOptionPane.showMessageDialog(Main.parent,
    199203                    tr("The ways must be entirely within the downloaded area."),
     
    202206        }
    203207       
    204         if (hasImportantNode(geometry, way)) {
     208        if (hasImportantNode(referenceWay, subjectWay)) {
    205209            JOptionPane.showMessageDialog(Main.parent,
    206210                    tr("The way to be replaced cannot have any nodes with properties or relation memberships unless they belong to both ways."),
     
    212216               
    213217        // merge tags
    214         Collection<Command> tagResolutionCommands = getTagConflictResolutionCommands(geometry, way);
     218        Collection<Command> tagResolutionCommands = getTagConflictResolutionCommands(referenceWay, subjectWay);
    215219        if (tagResolutionCommands == null) {
    216220            // user canceled tag merge dialog
     
    220224       
    221225        // Prepare a list of nodes that are not used anywhere except in the way
    222         Collection<Node> nodePool = getUnimportantNodes(way);
     226        Collection<Node> nodePool = getUnimportantNodes(subjectWay);
    223227
    224228        // And the same for geometry, list nodes that can be freely deleted
    225229        Set<Node> geometryPool = new HashSet<Node>();
    226         for( Node node : geometry.getNodes() ) {
     230        for( Node node : referenceWay.getNodes() ) {
    227231            List<OsmPrimitive> referrers = node.getReferrers();
    228232            if( node.isNew() && !node.isDeleted() && referrers.size() == 1
    229                     && referrers.get(0).equals(geometry) && !way.containsNode(node)
     233                    && referrers.get(0).equals(referenceWay) && !subjectWay.containsNode(node)
    230234                    && !hasInterestingKey(node))
    231235                geometryPool.add(node);
     
    248252
    249253        // And prepare a list of nodes with all the replacements
    250         List<Node> geometryNodes = geometry.getNodes();
     254        List<Node> geometryNodes = referenceWay.getNodes();
    251255        for( int i = 0; i < geometryNodes.size(); i++ )
    252256            if( nodeAssoc.containsKey(geometryNodes.get(i)) )
     
    254258
    255259        // Now do the replacement
    256         commands.add(new ChangeNodesCommand(way, geometryNodes));
     260        commands.add(new ChangeNodesCommand(subjectWay, geometryNodes));
    257261
    258262        // Move old nodes to new positions
     
    261265
    262266        // Remove geometry way from selection
    263         getCurrentDataSet().clearSelection(geometry);
     267        getCurrentDataSet().clearSelection(referenceWay);
    264268
    265269        // And delete old geometry way
    266         commands.add(new DeleteCommand(geometry));
     270        commands.add(new DeleteCommand(referenceWay));
    267271
    268272        // Delete nodes that are not used anymore
     
    272276        // Two items in undo stack: change original way and delete geometry way
    273277        Main.main.undoRedo.add(new SequenceCommand(
    274                 tr("Replace geometry for way {0}", way.getDisplayName(DefaultNameFormatter.getInstance())),
     278                tr("Replace geometry for way {0}", subjectWay.getDisplayName(DefaultNameFormatter.getInstance())),
    275279                commands));
    276280        return true;
     
    283287     * @return
    284288     */
    285     protected Collection<Node> getUnimportantNodes(Way way) {
     289    protected static Collection<Node> getUnimportantNodes(Way way) {
    286290        Set<Node> nodePool = new HashSet<Node>();
    287291        for (Node n : way.getNodes()) {
     
    302306     * @return
    303307     */
    304     protected boolean hasImportantNode(Way geometry, Way way) {
     308    protected static boolean hasImportantNode(Way geometry, Way way) {
    305309        for (Node n : way.getNodes()) {
    306310            // if original and replacement way share a node, it's safe to replace
     
    321325    }
    322326   
    323     protected boolean hasInterestingKey(OsmPrimitive object) {
     327    protected static boolean hasInterestingKey(OsmPrimitive object) {
    324328        for (String key : object.getKeys().keySet()) {
    325329            if (!OsmPrimitive.isUninterestingKey(key)) {
     
    355359     * needed.
    356360     *
    357      * @param source
    358      * @param target
     361     * @param source object tags are merged from
     362     * @param target object tags are merged to
    359363     * @return
    360364     */
    361     public List<Command> getTagConflictResolutionCommands(OsmPrimitive source, OsmPrimitive target) {
     365    protected static List<Command> getTagConflictResolutionCommands(OsmPrimitive source, OsmPrimitive target) {
    362366        Collection<OsmPrimitive> primitives = Arrays.asList(source, target);
    363367       
     
    394398     * @return null if there is no such node.
    395399     */
    396     private Node findNearestNode( Node node, Collection<Node> nodes ) {
     400    protected static Node findNearestNode( Node node, Collection<Node> nodes ) {
    397401        if( nodes.contains(node) )
    398402            return node;
Note: See TracChangeset for help on using the changeset viewer.