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