| 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 |
|
|---|
| 9 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 10 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 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.visitor.AllNodesVisitor;
|
|---|
| 15 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
|---|
| 16 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 17 | import org.openstreetmap.josm.gui.layer.MapGridPaintable;
|
|---|
| 18 | import org.openstreetmap.josm.gui.layer.MapGridPaintable.GridType;
|
|---|
| 19 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Moves the origin of the grid drawn over the map to the current selection, so that grid lines pass through it,
|
|---|
| 23 | * and enables the grid. The origin is the position of the selected node, or the centroid (arithmetic mean
|
|---|
| 24 | * position) of all nodes reachable from the current selection (nodes of selected ways, node members of selected
|
|---|
| 25 | * relations) if more than one node is involved.
|
|---|
| 26 | * @see MapGridPaintable
|
|---|
| 27 | * @see AlignGridRotationAction
|
|---|
| 28 | * @since 19640
|
|---|
| 29 | */
|
|---|
| 30 | public class SetGridOriginAction extends JosmAction {
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Constructs a new {@code SetGridOriginAction}.
|
|---|
| 34 | */
|
|---|
| 35 | public SetGridOriginAction() {
|
|---|
| 36 | super(tr("Set origin to selection"), "gridorigin",
|
|---|
| 37 | tr("Move the grid so that a grid line passes through the selected node, "
|
|---|
| 38 | + "or through the centroid of the current selection, and show the grid."),
|
|---|
| 39 | null, false);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | @Override
|
|---|
| 43 | protected void updateEnabledState() {
|
|---|
| 44 | setEnabled(getCentroid(getLayerManager().getEditDataSet()) != null);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | @Override
|
|---|
| 48 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
|---|
| 49 | updateEnabledState();
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | @Override
|
|---|
| 53 | public void actionPerformed(ActionEvent e) {
|
|---|
| 54 | EastNorth centroid = getCentroid(getLayerManager().getEditDataSet());
|
|---|
| 55 | if (centroid == null) {
|
|---|
| 56 | return;
|
|---|
| 57 | }
|
|---|
| 58 | setOrigin(centroid);
|
|---|
| 59 | if (MainApplication.isDisplayingMapView()) {
|
|---|
| 60 | MainApplication.getMap().mapView.repaint();
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Computes the centroid of the current selection: the (arithmetic mean) position of every node reachable from
|
|---|
| 66 | * the selected primitives (the selected nodes themselves, the nodes of selected ways, and the node members of
|
|---|
| 67 | * selected relations). For a single selected node this is simply that node's position.
|
|---|
| 68 | * @param ds the data set, may be {@code null}
|
|---|
| 69 | * @return the centroid, or {@code null} if the selection contains no node with known coordinates
|
|---|
| 70 | */
|
|---|
| 71 | static EastNorth getCentroid(DataSet ds) {
|
|---|
| 72 | if (ds == null) {
|
|---|
| 73 | return null;
|
|---|
| 74 | }
|
|---|
| 75 | double sumEast = 0;
|
|---|
| 76 | double sumNorth = 0;
|
|---|
| 77 | int count = 0;
|
|---|
| 78 | for (Node n : AllNodesVisitor.getAllNodes(ds.getSelected())) {
|
|---|
| 79 | if (n.isLatLonKnown()) {
|
|---|
| 80 | EastNorth en = n.getEastNorth();
|
|---|
| 81 | sumEast += en.east();
|
|---|
| 82 | sumNorth += en.north();
|
|---|
| 83 | count++;
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 | return count == 0 ? null : new EastNorth(sumEast / count, sumNorth / count);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * Sets the grid origin (in the coordinates of the current grid type) and enables the grid.
|
|---|
| 91 | * @param position the new grid origin, in projected coordinates
|
|---|
| 92 | */
|
|---|
| 93 | public static void setOrigin(EastNorth position) {
|
|---|
| 94 | if (MapGridPaintable.TYPE.get() == GridType.PROJECTED) {
|
|---|
| 95 | MapGridPaintable.ORIGIN_X.put(position.east());
|
|---|
| 96 | MapGridPaintable.ORIGIN_Y.put(position.north());
|
|---|
| 97 | } else {
|
|---|
| 98 | final LatLon ll;
|
|---|
| 99 | 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 | }
|
|---|