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 OLC_FORMAT = 4;
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$
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));
return sb.toString();
}
@ -159,11 +167,7 @@ public class LocationConvert {
latitude = -latitude;
}
DecimalFormat df = new DecimalFormat("##0.00000", new DecimalFormatSymbols(Locale.US));
latitude = convertMinutesAndSeconds(latitude, outputType, sb);
sb.append(df.format(latitude));
return sb.toString();
return formatDegrees(latitude, outputType, sb);
}
public static String convertLongitude(double longitude, int outputType, boolean addCardinalDirection) {
@ -186,28 +190,34 @@ public class LocationConvert {
longitude = -longitude;
}
DecimalFormat df = new DecimalFormat("##0.00000", new DecimalFormatSymbols(Locale.US));
longitude = convertMinutesAndSeconds(longitude, outputType, sb);
sb.append(df.format(longitude));
return sb.toString();
return formatDegrees(longitude, outputType, sb);
}
private static double convertMinutesAndSeconds(double coordinate, int outputType, StringBuilder sb) {
if (outputType == FORMAT_MINUTES || outputType == FORMAT_SECONDS) {
int degrees = (int) Math.floor(coordinate);
sb.append(degrees);
sb.append(DELIM);
coordinate -= degrees;
coordinate *= 60.0;
if (outputType == FORMAT_SECONDS) {
int minutes = (int) Math.floor(coordinate);
sb.append(minutes);
sb.append(DELIM);
coordinate -= minutes;
coordinate *= 60.0;
}
}
private static double formatCoordinate(double coordinate, StringBuilder sb, char delimiter) {
int deg = (int) Math.floor(coordinate);
sb.append(deg);
sb.append(delimiter);
coordinate -= deg;
coordinate *= 60.0;
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();
}
}