001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.event.ActionEvent; 007import java.util.Collection; 008import java.util.Iterator; 009 010import org.openstreetmap.josm.data.coor.EastNorth; 011import org.openstreetmap.josm.data.osm.DataSet; 012import org.openstreetmap.josm.data.osm.Node; 013import org.openstreetmap.josm.data.osm.OsmPrimitive; 014import org.openstreetmap.josm.data.osm.Way; 015import org.openstreetmap.josm.gui.MainApplication; 016import org.openstreetmap.josm.gui.layer.MapGridPaintable; 017import org.openstreetmap.josm.gui.layer.MapGridPaintable.GridType; 018 019/** 020 * Rotates the grid drawn over the map so that its lines run parallel to the current selection: either a single way 021 * (the direction from its first to its last node) or exactly two nodes. Switches the grid to projected coordinates, 022 * since a latitude/longitude grid cannot be rotated, and enables it. 023 * @see MapGridPaintable 024 * @see SetGridOriginAction 025 * @since 19640 026 */ 027public class AlignGridRotationAction extends JosmAction { 028 029 /** returned when the selection does not define a direction */ 030 private static final EastNorth[] EMPTY_DIRECTION = new EastNorth[0]; 031 032 /** 033 * Constructs a new {@code AlignGridRotationAction}. 034 */ 035 public AlignGridRotationAction() { 036 super(tr("Align rotation to selection"), "gridrotate", 037 tr("Rotate the grid so that its lines are parallel to the selected way " 038 + "(first to last node) or to the line between the two selected nodes. Switches to a projected grid."), 039 null, false); 040 } 041 042 /** 043 * Determines the direction given by the selection: a single way with at least two nodes, or exactly two nodes. 044 * @param ds the data set, may be null 045 * @return start and end point, or an empty array if the selection does not define a direction 046 */ 047 static EastNorth[] getSelectedDirection(DataSet ds) { 048 if (ds == null) { 049 return EMPTY_DIRECTION; 050 } 051 Collection<Way> ways = ds.getSelectedWays(); 052 Collection<Node> nodes = ds.getSelectedNodes(); 053 Node a = null; 054 Node b = null; 055 if (ways.size() == 1 && nodes.isEmpty()) { 056 Way w = ways.iterator().next(); 057 if (w.getNodesCount() >= 2) { 058 a = w.firstNode(); 059 b = w.isClosed() ? w.getNode(1) : w.lastNode(); 060 } 061 } else if (nodes.size() == 2 && ways.isEmpty()) { 062 Iterator<Node> it = nodes.iterator(); 063 a = it.next(); 064 b = it.next(); 065 } 066 if (a == null || b == null || !a.isLatLonKnown() || !b.isLatLonKnown()) { 067 return EMPTY_DIRECTION; 068 } 069 EastNorth[] result = {a.getEastNorth(), b.getEastNorth()}; 070 return result[0].equalsEpsilon(result[1], 1e-9) ? EMPTY_DIRECTION : result; 071 } 072 073 /** 074 * Computes the grid rotation (counter clockwise, in degrees, in the range [0, 90)) for which a grid line is 075 * parallel to the given direction. 076 * @param from start point 077 * @param to end point 078 * @return the rotation in degrees 079 */ 080 public static double rotationOf(EastNorth from, EastNorth to) { 081 double angle = Math.toDegrees(Math.atan2(to.north() - from.north(), to.east() - from.east())); 082 angle %= 90; 083 if (angle < 0) { 084 angle += 90; 085 } 086 return angle >= 90 - 1e-9 ? 0 : angle; 087 } 088 089 @Override 090 protected void updateEnabledState() { 091 setEnabled(getSelectedDirection(getLayerManager().getEditDataSet()).length == 2); 092 } 093 094 @Override 095 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { 096 updateEnabledState(); 097 } 098 099 @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}