source: josm/src/org/openstreetmap/josm/actions/AlignInCircleAction.java@ 146

Last change on this file since 146 was 146, checked in by imi, 18 years ago
  • added "Align nodes in a circle" patch from Matthew Newton.
  • added a couple of unit tests
File size: 2.6 KB
Line 
1package org.openstreetmap.josm.actions;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.event.ActionEvent;
6import java.awt.event.KeyEvent;
7import java.util.Collection;
8import java.util.LinkedList;
9
10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.command.Command;
14import org.openstreetmap.josm.command.MoveCommand;
15import org.openstreetmap.josm.command.SequenceCommand;
16import org.openstreetmap.josm.data.coor.EastNorth;
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20
21/**
22 * Aligns all selected nodes within a circle. (Usefull for roundabouts)
23 *
24 * @author Matthew Newton
25 */
26public final class AlignInCircleAction extends JosmAction {
27
28 public AlignInCircleAction() {
29 super(tr("Align Nodes in Circle"), "aligncircle", tr("Move the selected nodes into a circle."), KeyEvent.VK_O, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK);
30 }
31
32 public void actionPerformed(ActionEvent e) {
33 Collection<OsmPrimitive> sel = Main.ds.getSelected();
34 Collection<Node> nodes = new LinkedList<Node>();
35 for (OsmPrimitive osm : sel)
36 if (osm instanceof Node)
37 nodes.add((Node)osm);
38 if (nodes.size() < 4) {
39 JOptionPane.showMessageDialog(Main.parent, tr("Please select at least four nodes."));
40 return;
41 }
42
43 // Get average position of all nodes
44 Node avn = new Node(new LatLon(0,0));
45 for (Node n : nodes) {
46 avn.eastNorth = new EastNorth(avn.eastNorth.east()+n.eastNorth.east(), avn.eastNorth.north()+n.eastNorth.north());
47 avn.coor = Main.proj.eastNorth2latlon(avn.eastNorth);
48 }
49 avn.eastNorth = new EastNorth(avn.eastNorth.east()/nodes.size(), avn.eastNorth.north()/nodes.size());
50 avn.coor = Main.proj.eastNorth2latlon(avn.eastNorth);
51 // Node "avn" now is central to all selected nodes.
52
53 // Now calculate the average distance to each node from the
54 // centre.
55 double avdist = 0;
56 for (Node n : nodes)
57 avdist += Math.sqrt(avn.eastNorth.distance(n.eastNorth));
58 avdist = avdist / nodes.size();
59
60 Collection<Command> cmds = new LinkedList<Command>();
61 // Move each node to that distance from the centre.
62 for (Node n : nodes) {
63 double dx = n.eastNorth.east() - avn.eastNorth.east();
64 double dy = n.eastNorth.north() - avn.eastNorth.north();
65 double dist = Math.sqrt(avn.eastNorth.distance(n.eastNorth));
66 cmds.add(new MoveCommand(n, (dx * (avdist / dist)) - dx, (dy * (avdist / dist)) - dy));
67 }
68
69 Main.main.editLayer().add(new SequenceCommand(tr("Align Nodes in Circle"), cmds));
70 Main.ds.setSelected(avn);
71 Main.map.repaint();
72 }
73}
Note: See TracBrowser for help on using the repository browser.