1 | package UtilsPlugin;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.LinkedList;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import java.awt.event.ActionEvent;
|
---|
15 | import java.awt.event.KeyEvent;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
20 | import org.openstreetmap.josm.data.osm.Way;
|
---|
21 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
22 | import org.openstreetmap.josm.plugins.Plugin;
|
---|
23 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
24 | import org.openstreetmap.josm.command.Command;
|
---|
25 | import org.openstreetmap.josm.command.AddCommand;
|
---|
26 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
27 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
28 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
29 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
30 |
|
---|
31 | import javax.swing.AbstractAction;
|
---|
32 |
|
---|
33 | class 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 | }
|
---|