From 436630c1e24a96b9929766ee9eeb5861d83ebc32 Mon Sep 17 00:00:00 2001 From: Jan Backhaus Date: Thu, 29 Oct 2020 16:34:09 +0100 Subject: [PATCH] Added support for MGRS string flavoring for better readability ("32U PC 05573 97153" instead of "32UPC0557397153"). Dynamically adjustable accuracy included (also with letter overrolling correction on rounding). --- .../jwetherell/openmap/common/MGRSPoint.java | 95 +++++++++++++++++++ .../src/net/osmand/plus/OsmAndFormatter.java | 2 +- .../activities/NavigatePointFragment.java | 2 +- .../QuickSearchCoordinatesFragment.java | 2 +- .../mapwidgets/MapInfoWidgetsFactory.java | 2 +- 5 files changed, 99 insertions(+), 4 deletions(-) diff --git a/OsmAnd-java/src/main/java/com/jwetherell/openmap/common/MGRSPoint.java b/OsmAnd-java/src/main/java/com/jwetherell/openmap/common/MGRSPoint.java index 8cbb306b7e..a39c5af336 100644 --- a/OsmAnd-java/src/main/java/com/jwetherell/openmap/common/MGRSPoint.java +++ b/OsmAnd-java/src/main/java/com/jwetherell/openmap/common/MGRSPoint.java @@ -14,6 +14,10 @@ package com.jwetherell.openmap.common; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + public class MGRSPoint extends ZonedUTMPoint { /** @@ -633,6 +637,97 @@ public class MGRSPoint extends ZonedUTMPoint { return twoLetter; } + public String toFlavoredString() { + try { + List all = new ArrayList<>(); + for (int i = 0; i <= mgrs.length(); i++) { + if (Character.isAlphabetic(mgrs.charAt(i))){ + all.add(mgrs.substring(0,i+1)); + all.add(mgrs.substring(i+1,i+3)); + String remains = mgrs.substring(i+3); + all.add(remains.substring(0,remains.length()/2)); + all.add(remains.substring(remains.length()/2)); + break; + } + } + StringBuilder os = new StringBuilder(); + for(String part: all){ + if (os.length() > 0) os.append(" "); + os.append(part); + } + return os.toString(); + }catch (Exception e){ + return mgrs; + } + } + + public String toFlavoredString(int accuracy) { + try { + List all = new ArrayList<>(); + for (int i = 0; i <= mgrs.length(); i++) { + if (Character.isAlphabetic(mgrs.charAt(i))){ + all.add(mgrs.substring(0,i+1)); + all.add(mgrs.substring(i+1,i+3)); + String remains = mgrs.substring(i+3); + int easting = Integer.parseInt(remains.substring(0,remains.length()/2)); + int northing = Integer.parseInt(remains.substring(remains.length()/2)); + double resolution = Math.pow(10, getAccuracy() - accuracy); + long roundedEasting = Math.round(easting/resolution); + long roundedNorthing = Math.round(northing/resolution); + int eastShift = 0; + int northShift = 0; + if (roundedEasting == resolution*10){ + roundedEasting = 0L; + eastShift = 1; + } + if (roundedNorthing == resolution*10){ + roundedNorthing = 0L; + northShift = 1; + } + if (eastShift != 0 || northShift != 0){ + all.set(1, shiftChar(all.get(1), eastShift, northShift)); + String zero = ""; + } + + + all.add(String.format("%0" + accuracy + "d", roundedEasting)); + all.add(String.format("%0" + accuracy + "d", roundedNorthing)); + break; + } + } + StringBuilder os = new StringBuilder(); + for(String part: all){ + if (os.length() > 0) os.append(" "); + os.append(part); + } + return os.toString(); + }catch (Exception e){ + return toFlavoredString(); + } + } + + private static String shiftChar(String chars, int east, int north){ + ArrayList keys = new ArrayList( + Arrays.asList('A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z')); + StringBuilder s = new StringBuilder(); + if (east != 0){ + int idx = keys.indexOf(chars.charAt(0)); + idx += east; + if (idx >= keys.size()) idx -= keys.size(); + if (idx < 0) idx += keys.size(); + s.append(keys.get(idx)); + }else s.append(chars.charAt(0)); + if (north != 0){ + int idx = keys.indexOf(chars.charAt(1)); + idx += north; + if (idx >= keys.size()) idx -= keys.size(); + if (idx < 0) idx += keys.size(); + s.append(keys.get(idx)); + }else s.append(chars.charAt(1)); + return s.toString(); + } + + /** * {@inheritDoc} */ diff --git a/OsmAnd/src/net/osmand/plus/OsmAndFormatter.java b/OsmAnd/src/net/osmand/plus/OsmAndFormatter.java index 8ec817900b..f9d3315bf5 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndFormatter.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndFormatter.java @@ -522,7 +522,7 @@ public class OsmAndFormatter { result.append(r); } else if (outputFormat == MGRS_FORMAT) { MGRSPoint pnt = new MGRSPoint(new LatLonPoint(lat, lon)); - result.append(pnt.toString()); + result.append(pnt.toFlavoredString(5)); } return result.toString(); } diff --git a/OsmAnd/src/net/osmand/plus/activities/NavigatePointFragment.java b/OsmAnd/src/net/osmand/plus/activities/NavigatePointFragment.java index 1f484e7b22..7b0ca4c17c 100644 --- a/OsmAnd/src/net/osmand/plus/activities/NavigatePointFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/NavigatePointFragment.java @@ -182,7 +182,7 @@ public class NavigatePointFragment extends Fragment implements SearchActivityChi final EditText mgrsEdit = ((EditText) view.findViewById(R.id.MGRSEdit)); MGRSPoint pnt = new MGRSPoint(new LatLonPoint(l.getLatitude(), l.getLongitude())); - mgrsEdit.setText(pnt.toString()); + mgrsEdit.setText(pnt.toFlavoredString(5)); break; } diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchCoordinatesFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchCoordinatesFragment.java index 251a2a4227..409f38b319 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchCoordinatesFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchCoordinatesFragment.java @@ -609,7 +609,7 @@ public class QuickSearchCoordinatesFragment extends DialogFragment implements Os 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.toString()); + mgrsEdit.setText(pnt.toFlavoredString(5)); } else { mgrsEdit.setText(latEdit.getText()); diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapInfoWidgetsFactory.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapInfoWidgetsFactory.java index b89ef5fa50..d8101b487d 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapInfoWidgetsFactory.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapInfoWidgetsFactory.java @@ -1258,7 +1258,7 @@ public class MapInfoWidgetsFactory { AndroidUiHelper.updateVisibility(latitudeIcon, true); latitudeIcon.setImageDrawable(iconsCache.getIcon(nightMode ? R.drawable.widget_coordinates_utm_night : R.drawable.widget_coordinates_utm_day)); MGRSPoint pnt = new MGRSPoint(new LatLonPoint(lat, lon)); - latitudeText.setText(pnt.toString()); + latitudeText.setText(pnt.toFlavoredString(5)); } else if (f == PointDescription.OLC_FORMAT) { AndroidUiHelper.updateVisibility(lonCoordinatesContainer, false); AndroidUiHelper.updateVisibility(coordinatesDivider, false);