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 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
19 | import org.openstreetmap.josm.data.osm.Way;
|
---|
20 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
21 | import org.openstreetmap.josm.plugins.Plugin;
|
---|
22 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
23 | import org.openstreetmap.josm.command.Command;
|
---|
24 | import org.openstreetmap.josm.command.AddCommand;
|
---|
25 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
26 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
27 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
28 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
29 |
|
---|
30 | import javax.swing.AbstractAction;
|
---|
31 |
|
---|
32 | class 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 | }
|
---|