source: josm/trunk/src/org/openstreetmap/josm/actions/JoinNodeWayAction.java@ 729

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