| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 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.util.Collection;
|
|---|
| 8 | import java.util.Iterator;
|
|---|
| 9 |
|
|---|
| 10 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 11 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 12 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 15 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 16 | import org.openstreetmap.josm.gui.layer.MapGridPaintable;
|
|---|
| 17 | import org.openstreetmap.josm.gui.layer.MapGridPaintable.GridType;
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * Rotates the grid drawn over the map so that its lines run parallel to the current selection: either a single way
|
|---|
| 21 | * (the direction from its first to its last node) or exactly two nodes. Switches the grid to projected coordinates,
|
|---|
| 22 | * since a latitude/longitude grid cannot be rotated, and enables it.
|
|---|
| 23 | * @see MapGridPaintable
|
|---|
| 24 | * @see SetGridOriginAction
|
|---|
| 25 | * @since 19640
|
|---|
| 26 | */
|
|---|
| 27 | public class AlignGridRotationAction extends JosmAction {
|
|---|
| 28 |
|
|---|
| 29 | /** returned when the selection does not define a direction */
|
|---|
| 30 | private static final EastNorth[] EMPTY_DIRECTION = new EastNorth[0];
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Constructs a new {@code AlignGridRotationAction}.
|
|---|
| 34 | */
|
|---|
| 35 | public AlignGridRotationAction() {
|
|---|
| 36 | super(tr("Align rotation to selection"), "gridrotate",
|
|---|
| 37 | tr("Rotate the grid so that its lines are parallel to the selected way "
|
|---|
| 38 | + "(first to last node) or to the line between the two selected nodes. Switches to a projected grid."),
|
|---|
| 39 | null, false);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Determines the direction given by the selection: a single way with at least two nodes, or exactly two nodes.
|
|---|
| 44 | * @param ds the data set, may be null
|
|---|
| 45 | * @return start and end point, or an empty array if the selection does not define a direction
|
|---|
| 46 | */
|
|---|
| 47 | static EastNorth[] getSelectedDirection(DataSet ds) {
|
|---|
| 48 | if (ds == null) {
|
|---|
| 49 | return EMPTY_DIRECTION;
|
|---|
| 50 | }
|
|---|
| 51 | Collection<Way> ways = ds.getSelectedWays();
|
|---|
| 52 | Collection<Node> nodes = ds.getSelectedNodes();
|
|---|
| 53 | Node a = null;
|
|---|
| 54 | Node b = null;
|
|---|
| 55 | if (ways.size() == 1 && nodes.isEmpty()) {
|
|---|
| 56 | Way w = ways.iterator().next();
|
|---|
| 57 | if (w.getNodesCount() >= 2) {
|
|---|
| 58 | a = w.firstNode();
|
|---|
| 59 | b = w.isClosed() ? w.getNode(1) : w.lastNode();
|
|---|
| 60 | }
|
|---|
| 61 | } else if (nodes.size() == 2 && ways.isEmpty()) {
|
|---|
| 62 | Iterator<Node> it = nodes.iterator();
|
|---|
| 63 | a = it.next();
|
|---|
| 64 | b = it.next();
|
|---|
| 65 | }
|
|---|
| 66 | if (a == null || b == null || !a.isLatLonKnown() || !b.isLatLonKnown()) {
|
|---|
| 67 | return EMPTY_DIRECTION;
|
|---|
| 68 | }
|
|---|
| 69 | EastNorth[] result = {a.getEastNorth(), b.getEastNorth()};
|
|---|
| 70 | return result[0].equalsEpsilon(result[1], 1e-9) ? EMPTY_DIRECTION : result;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Computes the grid rotation (counter clockwise, in degrees, in the range [0, 90)) for which a grid line is
|
|---|
| 75 | * parallel to the given direction.
|
|---|
| 76 | * @param from start point
|
|---|
| 77 | * @param to end point
|
|---|
| 78 | * @return the rotation in degrees
|
|---|
| 79 | */
|
|---|
| 80 | public static double rotationOf(EastNorth from, EastNorth to) {
|
|---|
| 81 | double angle = Math.toDegrees(Math.atan2(to.north() - from.north(), to.east() - from.east()));
|
|---|
| 82 | angle %= 90;
|
|---|
| 83 | if (angle < 0) {
|
|---|
| 84 | angle += 90;
|
|---|
| 85 | }
|
|---|
| 86 | return angle >= 90 - 1e-9 ? 0 : angle;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | @Override
|
|---|
| 90 | protected void updateEnabledState() {
|
|---|
| 91 | setEnabled(getSelectedDirection(getLayerManager().getEditDataSet()).length == 2);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | @Override
|
|---|
| 95 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
|---|
| 96 | updateEnabledState();
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | @Override
|
|---|
| 100 | public void actionPerformed(ActionEvent e) {
|
|---|
| 101 | EastNorth[] direction = getSelectedDirection(getLayerManager().getEditDataSet());
|
|---|
| 102 | if (direction.length != 2) {
|
|---|
| 103 | return;
|
|---|
| 104 | }
|
|---|
| 105 | MapGridPaintable.ROTATION.put(rotationOf(direction[0], direction[1]));
|
|---|
| 106 | MapGridPaintable.TYPE.put(GridType.PROJECTED);
|
|---|
| 107 | MapGridPaintable.ENABLED.put(true);
|
|---|
| 108 | if (MainApplication.isDisplayingMapView()) {
|
|---|
| 109 | MainApplication.getMap().mapView.repaint();
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|