Changeset 2788 in osm for applications/editors/josm


Ignore:
Timestamp:
2007-05-06T19:50:38+02:00 (17 years ago)
Author:
frsantos
Message:

Fix for duplicated nodes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/DuplicateNode.java

    r2591 r2788  
    33import static org.openstreetmap.josm.tools.I18n.tr;
    44
    5 import java.util.List;
     5import java.util.*;
    66
    7 import org.openstreetmap.josm.command.Command;
    8 import org.openstreetmap.josm.command.DeleteCommand;
     7import javax.swing.JOptionPane;
     8
     9import org.openstreetmap.josm.Main;
     10import org.openstreetmap.josm.command.*;
    911import org.openstreetmap.josm.data.coor.LatLon;
    10 import org.openstreetmap.josm.data.osm.Node;
    11 import org.openstreetmap.josm.data.osm.OsmPrimitive;
     12import org.openstreetmap.josm.data.osm.*;
    1213import org.openstreetmap.josm.plugins.validator.Severity;
    1314import org.openstreetmap.josm.plugins.validator.Test;
     
    6061        }
    6162       
     63    /**
     64     * Merge the nodes into one.
     65     * Copied from UtilsPlugin.MergePointsAction
     66     */
    6267        @Override
    6368        public Command fixError(TestError testError)
    6469        {
    65                 //TODO Which should be the fix?
    66                 return new DeleteCommand(testError.getPrimitives());
     70        Collection<OsmPrimitive> sel = testError.getPrimitives();
     71        Collection<OsmPrimitive> nodes = new ArrayList<OsmPrimitive>();
     72       
     73        Node target = null;
     74        for (OsmPrimitive osm : sel)
     75            nodes.add(osm);
     76       
     77        if( nodes.size() < 2 )
     78            return null;
     79       
     80        for ( OsmPrimitive o : nodes )
     81        {
     82            Node n = (Node)o;
     83            if( target == null || target.id == 0 )
     84            {
     85                target = n;
     86                continue;
     87            }
     88            if( n.id == 0 )
     89                continue;
     90            if( n.id < target.id )
     91                target = n;
     92        }
     93        if( target == null )
     94            return null;
     95       
     96        // target is what we're merging into
     97        // nodes is the list of nodes to be removed
     98        nodes.remove(target);
     99       
     100        // Merge all properties
     101        Node newtarget = new Node(target);
     102        for (final OsmPrimitive o : nodes)
     103        {
     104            Node n = (Node)o;
     105            for ( String key : n.keySet() )
     106            {
     107                if( newtarget.keySet().contains(key) && !newtarget.get(key).equals(n.get(key)) )
     108                {
     109                    JOptionPane.showMessageDialog(Main.parent, tr("Nodes have conflicting key: " + key + " ["+newtarget.get(key)+", "+n.get(key)+"]"));
     110                    return null;
     111                }
     112                newtarget.put( key, n.get(key) );
     113            }
     114        }       
     115       
     116        // Since some segment may disappear, we need to track those too
     117        Collection<OsmPrimitive> seglist = new ArrayList<OsmPrimitive>();
     118               
     119        // Now do the merging
     120        Collection<Command> cmds = new LinkedList<Command>();
     121        for (final Segment s : Main.ds.segments)
     122        {
     123            if( s.deleted || s.incomplete )
     124                continue;
     125            if( !nodes.contains( s.from ) && !nodes.contains( s.to ) )
     126                continue;
     127               
     128            Segment newseg = new Segment(s);
     129            if( nodes.contains( s.from ) )
     130                newseg.from = target;
     131            if( nodes.contains( s.to ) )
     132                newseg.to = target;
     133
     134            // Is this node now a NULL node?
     135            if( newseg.from == newseg.to )
     136                seglist.add(s);
     137            else
     138                cmds.add(new ChangeCommand(s,newseg));
     139        }
     140       
     141        if( seglist.size() > 0 )  // Some segments to be deleted?
     142        {
     143            // We really want to delete this, but we must check if it is part of a way first
     144            for (final Way w : Main.ds.ways)
     145            {
     146                Way newway = null;
     147                if( w.deleted )
     148                    continue;
     149                for (final OsmPrimitive o : seglist )
     150                {
     151                    Segment s = (Segment)o;
     152                    if( w.segments.contains(s) )
     153                    {
     154                        if( newway == null )
     155                            newway = new Way(w);
     156                        newway.segments.remove(s);
     157                    }
     158                }
     159                if( newway != null )   // Made changes?
     160                {
     161                    // If no segments left, delete the way
     162                    if( newway.segments.size() == 0 )
     163                        cmds.add(new DeleteCommand(Arrays.asList(new OsmPrimitive[]{w})));
     164                    else
     165                        cmds.add(new ChangeCommand(w,newway));
     166                }
     167            }
     168            cmds.add(new DeleteCommand(seglist));
     169        }
     170
     171        cmds.add(new DeleteCommand(nodes));
     172        cmds.add(new ChangeCommand(target, newtarget));
     173        return new SequenceCommand(tr("Merge Nodes"), cmds);
    67174        }
    68175       
     
    70177        public boolean isFixable(TestError testError)
    71178        {
    72                 return false; //(testError.getTester() instanceof DuplicateNode);
     179                return (testError.getTester() instanceof DuplicateNode);
    73180        }       
    74181}
Note: See TracChangeset for help on using the changeset viewer.