A Simple Indonesian Geographic Coordinate Conversion Between Degrees to Degree-Minute-Second (DMS)
Recently i have a simple GIS project which force me to display an Indonesian DMS coordinate format, instead of the usual decimal format. This is a simple utility class i used to do convertion.
import java.text.DecimalFormat; public class DegreeConverterTest { public static final String LONGITUDE = "longitude"; public static final String LATITUDE = "latitude"; public DegreeConverterTest(String testName) { } public static void main(String a[]) { System.out.println(degree2DMS(-8.857, LATITUDE)); System.out.println(degree2DMS(10.645, LATITUDE)); System.out.println(degree2DMS(135.696, LONGITUDE)); } public static String degree2DMS(double coordinate, String mode) { String dms = ""; char degree = '\u00B0'; int degLong, minLongA; double minLong, secLong; DecimalFormat df = new DecimalFormat("#,###,####0.0000"); if (LONGITUDE.equals(mode)) { // minus is BB (Bujur Barat or west longitude) // plus is BT (Bujur Timur of east longitude) degLong = (int) coordinate; minLong = (coordinate % 1) * 60; minLongA = (int) minLong; secLong = (minLong % 1) * 60; String secLongAs = String.valueOf(df.format(secLong)); dms = degLong + degree + " " + minLongA + "' " + secLongAs + "\" BT"; } else if (LATITUDE.equals(mode)) { // minus is LS (Lintang Selatan or south latitude) // plus is LU (Lintang Utara or north latitude) boolean isMinus = true; String minusDegree = "LS"; degLong = (int) coordinate; String degLongS = String.valueOf(degLong); if (degLongS.contains("-")) { degLongS = degLongS.replace("-", ""); } else { isMinus = false; minusDegree = "LU"; } minLong = (coordinate % 1) * 60; minLongA = (int) minLong; String minLongAs = String.valueOf(minLongA); if (isMinus) { minLongAs = minLongAs.replace("-", ""); } secLong = (minLong % 1) * 60; String secLongAs = String.valueOf(df.format(secLong)); if (isMinus) { secLongAs = secLongAs.replace("-", ""); } dms = degLongS + degree + " " + minLongAs + "' " + secLongAs + "\" " + minusDegree; } return dms; } }
And this is the output i got on my netbeans’ console
8° 51' 25.2000" LS 10° 38' 42.0000" LU 311 41' 45.6000" BT