1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.coor.conversion;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.text.DecimalFormat;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.coor.ILatLon;
|
---|
9 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Coordinate format that converts a coordinate to degrees, minutes and seconds.
|
---|
13 | * @since 12735
|
---|
14 | */
|
---|
15 | public class DMSCoordinateFormat extends AbstractCoordinateFormat {
|
---|
16 |
|
---|
17 | private static final DecimalFormat DMS_MINUTE_FORMATTER = newUnlocalizedDecimalFormat("00");
|
---|
18 | private static final DecimalFormat DMS_SECOND_FORMATTER = newUnlocalizedDecimalFormat(
|
---|
19 | Config.getPref() == null ? "00.0" : Config.getPref().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 | /**
|
---|
24 | * The unique instance.
|
---|
25 | */
|
---|
26 | public static final DMSCoordinateFormat INSTANCE = new DMSCoordinateFormat();
|
---|
27 |
|
---|
28 | protected DMSCoordinateFormat() {
|
---|
29 | super("DEGREES_MINUTES_SECONDS", tr("deg\u00B0 min'' sec\""));
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public String latToString(ILatLon ll) {
|
---|
34 | return degreesMinutesSeconds(ll.lat()) + ((ll.lat() < 0) ? SOUTH : NORTH);
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public String lonToString(ILatLon ll) {
|
---|
39 | return degreesMinutesSeconds(ll.lon()) + ((ll.lon() < 0) ? WEST : EAST);
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Replies the coordinate in degrees/minutes/seconds format
|
---|
44 | * @param pCoordinate The coordinate to convert
|
---|
45 | * @return The coordinate in degrees/minutes/seconds format
|
---|
46 | * @since 12561
|
---|
47 | */
|
---|
48 | public static String degreesMinutesSeconds(double pCoordinate) {
|
---|
49 |
|
---|
50 | double tAbsCoord = Math.abs(pCoordinate);
|
---|
51 | int tDegree = (int) tAbsCoord;
|
---|
52 | double tTmpMinutes = (tAbsCoord - tDegree) * 60;
|
---|
53 | int tMinutes = (int) tTmpMinutes;
|
---|
54 | double tSeconds = (tTmpMinutes - tMinutes) * 60;
|
---|
55 |
|
---|
56 | String sDegrees = Integer.toString(tDegree);
|
---|
57 | String sMinutes = DMS_MINUTE_FORMATTER.format(tMinutes);
|
---|
58 | String sSeconds = DMS_SECOND_FORMATTER.format(tSeconds);
|
---|
59 |
|
---|
60 | if (DMS60.equals(sSeconds)) {
|
---|
61 | sSeconds = DMS00;
|
---|
62 | sMinutes = DMS_MINUTE_FORMATTER.format(tMinutes+1L);
|
---|
63 | }
|
---|
64 | if ("60".equals(sMinutes)) {
|
---|
65 | sMinutes = "00";
|
---|
66 | sDegrees = Integer.toString(tDegree+1);
|
---|
67 | }
|
---|
68 |
|
---|
69 | return sDegrees + '\u00B0' + sMinutes + '\'' + sSeconds + '\"';
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|