001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.coor.conversion;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.text.DecimalFormat;
007
008import org.openstreetmap.josm.data.coor.ILatLon;
009import org.openstreetmap.josm.spi.preferences.Config;
010
011/**
012 * Coordinate format that converts a coordinate to degrees and minutes (nautical format).
013 * @since 12735
014 */
015public class NauticalCoordinateFormat extends AbstractCoordinateFormat {
016    private static final DecimalFormat DM_MINUTE_FORMATTER = new DecimalFormat(
017            Config.getPref() == null ? "00.000" : Config.getPref().get("latlon.dm.decimal-format", "00.000"));
018    private static final String DM60 = DM_MINUTE_FORMATTER.format(60.0);
019    private static final String DM00 = DM_MINUTE_FORMATTER.format(0.0);
020
021    /**
022     * The unique instance.
023     */
024    public static final NauticalCoordinateFormat INSTANCE = new NauticalCoordinateFormat();
025
026    protected NauticalCoordinateFormat() {
027        super("NAUTICAL", tr("deg\u00B0 min'' (Nautical)"));
028    }
029
030    @Override
031    public String latToString(ILatLon ll) {
032        return degreesMinutes(ll.lat()) + ((ll.lat() < 0) ? SOUTH : NORTH);
033    }
034
035    @Override
036    public String lonToString(ILatLon ll) {
037        return degreesMinutes(ll.lon()) + ((ll.lon() < 0) ? WEST : EAST);
038    }
039
040    /**
041     * Replies the coordinate in degrees/minutes format
042     * @param pCoordinate The coordinate to convert
043     * @return The coordinate in degrees/minutes format
044     * @since 12537
045     */
046    public static String degreesMinutes(double pCoordinate) {
047
048        double tAbsCoord = Math.abs(pCoordinate);
049        int tDegree = (int) tAbsCoord;
050        double tMinutes = (tAbsCoord - tDegree) * 60;
051
052        String sDegrees = Integer.toString(tDegree);
053        String sMinutes = DM_MINUTE_FORMATTER.format(tMinutes);
054
055        if (sMinutes.equals(DM60)) {
056            sMinutes = DM00;
057            sDegrees = Integer.toString(tDegree+1);
058        }
059
060        return sDegrees + '\u00B0' + sMinutes + '\'';
061    }
062}