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; 008 009import org.openstreetmap.josm.data.coor.EastNorth; 010import org.openstreetmap.josm.data.coor.LatLon; 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.visitor.AllNodesVisitor; 015import org.openstreetmap.josm.data.projection.ProjectionRegistry; 016import org.openstreetmap.josm.gui.MainApplication; 017import org.openstreetmap.josm.gui.layer.MapGridPaintable; 018import org.openstreetmap.josm.gui.layer.MapGridPaintable.GridType; 019import org.openstreetmap.josm.tools.Logging; 020 021/** 022 * Moves the origin of the grid drawn over the map to the current selection, so that grid lines pass through it, 023 * and enables the grid. The origin is the position of the selected node, or the centroid (arithmetic mean 024 * position) of all nodes reachable from the current selection (nodes of selected ways, node members of selected 025 * relations) if more than one node is involved. 026 * @see MapGridPaintable 027 * @see AlignGridRotationAction 028 * @since 19640 029 */ 030public class SetGridOriginAction extends JosmAction { 031 032 /** 033 * Constructs a new {@code SetGridOriginAction}. 034 */ 035 public SetGridOriginAction() { 036 super(tr("Set origin to selection"), "gridorigin", 037 tr("Move the grid so that a grid line passes through the selected node, " 038 + "or through the centroid of the current selection, and show the grid."), 039 null, false); 040 } 041 042 @Override 043 protected void updateEnabledState() { 044 setEnabled(getCentroid(getLayerManager().getEditDataSet()) != null); 045 } 046 047 @Override 048 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { 049 updateEnabledState(); 050 } 051 052 @Override 053 public void actionPerformed(ActionEvent e) { 054 EastNorth centroid = getCentroid(getLayerManager().getEditDataSet()); 055 if (centroid == null) { 056 return; 057 } 058 setOrigin(centroid); 059 if (MainApplication.isDisplayingMapView()) { 060 MainApplication.getMap().mapView.repaint(); 061 } 062 } 063 064 /** 065 * Computes the centroid of the current selection: the (arithmetic mean) position of every node reachable from 066 * the selected primitives (the selected nodes themselves, the nodes of selected ways, and the node members of 067 * selected relations). For a single selected node this is simply that node's position. 068 * @param ds the data set, may be {@code null} 069 * @return the centroid, or {@code null} if the selection contains no node with known coordinates 070 */ 071 static EastNorth getCentroid(DataSet ds) { 072 if (ds == null) { 073 return null; 074 } 075 double sumEast = 0; 076 double sumNorth = 0; 077 int count = 0; 078 for (Node n : AllNodesVisitor.getAllNodes(ds.getSelected())) { 079 if (n.isLatLonKnown()) { 080 EastNorth en = n.getEastNorth(); 081 sumEast += en.east(); 082 sumNorth += en.north(); 083 count++; 084 } 085 } 086 return count == 0 ? null : new EastNorth(sumEast / count, sumNorth / count); 087 } 088 089 /** 090 * Sets the grid origin (in the coordinates of the current grid type) and enables the grid. 091 * @param position the new grid origin, in projected coordinates 092 */ 093 public static void setOrigin(EastNorth position) { 094 if (MapGridPaintable.TYPE.get() == GridType.PROJECTED) { 095 MapGridPaintable.ORIGIN_X.put(position.east()); 096 MapGridPaintable.ORIGIN_Y.put(position.north()); 097 } else { 098 final LatLon ll; 099 try { 100 ll = ProjectionRegistry.getProjection().eastNorth2latlon(position); 101 } catch (IllegalArgumentException e) { 102 // the position is outside the domain of the projection, leave the grid as it is 103 Logging.warn("Cannot use {0} as grid origin: {1}", position, e.getMessage()); 104 Logging.trace(e); 105 return; 106 } 107 MapGridPaintable.ORIGIN_X.put(ll.lon()); 108 MapGridPaintable.ORIGIN_Y.put(ll.lat()); 109 } 110 MapGridPaintable.ENABLED.put(true); 111 } 112}