use of com.jwetherell.openmap.common.MGRSPoint in project Osmand by osmandapp.
the class NavigatePointFragment method showCurrentFormat.
protected void showCurrentFormat(LatLon l) {
final EditText latEdit = view.findViewById(R.id.LatitudeEdit);
final EditText lonEdit = view.findViewById(R.id.LongitudeEdit);
switch(currentFormat) {
case PointDescription.UTM_FORMAT:
{
view.findViewById(R.id.easting_row).setVisibility(View.VISIBLE);
view.findViewById(R.id.northing_row).setVisibility(View.VISIBLE);
view.findViewById(R.id.zone_row).setVisibility(View.VISIBLE);
view.findViewById(R.id.lat_row).setVisibility(View.GONE);
view.findViewById(R.id.lon_row).setVisibility(View.GONE);
view.findViewById(R.id.mgrs_row).setVisibility(View.GONE);
final EditText northingEdit = view.findViewById(R.id.NorthingEdit);
final EditText eastingEdit = view.findViewById(R.id.EastingEdit);
final EditText zoneEdit = view.findViewById(R.id.ZoneEdit);
UTMPoint pnt = new UTMPoint(new LatLonPoint(l.getLatitude(), l.getLongitude()));
zoneEdit.setText(pnt.zone_number + "" + pnt.zone_letter);
northingEdit.setText(((long) pnt.northing) + "");
eastingEdit.setText(((long) pnt.easting) + "");
break;
}
case PointDescription.MGRS_FORMAT:
{
view.findViewById(R.id.easting_row).setVisibility(View.GONE);
view.findViewById(R.id.northing_row).setVisibility(View.GONE);
view.findViewById(R.id.zone_row).setVisibility(View.GONE);
view.findViewById(R.id.lat_row).setVisibility(View.GONE);
view.findViewById(R.id.lon_row).setVisibility(View.GONE);
view.findViewById(R.id.mgrs_row).setVisibility(View.VISIBLE);
final EditText mgrsEdit = ((EditText) view.findViewById(R.id.MGRSEdit));
MGRSPoint pnt = new MGRSPoint(new LatLonPoint(l.getLatitude(), l.getLongitude()));
mgrsEdit.setText(pnt.toFlavoredString(5));
break;
}
default:
{
view.findViewById(R.id.easting_row).setVisibility(View.GONE);
view.findViewById(R.id.northing_row).setVisibility(View.GONE);
view.findViewById(R.id.zone_row).setVisibility(View.GONE);
view.findViewById(R.id.lat_row).setVisibility(View.VISIBLE);
view.findViewById(R.id.lon_row).setVisibility(View.VISIBLE);
view.findViewById(R.id.mgrs_row).setVisibility(View.GONE);
latEdit.setText(LocationConvert.convert(MapUtils.checkLatitude(l.getLatitude()), currentFormat));
lonEdit.setText(LocationConvert.convert(MapUtils.checkLongitude(l.getLongitude()), currentFormat));
}
}
}
use of com.jwetherell.openmap.common.MGRSPoint in project Osmand by osmandapp.
the class OsmAndFormatter method getFormattedCoordinates.
public static String getFormattedCoordinates(double lat, double lon, int outputFormat) {
StringBuilder result = new StringBuilder();
if (outputFormat == FORMAT_DEGREES_SHORT) {
result.append(formatCoordinate(lat, outputFormat)).append(" ").append(formatCoordinate(lon, outputFormat));
} else if (outputFormat == FORMAT_DEGREES || outputFormat == FORMAT_MINUTES || outputFormat == FORMAT_SECONDS) {
boolean isLeftToRight = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_LTR;
String rtlCoordinates = isLeftToRight ? "" : "\u200f";
String rtlCoordinatesPunctuation = isLeftToRight ? ", " : " ,";
result.append(rtlCoordinates).append(formatCoordinate(lat, outputFormat)).append(rtlCoordinates).append(" ").append(rtlCoordinates).append(lat > 0 ? NORTH : SOUTH).append(rtlCoordinates).append(rtlCoordinatesPunctuation).append(rtlCoordinates).append(formatCoordinate(lon, outputFormat)).append(rtlCoordinates).append(" ").append(rtlCoordinates).append(lon > 0 ? EAST : WEST);
} else if (outputFormat == UTM_FORMAT) {
ZonedUTMPoint pnt = new ZonedUTMPoint(new LatLonPoint(lat, lon));
result.append(pnt.zone_number).append(pnt.zone_letter).append(" ").append((long) pnt.easting).append(" ").append((long) pnt.northing);
} else if (outputFormat == OLC_FORMAT) {
String r;
try {
r = getLocationOlcName(lat, lon);
} catch (RuntimeException e) {
r = "0, 0";
}
result.append(r);
} else if (outputFormat == MGRS_FORMAT) {
MGRSPoint pnt = new MGRSPoint(new LatLonPoint(lat, lon));
try {
result.append(pnt.toFlavoredString(5));
} catch (java.lang.Error e) {
e.printStackTrace();
}
} else if (outputFormat == SWISS_GRID_FORMAT) {
double[] swissGrid = SwissGridApproximation.convertWGS84ToLV03(new LatLon(lat, lon));
DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols(Locale.US);
formatSymbols.setDecimalSeparator('.');
formatSymbols.setGroupingSeparator(' ');
DecimalFormat swissGridFormat = new DecimalFormat("###,###.##", formatSymbols);
result.append(swissGridFormat.format(swissGrid[0]) + ", " + swissGridFormat.format(swissGrid[1]));
} else if (outputFormat == SWISS_GRID_PLUS_FORMAT) {
double[] swissGrid = SwissGridApproximation.convertWGS84ToLV95(new LatLon(lat, lon));
DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols(Locale.US);
formatSymbols.setDecimalSeparator('.');
formatSymbols.setGroupingSeparator(' ');
DecimalFormat swissGridFormat = new DecimalFormat("###,###.##", formatSymbols);
result.append(swissGridFormat.format(swissGrid[0]) + ", " + swissGridFormat.format(swissGrid[1]));
}
return result.toString();
}
use of com.jwetherell.openmap.common.MGRSPoint in project Osmand by osmandapp.
the class QuickSearchCoordinatesFragment method applyFormat.
private boolean applyFormat(int format, boolean forceApply) {
if (currentFormat != format || forceApply) {
int prevFormat = currentFormat;
currentFormat = format;
formatEdit.setText(PointDescription.formatToHumanString(getMyApplication(), currentFormat));
final EditText latEdit = ((EditText) view.findViewById(R.id.latitudeEditText));
final EditText lonEdit = ((EditText) view.findViewById(R.id.longitudeEditText));
updateControlsVisibility();
final LatLon latLon = currentLatLon;
if (currentFormat == PointDescription.UTM_FORMAT) {
final EditText northingEdit = ((EditText) view.findViewById(R.id.northingEditText));
final EditText eastingEdit = ((EditText) view.findViewById(R.id.eastingEditText));
final EditText zoneEdit = ((EditText) view.findViewById(R.id.zoneEditText));
if (latLon != null) {
UTMPoint pnt = new UTMPoint(new LatLonPoint(latLon.getLatitude(), latLon.getLongitude()));
zoneEdit.setText(pnt.zone_number + "" + pnt.zone_letter);
northingEdit.setText(((long) pnt.northing) + "");
eastingEdit.setText(((long) pnt.easting) + "");
} else if (prevFormat == PointDescription.OLC_FORMAT) {
zoneEdit.setText(olcEdit.getText());
northingEdit.setText("");
eastingEdit.setText("");
} else if (prevFormat == PointDescription.MGRS_FORMAT) {
zoneEdit.setText(mgrsEdit.getText());
northingEdit.setText("");
eastingEdit.setText("");
} else if (prevFormat == PointDescription.SWISS_GRID_FORMAT || prevFormat == PointDescription.SWISS_GRID_PLUS_FORMAT) {
zoneEdit.setText("");
northingEdit.setText(swissGridNorthEdit.getText());
eastingEdit.setText(swissGridEastEdit.getText());
} else {
zoneEdit.setText(latEdit.getText());
northingEdit.setText("");
eastingEdit.setText("");
}
} else if (currentFormat == PointDescription.MGRS_FORMAT) {
final EditText mgrsEdit = ((EditText) view.findViewById(R.id.mgrsEditText));
if (latLon != null) {
MGRSPoint pnt = new MGRSPoint(new LatLonPoint(latLon.getLatitude(), latLon.getLongitude()));
mgrsEdit.setText(pnt.toFlavoredString(5));
} else if (prevFormat == PointDescription.UTM_FORMAT) {
mgrsEdit.setText(zoneEdit.getText());
} else if (prevFormat == PointDescription.OLC_FORMAT) {
mgrsEdit.setText(olcEdit.getText());
} else if (prevFormat == PointDescription.SWISS_GRID_FORMAT || prevFormat == PointDescription.SWISS_GRID_PLUS_FORMAT) {
mgrsEdit.setText(swissGridEastEdit.getText());
} else {
mgrsEdit.setText(latEdit.getText());
}
} else if (currentFormat == PointDescription.OLC_FORMAT) {
if (latLon != null) {
String olc = OpenLocationCode.encode(latLon.getLatitude(), latLon.getLongitude());
olcEdit.setText(olc);
} else if (prevFormat == PointDescription.UTM_FORMAT) {
olcEdit.setText(zoneEdit.getText());
} else if (prevFormat == PointDescription.MGRS_FORMAT) {
olcEdit.setText(mgrsEdit.getText());
} else if (prevFormat == PointDescription.SWISS_GRID_FORMAT || prevFormat == PointDescription.SWISS_GRID_PLUS_FORMAT) {
olcEdit.setText(swissGridEastEdit.getText());
} else {
olcEdit.setText(latEdit.getText());
}
olcInfo.setText(provideOlcInfo(olcEdit.getText().toString()));
} else if (currentFormat == PointDescription.SWISS_GRID_FORMAT || currentFormat == PointDescription.SWISS_GRID_PLUS_FORMAT) {
if (latLon != null) {
double[] swissGrid;
if (currentFormat == PointDescription.SWISS_GRID_FORMAT) {
swissGrid = SwissGridApproximation.convertWGS84ToLV03(latLon);
} else {
swissGrid = SwissGridApproximation.convertWGS84ToLV95(latLon);
}
DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols(Locale.US);
formatSymbols.setDecimalSeparator('.');
formatSymbols.setGroupingSeparator(' ');
DecimalFormat swissGridFormat = new DecimalFormat("###,###.##", formatSymbols);
swissGridEastEdit.setText(swissGridFormat.format(swissGrid[0]));
swissGridNorthEdit.setText(swissGridFormat.format(swissGrid[1]));
} else if (prevFormat == PointDescription.UTM_FORMAT) {
swissGridEastEdit.setText(zoneEdit.getText());
swissGridNorthEdit.setText("");
} else if (prevFormat == PointDescription.MGRS_FORMAT) {
swissGridEastEdit.setText(mgrsEdit.getText());
swissGridNorthEdit.setText("");
} else if (prevFormat == PointDescription.OLC_FORMAT) {
swissGridEastEdit.setText(olcEdit.getText());
swissGridNorthEdit.setText("");
} else if (prevFormat != PointDescription.SWISS_GRID_PLUS_FORMAT) {
swissGridEastEdit.setText(latEdit.getText());
swissGridNorthEdit.setText("");
}
} else {
setInputTypeDependingOnFormat(new EditText[] { latEdit, lonEdit });
if (latLon != null) {
latEdit.setText(LocationConvert.convert(MapUtils.checkLatitude(latLon.getLatitude()), currentFormat));
lonEdit.setText(LocationConvert.convert(MapUtils.checkLongitude(latLon.getLongitude()), currentFormat));
} else if (prevFormat == PointDescription.UTM_FORMAT) {
latEdit.setText(zoneEdit.getText());
lonEdit.setText("");
} else if (prevFormat == PointDescription.MGRS_FORMAT) {
latEdit.setText(mgrsEdit.getText());
lonEdit.setText("");
} else if (prevFormat == PointDescription.OLC_FORMAT) {
latEdit.setText(olcEdit.getText());
lonEdit.setText("");
} else if (prevFormat == PointDescription.SWISS_GRID_FORMAT || prevFormat == PointDescription.SWISS_GRID_PLUS_FORMAT) {
latEdit.setText(swissGridEastEdit.getText());
lonEdit.setText(swissGridNorthEdit.getText());
}
}
return latLon != null;
} else {
return false;
}
}
use of com.jwetherell.openmap.common.MGRSPoint in project Osmand by osmandapp.
the class QuickSearchCoordinatesFragment method parseLocation.
private void parseLocation() {
LatLon loc;
LatLon additionalLoc = null;
try {
if (currentFormat == LocationConvert.UTM_FORMAT) {
double northing = Double.parseDouble(northingEdit.getText().toString());
double easting = Double.parseDouble(eastingEdit.getText().toString());
String zone = zoneEdit.getText().toString();
int zoneNumber = Integer.parseInt(zone.substring(0, zone.length() - 1));
char zoneLetter = zone.charAt(zone.length() - 1);
Pair<LatLon, LatLon> locations = parseUtmLocations(northing, easting, zoneNumber, zoneLetter);
loc = locations.first;
if (loc == null || !loc.equals(locations.second)) {
additionalLoc = locations.second;
}
} else if (currentFormat == LocationConvert.MGRS_FORMAT) {
String mgrs = (mgrsEdit.getText().toString());
MGRSPoint upoint = new MGRSPoint(mgrs);
LatLonPoint ll = upoint.toLatLonPoint();
loc = new LatLon(ll.getLatitude(), ll.getLongitude());
} else if (currentFormat == LocationConvert.OLC_FORMAT) {
String olcText = olcEdit.getText().toString();
olcInfo.setText(provideOlcInfo(olcText));
loc = parseOlcCode(olcText);
} else if (currentFormat == LocationConvert.SWISS_GRID_FORMAT) {
double eastCoordinate = Double.parseDouble(swissGridEastEdit.getText().toString().replaceAll("\\s+", ""));
double northCoordinate = Double.parseDouble(swissGridNorthEdit.getText().toString().replaceAll("\\s+", ""));
loc = SwissGridApproximation.convertLV03ToWGS84(eastCoordinate, northCoordinate);
} else if (currentFormat == LocationConvert.SWISS_GRID_PLUS_FORMAT) {
double eastCoordinate = Double.parseDouble(swissGridEastEdit.getText().toString().replaceAll("\\s+", ""));
double northCoordinate = Double.parseDouble(swissGridNorthEdit.getText().toString().replaceAll("\\s+", ""));
loc = SwissGridApproximation.convertLV95ToWGS84(eastCoordinate, northCoordinate);
} else {
double lat = LocationConvert.convert(latEdit.getText().toString(), true);
double lon = LocationConvert.convert(lonEdit.getText().toString(), true);
loc = new LatLon(lat, lon);
}
currentLatLon = loc;
additionalUtmLatLon = additionalLoc;
} catch (Exception e) {
currentLatLon = null;
additionalUtmLatLon = null;
}
updateLocationCell(coordsView, currentLatLon, additionalUtmLatLon != null);
updateLocationCell(additionalCoordsView, additionalUtmLatLon, false);
updateErrorVisibility();
}
use of com.jwetherell.openmap.common.MGRSPoint in project Osmand by osmandapp.
the class LocationParser method parseLocation.
public static LatLon parseLocation(String locPhrase) {
locPhrase = locPhrase.trim();
boolean valid = isValidLocPhrase(locPhrase);
if (!valid) {
String[] split = locPhrase.split(" ");
if (split.length == 4 && split[1].contains(".") && split[3].contains(".")) {
locPhrase = split[1] + " " + split[3];
valid = isValidLocPhrase(locPhrase);
}
}
if (!valid) {
return null;
}
List<Double> d = new ArrayList<>();
List<Object> all = new ArrayList<>();
List<String> strings = new ArrayList<>();
splitObjects(locPhrase, d, all, strings);
if (d.size() == 0) {
return null;
}
// detect UTM
if (all.size() == 4 && d.size() == 3 && all.get(1) instanceof String && ((String) all.get(1)).length() == 1) {
char ch = all.get(1).toString().charAt(0);
if (Character.isLetter(ch)) {
UTMPoint upoint = new UTMPoint(d.get(2), d.get(1), d.get(0).intValue(), ch);
LatLonPoint ll = upoint.toLatLonPoint();
return validateAndCreateLatLon(ll.getLatitude(), ll.getLongitude());
}
}
if (all.size() == 3 && d.size() == 2 && all.get(1) instanceof String && ((String) all.get(1)).length() == 1) {
char ch = all.get(1).toString().charAt(0);
String combined = strings.get(2);
if (Character.isLetter(ch)) {
try {
String east = combined.substring(0, combined.length() / 2);
String north = combined.substring(combined.length() / 2, combined.length());
UTMPoint upoint = new UTMPoint(Double.parseDouble(north), Double.parseDouble(east), d.get(0).intValue(), ch);
LatLonPoint ll = upoint.toLatLonPoint();
return validateAndCreateLatLon(ll.getLatitude(), ll.getLongitude());
} catch (NumberFormatException e) {
}
}
}
// detect MGRS
if (all.size() >= 3 && (d.size() == 2 || d.size() == 3) && all.get(1) instanceof String) {
try {
MGRSPoint mgrsPoint = new MGRSPoint(locPhrase);
LatLonPoint ll = mgrsPoint.toLatLonPoint();
return validateAndCreateLatLon(ll.getLatitude(), ll.getLongitude());
} catch (NumberFormatException e) {
// do nothing
}
}
// try to find split lat/lon position
int jointNumbers = 0;
int lastJoin = 0;
int degSplit = -1;
// 0 - degree, 1 - minutes, 2 - seconds
int degType = -1;
boolean finishDegSplit = false;
int northSplit = -1;
int eastSplit = -1;
for (int i = 1; i < all.size(); i++) {
if (all.get(i - 1) instanceof Double && all.get(i) instanceof Double) {
jointNumbers++;
lastJoin = i;
}
if (all.get(i).equals("n") || all.get(i).equals("s") || all.get(i).equals("N") || all.get(i).equals("S")) {
northSplit = i + 1;
}
if (all.get(i).equals("e") || all.get(i).equals("w") || all.get(i).equals("E") || all.get(i).equals("W")) {
eastSplit = i;
}
int dg = -1;
if (all.get(i).equals("°")) {
dg = 0;
} else if (all.get(i).equals("\'") || all.get(i).equals("′")) {
dg = 1;
} else if (all.get(i).equals("″") || all.get(i).equals("\"")) {
dg = 2;
}
if (dg != -1) {
if (!finishDegSplit) {
if (degType < dg) {
degSplit = i + 1;
degType = dg;
} else {
finishDegSplit = true;
degType = dg;
}
} else {
if (degType < dg) {
degType = dg;
} else {
// reject delimiter
degSplit = -1;
}
}
}
}
int split = -1;
if (jointNumbers == 1) {
split = lastJoin;
}
if (northSplit != -1 && northSplit < all.size() - 1) {
split = northSplit;
} else if (eastSplit != -1 && eastSplit < all.size() - 1) {
split = eastSplit;
} else if (degSplit != -1 && degSplit < all.size() - 1) {
split = degSplit;
}
if (split != -1) {
double lat = parse1Coordinate(all, 0, split);
double lon = parse1Coordinate(all, split, all.size());
return validateAndCreateLatLon(lat, lon);
}
if (d.size() == 2) {
return validateAndCreateLatLon(d.get(0), d.get(1));
}
// simple url case
if (locPhrase.contains("://")) {
double lat = 0;
double lon = 0;
boolean only2decimals = true;
for (int i = 0; i < d.size(); i++) {
if (d.get(i).doubleValue() != d.get(i).intValue()) {
if (lat == 0) {
lat = d.get(i);
} else if (lon == 0) {
lon = d.get(i);
} else {
only2decimals = false;
}
}
}
if (lat != 0 && lon != 0 && only2decimals) {
return validateAndCreateLatLon(lat, lon);
}
}
// split by equal number of digits
if (d.size() > 2 && d.size() % 2 == 0) {
int ind = d.size() / 2 + 1;
int splitEq = -1;
for (int i = 0; i < all.size(); i++) {
if (all.get(i) instanceof Double) {
ind--;
}
if (ind == 0) {
splitEq = i;
break;
}
}
if (splitEq != -1) {
double lat = parse1Coordinate(all, 0, splitEq);
double lon = parse1Coordinate(all, splitEq, all.size());
return validateAndCreateLatLon(lat, lon);
}
}
return null;
}
Aggregations