source: osm/applications/editors/josm/plugins/utilsplugin/src/UtilsPlugin/JoinNodeWayAction.java@ 5449

Last change on this file since 5449 was 5449, checked in by ulf, 17 years ago

rename mergenodeway to joinnodeway, to reflect it's menu name

File size: 2.8 KB
Line 
1package UtilsPlugin;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.util.ArrayList;
6import java.util.LinkedList;
7import java.util.Collection;
8import java.util.Collections;
9import java.util.List;
10import java.util.HashMap;
11import java.util.HashSet;
12import java.util.Map;
13
14import java.awt.event.ActionEvent;
15import java.awt.event.KeyEvent;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.WaySegment;
20import org.openstreetmap.josm.data.osm.Way;
21import org.openstreetmap.josm.gui.MapFrame;
22import org.openstreetmap.josm.plugins.Plugin;
23import org.openstreetmap.josm.actions.JosmAction;
24import org.openstreetmap.josm.command.Command;
25import org.openstreetmap.josm.command.AddCommand;
26import org.openstreetmap.josm.command.DeleteCommand;
27import org.openstreetmap.josm.command.ChangeCommand;
28import org.openstreetmap.josm.command.SequenceCommand;
29import org.openstreetmap.josm.data.osm.OsmPrimitive;
30
31import javax.swing.AbstractAction;
32
33class JoinNodeWayAction extends JosmAction {
34 public JoinNodeWayAction() {
35 super(tr("Join node to way"), "joinnodeway",
36 tr("Join a node into the nearest way segments"), KeyEvent.VK_J, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK, true);
37 }
38
39 public void actionPerformed(ActionEvent e) {
40 Collection<OsmPrimitive> sel = Main.ds.getSelected();
41 if (sel.size() != 1 || !(sel.iterator().next() instanceof Node)) return;
42 Node node = (Node) sel.iterator().next();
43
44 List<WaySegment> wss = Main.map.mapView.getNearestWaySegments(
45 Main.map.mapView.getPoint(node.eastNorth));
46 HashMap<Way, List<Integer>> insertPoints = new HashMap<Way, List<Integer>>();
47 for (WaySegment ws : wss) {
48 List<Integer> is;
49 if (insertPoints.containsKey(ws.way)) {
50 is = insertPoints.get(ws.way);
51 } else {
52 is = new ArrayList<Integer>();
53 insertPoints.put(ws.way, is);
54 }
55
56 if (ws.way.nodes.get(ws.lowerIndex) != node
57 && ws.way.nodes.get(ws.lowerIndex+1) != node) {
58 is.add(ws.lowerIndex);
59 }
60 }
61
62 Collection<Command> cmds = new LinkedList<Command>();
63 for (Map.Entry<Way, List<Integer>> insertPoint : insertPoints.entrySet()) {
64 Way w = insertPoint.getKey();
65 Way wnew = new Way(w);
66 List<Integer> is = insertPoint.getValue();
67 pruneSuccsAndReverse(is);
68 for (int i : is) wnew.nodes.add(i+1, node);
69 cmds.add(new ChangeCommand(w, wnew));
70 }
71
72 Main.main.undoRedo.add(new SequenceCommand(tr("Join Node and Line"), cmds));
73 Main.map.repaint();
74 }
75
76 private static void pruneSuccsAndReverse(List<Integer> is) {
77 //if (is.size() < 2) return;
78
79 HashSet<Integer> is2 = new HashSet<Integer>();
80 for (int i : is) {
81 if (!is2.contains(i - 1) && !is2.contains(i + 1)) {
82 is2.add(i);
83 }
84 }
85 is.clear();
86 is.addAll(is2);
87 Collections.sort(is);
88 Collections.reverse(is);
89 }
90}
Note: See TracBrowser for help on using the repository browser.