Improve coordinates formatting

This commit is contained in:
Chumva 2019-05-15 18:16:59 +03:00
parent 3b408a1fb0
commit 698c1bb6b0

View file

@ -16,6 +16,10 @@ public class LocationConvert {
public static final int UTM_FORMAT = 3; public static final int UTM_FORMAT = 3;
public static final int OLC_FORMAT = 4; public static final int OLC_FORMAT = 4;
private static final char DELIM = ':'; private static final char DELIM = ':';
private static final char DELIMITER_DEGREES = '°';
private static final char DELIMITER_MINUTES = '';
private static final char DELIMITER_SECONDS = '″';
private static final char DELIMITER_SPACE = ' ';
@ -133,8 +137,12 @@ public class LocationConvert {
} }
DecimalFormat df = new DecimalFormat("##0.00000", new DecimalFormatSymbols(Locale.US)); //$NON-NLS-1$ DecimalFormat df = new DecimalFormat("##0.00000", new DecimalFormatSymbols(Locale.US)); //$NON-NLS-1$
coordinate = convertMinutesAndSeconds(coordinate, outputType, sb); if (outputType == FORMAT_MINUTES || outputType == FORMAT_SECONDS) {
coordinate = formatCoordinate(coordinate, sb, DELIM);
if (outputType == FORMAT_SECONDS) {
coordinate = formatCoordinate(coordinate, sb, DELIM);
}
}
sb.append(df.format(coordinate)); sb.append(df.format(coordinate));
return sb.toString(); return sb.toString();
} }
@ -159,11 +167,7 @@ public class LocationConvert {
latitude = -latitude; latitude = -latitude;
} }
DecimalFormat df = new DecimalFormat("##0.00000", new DecimalFormatSymbols(Locale.US)); return formatDegrees(latitude, outputType, sb);
latitude = convertMinutesAndSeconds(latitude, outputType, sb);
sb.append(df.format(latitude));
return sb.toString();
} }
public static String convertLongitude(double longitude, int outputType, boolean addCardinalDirection) { public static String convertLongitude(double longitude, int outputType, boolean addCardinalDirection) {
@ -186,28 +190,34 @@ public class LocationConvert {
longitude = -longitude; longitude = -longitude;
} }
DecimalFormat df = new DecimalFormat("##0.00000", new DecimalFormatSymbols(Locale.US)); return formatDegrees(longitude, outputType, sb);
longitude = convertMinutesAndSeconds(longitude, outputType, sb);
sb.append(df.format(longitude));
return sb.toString();
} }
private static double convertMinutesAndSeconds(double coordinate, int outputType, StringBuilder sb) { private static double formatCoordinate(double coordinate, StringBuilder sb, char delimiter) {
if (outputType == FORMAT_MINUTES || outputType == FORMAT_SECONDS) { int deg = (int) Math.floor(coordinate);
int degrees = (int) Math.floor(coordinate); sb.append(deg);
sb.append(degrees); sb.append(delimiter);
sb.append(DELIM); coordinate -= deg;
coordinate -= degrees; coordinate *= 60.0;
coordinate *= 60.0;
if (outputType == FORMAT_SECONDS) {
int minutes = (int) Math.floor(coordinate);
sb.append(minutes);
sb.append(DELIM);
coordinate -= minutes;
coordinate *= 60.0;
}
}
return coordinate; return coordinate;
} }
}
private static String formatDegrees(double coordinate, int outputType, StringBuilder sb) {
if (outputType == FORMAT_DEGREES) {
sb.append(new DecimalFormat("##0.0000", new DecimalFormatSymbols(Locale.US)).format(coordinate));
sb.append(DELIMITER_DEGREES);
} else if (outputType == FORMAT_MINUTES) {
coordinate = formatCoordinate(coordinate, sb, DELIMITER_DEGREES);
sb.append(DELIMITER_SPACE);
sb.append(new DecimalFormat("##0.00", new DecimalFormatSymbols(Locale.US)).format(coordinate));
sb.append(DELIMITER_MINUTES);
} else if (outputType == FORMAT_SECONDS) {
coordinate = formatCoordinate(coordinate, sb, DELIMITER_DEGREES);
sb.append(DELIMITER_SPACE);
coordinate = formatCoordinate(coordinate, sb, DELIMITER_MINUTES);
sb.append(DELIMITER_SPACE);
formatCoordinate(coordinate, sb, DELIMITER_SECONDS);
}
return sb.toString();
}
}