source: osm/applications/editors/josm/plugins/utilsplugin/src/UtilsPlugin/MergeNodeWayAction.java@ 5080

Last change on this file since 5080 was 5076, checked in by gabriel, 17 years ago

utilsplugin: Port to API 0.5.

File size: 2.7 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;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.osm.Node;
18import org.openstreetmap.josm.data.osm.WaySegment;
19import org.openstreetmap.josm.data.osm.Way;
20import org.openstreetmap.josm.gui.MapFrame;
21import org.openstreetmap.josm.plugins.Plugin;
22import org.openstreetmap.josm.actions.JosmAction;
23import org.openstreetmap.josm.command.Command;
24import org.openstreetmap.josm.command.AddCommand;
25import org.openstreetmap.josm.command.DeleteCommand;
26import org.openstreetmap.josm.command.ChangeCommand;
27import org.openstreetmap.josm.command.SequenceCommand;
28import org.openstreetmap.josm.data.osm.OsmPrimitive;
29
30import javax.swing.AbstractAction;
31
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);
36 }
37
38 public void actionPerformed(ActionEvent e) {
39 Collection<OsmPrimitive> sel = Main.ds.getSelected();
40 if (sel.size() != 1 || !(sel.iterator().next() instanceof Node)) return;
41 Node node = (Node) sel.iterator().next();
42
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);
53 }
54
55 if (ws.way.nodes.get(ws.lowerIndex) != node
56 && ws.way.nodes.get(ws.lowerIndex+1) != node) {
57 is.add(ws.lowerIndex);
58 }
59 }
60
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));
69 }
70
71 Main.main.undoRedo.add(new SequenceCommand(tr("Join Node and Line"), cmds));
72 Main.map.repaint();
73 }
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 TracBrowser for help on using the repository browser.