Ignore:
Timestamp:
2007-10-19T00:13:15+02:00 (18 years ago)
Author:
gabriel
Message:

utilsplugin: Port to API 0.5.

Location:
applications/editors/josm/plugins/utilsplugin/src
Files:
2 added
1 copied

Legend:

Unmodified
Added
Removed
  • TabularUnified applications/editors/josm/plugins/utilsplugin/src/UtilsPlugin/MergeNodeWayAction.java

    r5075 r5076  
    44
    55import java.util.ArrayList;
    6 import java.util.Arrays;
    76import java.util.LinkedList;
    87import java.util.Collection;
     8import java.util.Collections;
     9import java.util.List;
     10import java.util.HashMap;
     11import java.util.HashSet;
     12import java.util.Map;
    913
    1014import java.awt.event.ActionEvent;
     
    1216import org.openstreetmap.josm.Main;
    1317import org.openstreetmap.josm.data.osm.Node;
    14 import org.openstreetmap.josm.data.osm.Segment;
     18import org.openstreetmap.josm.data.osm.WaySegment;
    1519import org.openstreetmap.josm.data.osm.Way;
    1620import org.openstreetmap.josm.gui.MapFrame;
    1721import org.openstreetmap.josm.plugins.Plugin;
     22import org.openstreetmap.josm.actions.JosmAction;
    1823import org.openstreetmap.josm.command.Command;
    1924import org.openstreetmap.josm.command.AddCommand;
     
    2530import javax.swing.AbstractAction;
    2631
    27 class MergePointLineAction extends AbstractAction {
    28         public MergePointLineAction() {
    29             super("Join Point and Segment");
     32class MergeNodeWayAction extends JosmAction {
     33        public MergeNodeWayAction() {
     34            super(tr("Join node to way"), "mergenodeway",
     35                        tr("Join a node into the nearest way segments"), 0, 0, true);
    3036        }
     37
    3138        public void actionPerformed(ActionEvent e) {
    3239                Collection<OsmPrimitive> sel = Main.ds.getSelected();
    33                 Node node = null;
    34                 Segment seg = null;
    35                 Way way = null;
     40                if (sel.size() != 1 || !(sel.iterator().next() instanceof Node)) return;
     41                Node node = (Node) sel.iterator().next();
    3642
    37                 boolean error = false;         
    38                 for (OsmPrimitive osm : sel)
    39                 {
    40                         if (osm instanceof Node)
    41                                 if( node == null )
    42                                         node = (Node)osm;
    43                                 else
    44                                         error = true;
    45                         if (osm instanceof Segment)
    46                                 if( seg == null )
    47                                         seg = (Segment)osm;
    48                                 else
    49                                         error = true;
    50                         if (osm instanceof Way)
    51                                 if( way == null )
    52                                         way = (Way)osm;
    53                                 else
    54                                         error = true;
    55                 }
    56                 if( node == null || !(seg == null ^ way == null))
    57                         error = true;
    58                 if( error )
    59                 {
    60                         javax.swing.JOptionPane.showMessageDialog(Main.parent, tr("Must select one node and one segment/way."));
    61                         return;
    62                 }
    63                 if( way != null )
    64                 {
    65                         if( way.isIncomplete() )
    66                         {
    67                                 javax.swing.JOptionPane.showMessageDialog(Main.parent, tr("Selected way must be complete."));
    68                                 return;
     43                List<WaySegment> wss = Main.map.mapView.getNearestWaySegments(
     44                        Main.map.mapView.getPoint(node.eastNorth));
     45                HashMap<Way, List<Integer>> insertPoints = new HashMap<Way, List<Integer>>();
     46                for (WaySegment ws : wss) {
     47                        List<Integer> is;
     48                        if (insertPoints.containsKey(ws.way)) {
     49                                is = insertPoints.get(ws.way);
     50                        } else {
     51                                is = new ArrayList<Integer>();
     52                                insertPoints.put(ws.way, is);
    6953                        }
    70                         double mindist = 0;
    71 //                      System.out.println( node.toString() );
    72                         // If the user has selected a way and a point, we need to determine the segment that is closest to the given point.
    73                         for (Segment s : way.segments )
    74                         {
    75                                 if( s.incomplete )
    76                                         continue;
    77                                        
    78 //                              System.out.println( s.toString() );
    79                                 double dx1 = s.from.coor.lat() - node.coor.lat();
    80                                 double dy1 = s.from.coor.lon() - node.coor.lon();
    81                                 double dx2 = s.to.coor.lat() - node.coor.lat();
    82                                 double dy2 = s.to.coor.lon() - node.coor.lon();
    83                                
    84 //                              System.out.println( dx1+","+dx2+" && "+dy1+","+dy2 );
    85                                 double len1 = Math.sqrt(dx1*dx1+dy1*dy1);
    86                                 double len2 = Math.sqrt(dx2*dx2+dy2*dy2);
    87                                 dx1 /= len1;
    88                                 dy1 /= len1;
    89                                 dx2 /= len2;
    90                                 dy2 /= len2;
    91 //                              System.out.println( dx1+","+dx2+" && "+dy1+","+dy2 );
    92                                
    93                                 double dist = dx1*dx2 + dy1*dy2;
    94 //                              System.out.println( "Dist: "+dist );
    95                                 if( dist < mindist )
    96                                 {
    97                                         mindist = dist;
    98                                         seg = s;
    99                                 }
    100                         }
    101                         if( seg == null )
    102                         {
    103                                 javax.swing.JOptionPane.showMessageDialog(Main.parent, tr("No segment found in range"));
    104                                 return;
     54
     55                        if (ws.way.nodes.get(ws.lowerIndex) != node
     56                                        && ws.way.nodes.get(ws.lowerIndex+1) != node) {
     57                                is.add(ws.lowerIndex);
    10558                        }
    10659                }
    10760
    108                 if( seg.incomplete )
    109                 {
    110                         javax.swing.JOptionPane.showMessageDialog(Main.parent, tr("Both objects must be complete."));
    111                         return;
     61                Collection<Command> cmds = new LinkedList<Command>();
     62                for (Map.Entry<Way, List<Integer>> insertPoint : insertPoints.entrySet()) {
     63                        Way w = insertPoint.getKey();
     64                        Way wnew = new Way(w);
     65                        List<Integer> is = insertPoint.getValue();
     66                        pruneSuccsAndReverse(is);
     67                        for (int i : is) wnew.nodes.add(i+1, node);
     68                        cmds.add(new ChangeCommand(w, wnew));
    11269                }
    113                 if( node == seg.from || node == seg.to )
    114                 {
    115                         javax.swing.JOptionPane.showMessageDialog(Main.parent, tr("Node can't be endpoint of segment"));
    116                         return;
    117                 }
    118                 // Now do the merging
    119                 Collection<Command> cmds = new LinkedList<Command>();
    120                 Segment newseg1 = new Segment(seg);
    121                 newseg1.to = node;
    122                 Segment newseg2 = new Segment(node, seg.to);
    123                 if (seg.keys != null)
    124                         newseg2.keys = new java.util.HashMap<String, String>(seg.keys);
    125                 newseg2.selected = newseg1.selected;
    126                                                
    127                 cmds.add(new ChangeCommand(seg,newseg1));
    128                 cmds.add(new AddCommand(newseg2));
    129                
    130                 // find ways affected and fix them up...
    131                 for (final Way w : Main.ds.ways)
    132                 {
    133                         if( w.deleted )
    134                                 continue;
    135                         int pos = w.segments.indexOf(seg);
    136                         if( pos == -1 )
    137                                 continue;
    138                         Way newway = new Way(w);
    139                         newway.segments.add(pos+1, newseg2);
    140                         cmds.add(new ChangeCommand(w,newway));
    141                 }
    142                 Main.main.editLayer().add(new SequenceCommand(tr("Join Node and Line"), cmds));
     70
     71                Main.main.undoRedo.add(new SequenceCommand(tr("Join Node and Line"), cmds));
    14372                Main.map.repaint();
    14473        }
    145     }
     74
     75        private static void pruneSuccsAndReverse(List<Integer> is) {
     76                //if (is.size() < 2) return;
     77
     78                HashSet<Integer> is2 = new HashSet<Integer>();
     79                for (int i : is) {
     80                        if (!is2.contains(i - 1) && !is2.contains(i + 1)) {
     81                                is2.add(i);
     82                        }
     83                }
     84                is.clear();
     85                is.addAll(is2);
     86                Collections.sort(is);
     87                Collections.reverse(is);
     88        }
     89}
Note: See TracChangeset for help on using the changeset viewer.