source: josm/trunk/src/org/openstreetmap/josm/data/coor/conversion/DMSCoordinateFormat.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: 2.4 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, minutes and seconds.
13 * @since 12735
14 */
15public class DMSCoordinateFormat extends AbstractCoordinateFormat {
16
17 private static final DecimalFormat DMS_MINUTE_FORMATTER = new DecimalFormat("00");
18 private static final DecimalFormat DMS_SECOND_FORMATTER = new DecimalFormat(
19 Main.pref == null ? "00.0" : Main.pref.get("latlon.dms.decimal-format", "00.0"));
20 private static final String DMS60 = DMS_SECOND_FORMATTER.format(60.0);
21 private static final String DMS00 = DMS_SECOND_FORMATTER.format(0.0);
22
23 public static DMSCoordinateFormat INSTANCE = new DMSCoordinateFormat();
24
25 private DMSCoordinateFormat() {
26 super("DEGREES_MINUTES_SECONDS", tr("deg\u00B0 min'' sec\""));
27 }
28
29 @Override
30 public String latToString(ILatLon ll) {
31 return degreesMinutesSeconds(ll.lat()) + ((ll.lat() < 0) ? SOUTH : NORTH);
32 }
33
34 @Override
35 public String lonToString(ILatLon ll) {
36 return degreesMinutesSeconds(ll.lon()) + ((ll.lon() < 0) ? WEST : EAST);
37 }
38
39 /**
40 * Replies the coordinate in degrees/minutes/seconds format
41 * @param pCoordinate The coordinate to convert
42 * @return The coordinate in degrees/minutes/seconds format
43 * @since 12561
44 */
45 public static String degreesMinutesSeconds(double pCoordinate) {
46
47 double tAbsCoord = Math.abs(pCoordinate);
48 int tDegree = (int) tAbsCoord;
49 double tTmpMinutes = (tAbsCoord - tDegree) * 60;
50 int tMinutes = (int) tTmpMinutes;
51 double tSeconds = (tTmpMinutes - tMinutes) * 60;
52
53 String sDegrees = Integer.toString(tDegree);
54 String sMinutes = DMS_MINUTE_FORMATTER.format(tMinutes);
55 String sSeconds = DMS_SECOND_FORMATTER.format(tSeconds);
56
57 if (DMS60.equals(sSeconds)) {
58 sSeconds = DMS00;
59 sMinutes = DMS_MINUTE_FORMATTER.format(tMinutes+1L);
60 }
61 if ("60".equals(sMinutes)) {
62 sMinutes = "00";
63 sDegrees = Integer.toString(tDegree+1);
64 }
65
66 return sDegrees + '\u00B0' + sMinutes + '\'' + sSeconds + '\"';
67 }
68
69}
Note: See TracBrowser for help on using the repository browser.