source: josm/trunk/src/org/openstreetmap/josm/data/coor/conversion/NauticalCoordinateFormat.java@ 12735

Last change on this file since 12735 was 12735, checked in by bastiK, 7 years ago

see #15229 - move CoordinateFormat code out of LatLon class

File size: 1.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.coor.conversion;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.text.DecimalFormat;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.data.coor.ILatLon;
10
11/**
12 * Coordinate format that converts a coordinate to degrees and minutes (nautical format).
13 * @since 12735
14 */
15public class NauticalCoordinateFormat extends AbstractCoordinateFormat {
16 private static final DecimalFormat DM_MINUTE_FORMATTER = new DecimalFormat(
17 Main.pref == null ? "00.000" : Main.pref.get("latlon.dm.decimal-format", "00.000"));
18 private static final String DM60 = DM_MINUTE_FORMATTER.format(60.0);
19 private static final String DM00 = DM_MINUTE_FORMATTER.format(0.0);
20
21 public static NauticalCoordinateFormat INSTANCE = new NauticalCoordinateFormat();
22
23 private NauticalCoordinateFormat() {
24 super("NAUTICAL", tr("deg\u00B0 min'' (Nautical)"));
25 }
26
27 @Override
28 public String latToString(ILatLon ll) {
29 return degreesMinutes(ll.lat()) + ((ll.lat() < 0) ? SOUTH : NORTH);
30 }
31
32 @Override
33 public String lonToString(ILatLon ll) {
34 return degreesMinutes(ll.lon()) + ((ll.lon() < 0) ? WEST : EAST);
35 }
36
37 /**
38 * Replies the coordinate in degrees/minutes format
39 * @param pCoordinate The coordinate to convert
40 * @return The coordinate in degrees/minutes format
41 * @since 12537
42 */
43 public static String degreesMinutes(double pCoordinate) {
44
45 double tAbsCoord = Math.abs(pCoordinate);
46 int tDegree = (int) tAbsCoord;
47 double tMinutes = (tAbsCoord - tDegree) * 60;
48
49 String sDegrees = Integer.toString(tDegree);
50 String sMinutes = DM_MINUTE_FORMATTER.format(tMinutes);
51
52 if (sMinutes.equals(DM60)) {
53 sMinutes = DM00;
54 sDegrees = Integer.toString(tDegree+1);
55 }
56
57 return sDegrees + '\u00B0' + sMinutes + '\'';
58 }
59}
Note: See TracBrowser for help on using the repository browser.