Changeset 1108 in josm for trunk


Ignore:
Timestamp:
2008-12-07T20:38:10+01:00 (15 years ago)
Author:
framm
Message:
  • new preference setting allows switching between decimal coordinates and degrees/minutes/seconds. patch by Markus Lindholm <markus.lindholm@…>, applied with modifications. Fixes #1730.
Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/coor/LatLon.java

    r655 r1108  
    22package org.openstreetmap.josm.data.coor;
    33
     4
     5import static org.openstreetmap.josm.tools.I18n.tr;
     6
    47import org.openstreetmap.josm.data.Bounds;
    58import org.openstreetmap.josm.data.projection.Projection;
     9
     10import java.text.DecimalFormat;
    611import java.text.NumberFormat;
    712
     
    1520public class LatLon extends Coordinate {
    1621
    17         public LatLon(double lat, double lon) {
    18                 super(lon, lat);
    19         }
     22    private static DecimalFormat cDmsMinuteFormatter = new DecimalFormat("00");
     23    private static DecimalFormat cDmsSecondFormatter = new DecimalFormat("00.0");
     24    private static DecimalFormat cDdFormatter = new DecimalFormat("###0.0000");
     25   
     26    /**
     27     * Possible ways to display coordinates
     28     */
     29    public enum CoordinateFormat {
     30        DECIMAL_DEGREES {public String toString() {return tr("Decimal Degrees");}}, 
     31        DEGREES_MINUTES_SECONDS {public String toString() {return tr("Degrees Minutes Seconds");}};
     32    }
     33   
     34    public static String dms(double pCoordinate) {
    2035
    21         public double lat() {
    22                 return y;
    23         }
     36        double tAbsCoord = Math.abs(pCoordinate);
     37        int tDegree = (int) tAbsCoord;
     38        double tTmpMinutes = (tAbsCoord - tDegree) * 60;
     39        int tMinutes = (int) tTmpMinutes;
     40        double tSeconds = (tTmpMinutes - tMinutes) * 60;
    2441
    25         public double lon() {
    26                 return x;
    27         }
     42        return tDegree + "\u00B0" + cDmsMinuteFormatter.format(tMinutes) + "\'"
     43            + cDmsSecondFormatter.format(tSeconds) + "\"";
     44    }
    2845
    29         /**
    30          * @return <code>true</code> if the other point has almost the same lat/lon
    31          * values, only differing by no more than
    32          * 1 / {@link org.openstreetmap.josm.data.projection.Projection#MAX_SERVER_PRECISION MAX_SERVER_PRECISION}.
    33          */
    34         public boolean equalsEpsilon(LatLon other) {
    35                 final double p = 1/Projection.MAX_SERVER_PRECISION;
    36                 return Math.abs(lat()-other.lat()) <= p && Math.abs(lon()-other.lon()) <= p;
    37         }
     46    public LatLon(double lat, double lon) {
     47        super(lon, lat);
     48    }
    3849
    39         /**
    40          * @return <code>true</code>, if the coordinate is outside the world, compared
     50    public double lat() {
     51        return y;
     52    }
     53   
     54    public String latToString(CoordinateFormat d) {
     55        switch(d) {
     56        case DECIMAL_DEGREES: return cDdFormatter.format(y);
     57        case DEGREES_MINUTES_SECONDS: return dms(y) + ((y < 0) ? tr("S") : tr("N"));
     58        default: return "ERR";
     59        }
     60    }
     61   
     62    public double lon() {
     63        return x;
     64    }
     65   
     66    public String lonToString(CoordinateFormat d) {
     67        switch(d) {
     68        case DECIMAL_DEGREES: return cDdFormatter.format(x);
     69        case DEGREES_MINUTES_SECONDS: return dms(x) + ((x < 0) ? tr("W") : tr("E"));
     70        default: return "ERR";
     71        }
     72    }
     73   
     74 
     75
     76    /**
     77     * @return <code>true</code> if the other point has almost the same lat/lon
     78     * values, only differing by no more than
     79     * 1 / {@link org.openstreetmap.josm.data.projection.Projection#MAX_SERVER_PRECISION MAX_SERVER_PRECISION}.
     80     */
     81    public boolean equalsEpsilon(LatLon other) {
     82        final double p = 1/Projection.MAX_SERVER_PRECISION;
     83        return Math.abs(lat()-other.lat()) <= p && Math.abs(lon()-other.lon()) <= p;
     84    }
     85
     86    /**
     87     * @return <code>true</code>, if the coordinate is outside the world, compared
    4188         * by using lat/lon.
    4289         */
  • trunk/src/org/openstreetmap/josm/data/osm/Node.java

    r999 r1108  
    99import org.openstreetmap.josm.data.coor.LatLon;
    1010import org.openstreetmap.josm.data.coor.EastNorth;
     11import org.openstreetmap.josm.data.coor.LatLon.CoordinateFormat;
    1112import org.openstreetmap.josm.data.osm.visitor.Visitor;
    1213
     
    2122        public LatLon coor;
    2223        public volatile EastNorth eastNorth;
     24   
     25    private static CoordinateFormat mCord;
     26   
     27    static {
     28        try {
     29            mCord = LatLon.CoordinateFormat.valueOf(Main.pref.get("coordinates"));
     30        } catch (IllegalArgumentException iae) {
     31            mCord =LatLon.CoordinateFormat.DECIMAL_DEGREES;
     32        }
     33    }
    2334
    2435        /**
     
    7889                        name = tr("incomplete");
    7990                } else {
    80                         NumberFormat latLonFormat = new DecimalFormat("###0.0000000");
    81 
    8291                        name = get("name");
    8392                        if (name == null)
    8493                                name = id == 0 ? "" : ""+id;
    85                         name += " ("+latLonFormat.format(coor.lat())+", "+latLonFormat.format(coor.lon())+")";
     94                        name += " (" + coor.latToString(mCord) + ", " + coor.lonToString(mCord) + ")";
    8695                }
    8796                return name;
  • trunk/src/org/openstreetmap/josm/gui/MapStatus.java

    r999 r1108  
    8989        }
    9090
    91     DecimalFormat latlon = new DecimalFormat("###0.0000");
    92     ImageLabel lonText = new ImageLabel("lon", tr("The geographic longitude at the mouse pointer."), 8);
     91        LatLon.CoordinateFormat mCord;
     92
     93    ImageLabel lonText = new ImageLabel("lon", tr("The geographic longitude at the mouse pointer."), 11);
    9394    ImageLabel nameText = new ImageLabel("name", tr("The name of the object at the mouse pointer."), 20);
    9495    JTextField helpText = new JTextField();
    95     ImageLabel latText = new ImageLabel("lat", tr("The geographic latitude at the mouse pointer."), 7);
     96    ImageLabel latText = new ImageLabel("lat", tr("The geographic latitude at the mouse pointer."), 10);
    9697    ImageLabel angleText = new ImageLabel("angle", tr("The angle between the previous and the current way segment."), 6);
    9798    ImageLabel headingText = new ImageLabel("heading", tr("The (compass) heading of the line segment being drawn."), 6);
     
    254255        public MapStatus(final MapFrame mapFrame) {
    255256                this.mv = mapFrame.mapView;
    256 
     257               
     258        try {
     259            mCord = LatLon.CoordinateFormat.valueOf(Main.pref.get("coordinates"));
     260        } catch (IllegalArgumentException iae) {
     261            mCord =LatLon.CoordinateFormat.DECIMAL_DEGREES;
     262        }
    257263                // Listen for mouse movements and set the position text field
    258264                mv.addMouseMotionListener(new MouseMotionListener(){
     
    263269                                if (mv.center == null)
    264270                                        return;
    265                                 // Do not update the view, if ctrl is pressed.
     271                                // Do not update the view if ctrl is pressed.
    266272                                if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) == 0) {
    267273                                        LatLon p = mv.getLatLon(e.getX(),e.getY());
    268                                         latText.setText(latlon.format(p.lat()));
    269                                         lonText.setText(latlon.format(p.lon()));
     274                                        latText.setText(p.latToString(mCord));
     275                                        lonText.setText(p.lonToString(mCord));
    270276                                }
    271277                        }
  • trunk/src/org/openstreetmap/josm/gui/preferences/ProjectionPreference.java

    r627 r1108  
    1414import org.openstreetmap.josm.Main;
    1515import org.openstreetmap.josm.data.projection.Projection;
     16import org.openstreetmap.josm.data.coor.LatLon.CoordinateFormat;
    1617import org.openstreetmap.josm.tools.GBC;
    1718
     
    2223         */
    2324        private JComboBox projectionCombo = new JComboBox(Projection.allProjections);
     25        private JComboBox coordinatesCombo = new JComboBox(CoordinateFormat.values());
    2426
    2527        public void addGui(PreferenceDialog gui) {
    26                 for (int i = 0; i < projectionCombo.getItemCount(); ++i) {
    27                         if (projectionCombo.getItemAt(i).getClass().getName().equals(Main.pref.get("projection"))) {
    28                                 projectionCombo.setSelectedIndex(i);
    29                                 break;
    30                         }
    31                 }
     28               
     29            for (int i = 0; i < projectionCombo.getItemCount(); ++i) {
     30                if (projectionCombo.getItemAt(i).getClass().getName().equals(Main.pref.get("projection"))) {
     31                    projectionCombo.setSelectedIndex(i);
     32                    break;
     33                }
     34            }
     35
     36            for (int i = 0; i < coordinatesCombo.getItemCount(); ++i) {
     37                if (((CoordinateFormat)coordinatesCombo.getItemAt(i)).name().equals(Main.pref.get("coordinates"))) {
     38                    coordinatesCombo.setSelectedIndex(i);
     39                    break;
     40                }
     41            }
     42
    3243                projectionCombo.addActionListener(gui.requireRestartAction);
     44        coordinatesCombo.addActionListener(gui.requireRestartAction);
    3345               
    3446                JPanel projPanel = new JPanel();
     
    3850                projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
    3951                projPanel.add(projectionCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
     52            projPanel.add(new JLabel(tr("Display coordinates as")), GBC.std().insets(5,5,0,5));
     53            projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
     54            projPanel.add(coordinatesCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
    4055                gui.map.add(projPanel, GBC.eol().insets(0,0,0,10).fill(GBC.HORIZONTAL));
    4156    }
     
    4358        public void ok() {
    4459                Main.pref.put("projection", projectionCombo.getSelectedItem().getClass().getName());
     60                Main.pref.put("coordinates", ((CoordinateFormat)coordinatesCombo.getSelectedItem()).name());
    4561    }
    4662}
Note: See TracChangeset for help on using the changeset viewer.