Skip to content

Commit 7431823

Browse files
committed
Add coordinate converters (ported from Java).
Add UTM, MGRS, GARS and LatLon graticule (ported from Java). Add MGRSGraticuleActivity to examples.
1 parent e546ee4 commit 7431823

37 files changed

Lines changed: 6937 additions & 0 deletions

worldwind-examples/src/main/AndroidManifest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
android:noHistory="true"
5757
android:theme="@style/AppTheme.NoActionBar">
5858
</activity>
59+
<activity
60+
android:name=".MGRSGraticuleActivity"
61+
android:configChanges="orientation|screenSize"
62+
android:label="@string/title_mgrs_graticule"
63+
android:launchMode="singleInstance"
64+
android:noHistory="true"
65+
android:theme="@style/AppTheme.NoActionBar">
66+
</activity>
5967
<activity
6068
android:name=".MultiGlobeActivity"
6169
android:configChanges="orientation|screenSize"

worldwind-examples/src/main/java/gov/nasa/worldwindx/AbstractMainActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ public boolean onNavigationItemSelected(MenuItem item) {
315315
case R.id.nav_general_globe_activity:
316316
startActivity(new Intent(getApplicationContext(), GeneralGlobeActivity.class));
317317
break;
318+
case R.id.nav_mgrs_graticule_activity:
319+
startActivity(new Intent(getApplicationContext(), MGRSGraticuleActivity.class));
320+
break;
318321
case R.id.nav_multi_globe_activity:
319322
startActivity(new Intent(getApplicationContext(), MultiGlobeActivity.class));
320323
break;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package gov.nasa.worldwindx;
2+
3+
import android.os.Bundle;
4+
5+
import gov.nasa.worldwind.layer.graticule.MGRSGraticuleLayer;
6+
7+
public class MGRSGraticuleActivity extends GeneralGlobeActivity {
8+
9+
@Override
10+
protected void onCreate(Bundle savedInstanceState) {
11+
super.onCreate(savedInstanceState);
12+
13+
this.wwd.getLayers().addLayer(new MGRSGraticuleLayer());
14+
}
15+
16+
}

worldwind-examples/src/main/res/menu/activity_drawer.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
android:id="@+id/nav_multi_globe_activity"
1616
android:icon="@android:drawable/ic_menu_compass"
1717
android:title="@string/title_multi_globe"/>
18+
<item
19+
android:id="@+id/nav_mgrs_graticule_activity"
20+
android:icon="@android:drawable/ic_menu_compass"
21+
android:title="@string/title_mgrs_graticule"/>
1822
<item
1923
android:id="@+id/nav_placemarks_demo_activity"
2024
android:icon="@android:drawable/ic_menu_myplaces"

worldwind-examples/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<string name="title_basic_stress_test">Basic Stress Test</string>
1313
<string name="title_day_night_cycle">Day and Night Cycle</string>
1414
<string name="title_general_globe">General Purpose Globe</string>
15+
<string name="title_mgrs_graticule">MGRS graticule Demonstration</string>
1516
<string name="title_multi_globe">Multi-Globe Demonstration</string>
1617
<string name="title_movable_line_of_sight">Movable Line of Sight</string>
1718
<string name="title_paths_example">Paths Example</string>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package gov.nasa.worldwind.geom.coords;
2+
3+
public enum Hemisphere {
4+
N, S
5+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (C) 2011 United States Government as represented by the Administrator of the
3+
* National Aeronautics and Space Administration.
4+
* All Rights Reserved.
5+
*/
6+
package gov.nasa.worldwind.geom.coords;
7+
8+
import android.support.annotation.NonNull;
9+
10+
/**
11+
* This class holds an immutable MGRS coordinate string along with
12+
* the corresponding latitude and longitude.
13+
*
14+
* @author Patrick Murris
15+
* @version $Id$
16+
*/
17+
18+
public class MGRSCoord {
19+
private final String MGRSString;
20+
private final double latitude;
21+
private final double longitude;
22+
23+
/**
24+
* Create a WGS84 MGRS coordinate from a pair of latitude and longitude <code>double</code>
25+
* with the maximum precision of five digits (one meter).
26+
*
27+
* @param latitude the latitude <code>double</code>.
28+
* @param longitude the longitude <code>double</code>.
29+
* @return the corresponding <code>MGRSCoord</code>.
30+
* @throws IllegalArgumentException if <code>latitude</code> or <code>longitude</code> is null,
31+
* or the conversion to MGRS coordinates fails.
32+
*/
33+
public static MGRSCoord fromLatLon(double latitude, double longitude) {
34+
return fromLatLon(latitude, longitude, 5);
35+
}
36+
37+
/**
38+
* Create a MGRS coordinate from a pair of latitude and longitude <code>double</code>
39+
* with the given precision or number of digits (1 to 5).
40+
*
41+
* @param latitude the latitude <code>double</code>.
42+
* @param longitude the longitude <code>double</code>.
43+
* @param precision the number of digits used for easting and northing (1 to 5).
44+
* @return the corresponding <code>MGRSCoord</code>.
45+
* @throws IllegalArgumentException if <code>latitude</code> or <code>longitude</code> is null,
46+
* or the conversion to MGRS coordinates fails.
47+
*/
48+
public static MGRSCoord fromLatLon(double latitude, double longitude, int precision) {
49+
final MGRSCoordConverter converter = new MGRSCoordConverter();
50+
long err = converter.convertGeodeticToMGRS(Math.toRadians(latitude), Math.toRadians(longitude), precision);
51+
52+
if (err != MGRSCoordConverter.MGRS_NO_ERROR) {
53+
throw new IllegalArgumentException("MGRS Conversion Error");
54+
}
55+
56+
return new MGRSCoord(latitude, longitude, converter.getMGRSString());
57+
}
58+
59+
/**
60+
* Create a MGRS coordinate from a standard MGRS coordinate text string.
61+
* <p>
62+
* The string will be converted to uppercase and stripped of all spaces before being evaluated.
63+
* </p>
64+
* <p>Valid examples:<br />
65+
* 32TLP5626635418<br />
66+
* 32 T LP 56266 35418<br />
67+
* 11S KU 528 111<br />
68+
* </p>
69+
* @param MGRSString the MGRS coordinate text string.
70+
* @return the corresponding <code>MGRSCoord</code>.
71+
* @throws IllegalArgumentException if the <code>MGRSString</code> is null or empty,
72+
* the <code>globe</code> is null, or the conversion to geodetic coordinates fails (invalid coordinate string).
73+
*/
74+
public static MGRSCoord fromString(String MGRSString) {
75+
MGRSString = MGRSString.toUpperCase().replaceAll(" ", "");
76+
77+
final MGRSCoordConverter converter = new MGRSCoordConverter();
78+
long err = converter.convertMGRSToGeodetic(MGRSString);
79+
80+
if (err != MGRSCoordConverter.MGRS_NO_ERROR) {
81+
throw new IllegalArgumentException("MGRS Conversion Error");
82+
}
83+
84+
return new MGRSCoord(Math.toDegrees(converter.getLatitude()), Math.toDegrees(converter.getLongitude()), MGRSString);
85+
}
86+
87+
/**
88+
* Create an arbitrary MGRS coordinate from a pair of latitude-longitude <code>double</code>
89+
* and the corresponding MGRS coordinate string.
90+
*
91+
* @param latitude the latitude <code>double</code>.
92+
* @param longitude the longitude <code>double</code>.
93+
* @param MGRSString the corresponding MGRS coordinate string.
94+
* @throws IllegalArgumentException if <code>latitude</code> or <code>longitude</code> is null,
95+
* or the MGRSString is null or empty.
96+
*/
97+
public MGRSCoord(double latitude, double longitude, String MGRSString) {
98+
this.latitude = latitude;
99+
this.longitude = longitude;
100+
this.MGRSString = MGRSString;
101+
}
102+
103+
public double getLatitude() {
104+
return this.latitude;
105+
}
106+
107+
public double getLongitude() {
108+
return this.longitude;
109+
}
110+
111+
@NonNull
112+
@Override
113+
public String toString() {
114+
return this.MGRSString;
115+
}
116+
117+
}

0 commit comments

Comments
 (0)