Skip to content

Commit

Permalink
fix #129 (mgrs space separation) and improve UI layout
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrupczak3 committed Apr 26, 2024
1 parent 8cef83a commit d88b455
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 16 deletions.
55 changes: 55 additions & 0 deletions app/src/main/java/com/openathena/CoordTranslator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.openathena;

// Libraries from the U.S. National Geospatial Intelligence Agency https://www.nga.mil
import android.util.Log;

import mil.nga.mgrs.grid.GridType;
import mil.nga.tiff.util.TiffException;
import mil.nga.mgrs.*;
Expand All @@ -11,6 +13,8 @@ public class CoordTranslator {
super();
}

private static final String TAG = "CoordTranslator";

public static String toMGRS1m(double latitude, double longitude) {
return toMGRS(latitude, longitude, GridType.METER);
}
Expand All @@ -25,10 +29,61 @@ public static String toMGRS100m(double latitude, double longitude) {

private static String toMGRS(double latitude, double longitude, GridType gridType) {
MGRS mgrsObj = MGRS.from(new Point(longitude, latitude));
// Log.d(TAG, "grid zone: " + mgrsObj.getGridZone().getName());
// Log.d(TAG, "square identifier: " + mgrsObj.getColumn() + mgrsObj.getRow());
// Log.d(TAG, "easting and northing: " + mgrsObj.getEastingAndNorthing(gridType));
String mgrsText = mgrsObj.coordinate(gridType);
return mgrsText;
}

public static String toMGRS1m_Space_Separated(double latitude, double longitude) {
return toMGRS_Space_Separated(latitude, longitude, GridType.METER);
}

public static String toMGRS10m_Space_Separated(double latitude, double longitude) {
return toMGRS_Space_Separated(latitude, longitude, GridType.TEN_METER);
}

public static String toMGRS100m_Space_Separated(double latitude, double longitude) {
return toMGRS_Space_Separated(latitude, longitude, GridType.HUNDRED_METER);
}

private static String toMGRS_Space_Separated(double latitude, double longitude, GridType gridType) {
MGRS mgrsObj = MGRS.from(new Point(longitude, latitude));
String gridZone = mgrsObj.getGridZone().getName();
String squareIdentifier = "" + mgrsObj.getColumn() + mgrsObj.getRow();
String eastingAndNorthing = mgrsObj.getEastingAndNorthing(gridType);
String easting = ""; String northing = "";
switch (gridType) {
case METER:
easting = eastingAndNorthing.substring(0,5);
northing = eastingAndNorthing.substring(5);
// Log.d(TAG, "square size: METER");
// Log.d(TAG, "easting: " + easting);
// Log.d(TAG, "northing: " + northing);
break;
case TEN_METER:
easting = eastingAndNorthing.substring(0,4);
northing = eastingAndNorthing.substring(4);
// Log.d(TAG, "square size: TEN_METER");
// Log.d(TAG, "easting: " + easting);
// Log.d(TAG, "northing: " + northing);
break;
case HUNDRED_METER:
easting = eastingAndNorthing.substring(0,3);
northing = eastingAndNorthing.substring(3);
// Log.d(TAG, "square size: TEN_METER");
// Log.d(TAG, "easting: " + easting);
// Log.d(TAG, "northing: " + northing);
break;
// default should never happen
default:
throw new RuntimeException("gridType was an invalid value!");
}
String[] components = new String[] {gridZone, squareIdentifier, easting, northing};
return String.join(" ", components);
}

public static double toCK42Lat(double WGS84_latitude, double WGS84_longitude, double WGS84_altitude_meters) {
return WGS84_CK42_Geodetic_Translator.WGS84_CK42_Lat(WGS84_latitude, WGS84_longitude, WGS84_altitude_meters);
}
Expand Down
40 changes: 31 additions & 9 deletions app/src/main/java/com/openathena/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Handler;
Expand Down Expand Up @@ -707,7 +708,7 @@ public void calculateImage(View view, boolean shouldISendCoT)

altitude = Math.round(result[3]);
if (!outputModeIsSlavic()) {
attribs += getString(R.string.target_found_at_msg) + ": " + roundDouble(latitude) + "," + roundDouble(longitude) + " Alt (hae): " + Math.round(altitude * (isUnitFoot() ? FEET_PER_METER : 1.0d)) + " " + (isUnitFoot() ? "ft.":"m") + "\n";
attribs += getString(R.string.target_found_at_msg) + ": " + roundDouble(latitude) + "," + roundDouble(longitude) + "\nAlt (hae): " + Math.round(altitude * (isUnitFoot() ? FEET_PER_METER : 1.0d)) + " " + (isUnitFoot() ? "ft.":"m") + "\n";
} else {
attribs += getString(R.string.target_found_at_msg) + " (WGS84): " + roundDouble(latitude) + "," + roundDouble(longitude) + " Alt (hae): " + altitude + " " + "m" + "\n";
attribs += getString(R.string.target_found_at_msg) + " (CK-42): " + roundDouble(latCK42) + "," + roundDouble(lonCK42) + " Alt (hae): " + altCK42 + " " + "m" + "\n";
Expand Down Expand Up @@ -746,27 +747,33 @@ public void calculateImage(View view, boolean shouldISendCoT)
textView.append(Html.fromHtml(attribs, 0, null, null));
// Obtain NATO MGRS from mil.nga.mgrs library
String mgrs1m = CoordTranslator.toMGRS1m(latitude, longitude);
String mgrs10m = CoordTranslator.toMGRS10m(latitude, longitude);
String mgrs100m = CoordTranslator.toMGRS100m(latitude, longitude);
Log.d(TAG, "mgrs1m: " + mgrs1m);
// String mgrs10m = CoordTranslator.toMGRS10m(latitude, longitude);
// String mgrs100m = CoordTranslator.toMGRS100m(latitude, longitude);
String mgrs1m_space_separated = CoordTranslator.toMGRS1m_Space_Separated(latitude, longitude);
String mgrs10m_space_separated = CoordTranslator.toMGRS10m_Space_Separated(latitude, longitude);
String mgrs100m_space_separated = CoordTranslator.toMGRS100m_Space_Separated(latitude, longitude);
String targetCoordString;
if (!outputModeIsSlavic()) {
// open link portion of href tag
targetCoordString = "<a href=\"https://maps.google.com/?q=";
if (outputModeIsMGRS()) {
targetCoordString += mgrs1m; // use MGRS 1m for maps link, even if on 10m or 100m mode
} else {
targetCoordString += roundDouble(latitude) + "," + roundDouble(longitude); // otherwise just use normal WGS84
}
targetCoordString += "\">"; // close start of href tag
targetCoordString += "\">"; // close link portion of href tag
// start building display text portion of href tag
if (outputModeIsMGRS()) {
switch(outputMode) {
case MGRS1m:
targetCoordString += mgrs1m;
targetCoordString += mgrs1m_space_separated;
break;
case MGRS10m:
targetCoordString += mgrs10m;
targetCoordString += mgrs10m_space_separated;
break;
case MGRS100m:
targetCoordString += mgrs100m;
targetCoordString += mgrs100m_space_separated;
break;
default:
throw new RuntimeException("Program entered an inoperable state due to outputMode"); // this shouldn't ever happen
Expand All @@ -776,6 +783,10 @@ public void calculateImage(View view, boolean shouldISendCoT)
}
targetCoordString += "</a> "; // end href tag

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
targetCoordString += "<br>";
}

// convert from WGS84 height above ellipsoid to EGM96 above mean sea level (much more commonly used)
double mslAlt = altitudeDouble + offsetAdapter.getEGM96OffsetAtLatLon(latitude, longitude);
// convert from meters to feet if user setting indicates to do so
Expand All @@ -785,7 +796,13 @@ public void calculateImage(View view, boolean shouldISendCoT)
targetCoordString += getString(R.string.altitude_label_short) + " " + altEGM96 + " " + (isUnitFoot() ? "ft.":"m");
} else { // to avoid confusion with WGS84, no Google Maps link is provided when outputModeIsSlavic()
if (outputMode == outputModes.CK42Geodetic) {
targetCoordString = "(CK-42) " + roundDouble(latCK42) + ", " + roundDouble(lonCK42) + " Alt: " + altCK42 + "m" + "<br>";
targetCoordString = "(CK-42) " + roundDouble(latCK42) + ", " + roundDouble(lonCK42);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
targetCoordString += "<br>";
} else {
targetCoordString += " ";
}
targetCoordString += "Alt: " + altCK42 + "m";
} else if (outputMode == outputModes.CK42GaussKrüger) {
String northing_string = makeGKHumanReadable(GK_northing);
String easting_string = makeGKHumanReadable(GK_easting);
Expand Down Expand Up @@ -873,8 +890,13 @@ public void displayAutelAlert() {
public void copyTargetCoordText(View view) {
if (isTargetCoordDisplayed) {
String text = textViewTargetCoord.getText().toString();
Log.d(TAG, "clipboard text: " + text);
text = text.replaceAll("<[^>]*>", ""); // remove HTML link tag(s)

// don't remove newline characters for copy/paste text if output mode is CK42 GK gridref
if (outputMode != outputModes.CK42GaussKrüger) {
text = text.replaceAll("<br>", ""); // remove HTML <br> newlines
text = text.replaceAll("(\r\n|\n)", ""); // remove string literal newlines
}
// Copy the text to the clipboard
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Text", text);
Expand Down
13 changes: 7 additions & 6 deletions app/src/main/res/layout-land/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@

<ScrollView
android:id="@+id/scrollView2"
android:layout_width="243dp"
android:layout_height="234dp"
android:layout_marginStart="16dp"
android:layout_width="300sp"
android:layout_height="0dp"
android:layout_marginStart="12dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="128dp"
android:layout_marginBottom="23dp"
android:layout_marginBottom="4dp"
android:maxHeight="480dp"

android:minHeight="48dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toTopOf="@+id/progressBar"
Expand All @@ -88,7 +89,7 @@
android:text="@string/openathena_for_android"
android:textColor="?attr/actionMenuTextColor"
android:textIsSelectable="true"
android:textSize="18dp" />
android:textSize="18sp" />
</ScrollView>

<Button
Expand All @@ -107,7 +108,7 @@

<TextView
android:id="@+id/textViewTargetCoord"
android:layout_width="243dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<string name="CallToActionSnippet"><![CDATA[View the project on GitHub</a><br>]]></string>
<string name="bad_altitude_data_error_msg">ERROR: bad altitude or terrain data. This image is unusable. 🖼🚫🎯</string>
<string name="target_found_at_msg">Target found at </string>
<string name="drone_dist_to_target_msg">Drone distance to target (slant range):</string>
<string name="drone_dist_to_target_msg">Distance to target:</string>

This comment has been minimized.

Copy link
@mkrupczak3

mkrupczak3 Apr 26, 2024

Author Member

UI cleanliness takes priority over technical correctness here

<string name="resolveTarget_oob_error_msg">ERROR: resolveTarget ran OOB at</string>
<string name="geotiff_coverage_reminder">Please ensure your DEM file covers the drone\'s location!</string>
<string name="geotiff_coverage_precedent_message">Your DEM\'s coverage ⛰:\n</string>
Expand Down

0 comments on commit d88b455

Please sign in to comment.