Skip to content

Commit b844903

Browse files
committed
Improve MGRS coordinate code
1 parent d7b3b71 commit b844903

File tree

10 files changed

+68
-354
lines changed

10 files changed

+68
-354
lines changed

src/main/kotlin/com/kylecorry/sol/science/geography/CoordinateFormatter.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.kylecorry.sol.shared.toDoubleCompat
77
import com.kylecorry.sol.units.Coordinate
88
import gov.nasa.worldwind.avlist.AVKey
99
import gov.nasa.worldwind.geom.Angle
10-
import gov.nasa.worldwind.geom.coords.MGRSCoord
10+
import gov.nasa.worldwind.geom.coords.MGRSCoordinateFormat
1111
import gov.nasa.worldwind.geom.coords.UPSCoord
1212
import gov.nasa.worldwind.geom.coords.UTMCoord
1313
import uk.gov.dstl.geo.osgb.Constants
@@ -57,11 +57,8 @@ object CoordinateFormatter {
5757

5858
fun Coordinate.toMGRS(precision: Int = 5): String {
5959
return try {
60-
val lat = Angle.fromDegreesLatitude(latitude)
61-
val lng = Angle.fromDegreesLongitude(longitude)
62-
val mgrs = MGRSCoord.fromLatLon(lat, lng, precision)
63-
mgrs.toString().trim()
64-
} catch (e: Exception) {
60+
MGRSCoordinateFormat.getString(latitude, longitude, precision)
61+
} catch (_: Exception) {
6562
"?"
6663
}
6764
}
@@ -188,8 +185,7 @@ object CoordinateFormatter {
188185

189186
private fun fromMGRS(location: String): Coordinate? {
190187
return try {
191-
val mgrs = MGRSCoord.fromString(location)
192-
Coordinate(mgrs.latitude.degrees, mgrs.longitude.degrees)
188+
return MGRSCoordinateFormat.fromString(location)
193189
} catch (e: Exception) {
194190
null
195191
}

src/main/kotlin/gov/nasa/worldwind/geom/LatLon.kt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,6 @@ open class LatLon {
5353
return LatLon(lat, lon)
5454
}
5555

56-
open fun add(that: Position): LatLon {
57-
58-
val lat = Angle.normalizedLatitude(this.latitude.add(that.latitude))
59-
val lon = Angle.normalizedLongitude(this.longitude.add(that.longitude))
60-
61-
return LatLon(lat, lon)
62-
}
63-
64-
open fun subtract(that: Position): LatLon {
65-
66-
val lat = Angle.normalizedLatitude(this.latitude.subtract(that.latitude))
67-
val lon = Angle.normalizedLongitude(this.longitude.subtract(that.longitude))
68-
69-
return LatLon(lat, lon)
70-
}
71-
7256
override fun toString(): String {
7357
val las = String.format("Lat %7.4f\u00B0", this.latitude.degrees)
7458
val los = String.format("Lon %7.4f\u00B0", this.longitude.degrees)

src/main/kotlin/gov/nasa/worldwind/geom/Position.kt

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/main/kotlin/gov/nasa/worldwind/geom/coords/MGRSCoord.kt

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/main/kotlin/gov/nasa/worldwind/geom/coords/MGRSCoordConverter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import kotlin.math.pow
1515
*
1616
* @author Patrick Murris
1717
* @version $Id$
18-
* @see MGRSCoord
18+
* @see MGRSCoordinateFormat
1919
*/
2020
/**
2121
* Ported to Java from the NGA GeoTrans mgrs.c and mgrs.h code. Contains routines to convert from Geodetic to MGRS and
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 com.kylecorry.sol.units.Coordinate
9+
import gov.nasa.worldwind.geom.Angle
10+
11+
/**
12+
* This class holds an immutable MGRS coordinate string along with
13+
* the corresponding latitude and longitude.
14+
*
15+
* @author Patrick Murris
16+
* @version $Id$
17+
*/
18+
object MGRSCoordinateFormat {
19+
fun getString(latitude: Double, longitude: Double, precision: Int): String {
20+
val converter = MGRSCoordConverter()
21+
val error = converter.convertGeodeticToMGRS(
22+
Angle.fromDegreesLatitude(latitude).radians,
23+
Angle.fromDegreesLongitude(longitude).radians,
24+
precision
25+
)
26+
require(error == MGRSCoordConverter.MGRS_NO_ERROR.toLong()) { "MGRS Conversion Error" }
27+
return converter.mgrsString.trim()
28+
}
29+
30+
fun fromString(str: String): Coordinate {
31+
val upperString = str.uppercase().replace(" ", "")
32+
val converter = MGRSCoordConverter()
33+
val error = converter.convertMGRSToGeodetic(upperString)
34+
35+
require(error == MGRSCoordConverter.MGRS_NO_ERROR.toLong()) { "MGRS Conversion Error" }
36+
37+
return Coordinate(
38+
Angle.fromRadians(converter.latitude).degrees,
39+
Angle.fromRadians(converter.longitude).degrees
40+
)
41+
}
42+
}

src/main/kotlin/gov/nasa/worldwind/geom/coords/TMCoord.kt

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,13 @@ import gov.nasa.worldwind.geom.Angle
1515
* @version $Id$
1616
* @see TMCoordConverter
1717
*/
18-
class TMCoord
19-
(
20-
latitude: Angle, longitude: Angle, easting: Double, northing: Double,
21-
originLatitude: Angle, centralMeridian: Angle,
22-
falseEasting: Double, falseNorthing: Double,
23-
scale: Double
24-
) {
25-
val latitude: Angle
26-
val longitude: Angle
27-
private val originLatitude: Angle
28-
private val centralMeridian: Angle
29-
private val falseEasting: Double
30-
private val falseNorthing: Double
18+
internal class TMCoord(
19+
val latitude: Angle,
20+
val longitude: Angle,
21+
val easting: Double,
22+
val northing: Double,
3123
val scale: Double
32-
val easting: Double
33-
val northing: Double
34-
35-
/**
36-
* Create an arbitrary set of Transverse Mercator coordinates with the given values.
37-
*
38-
* @param latitude the latitude `Angle`.
39-
* @param longitude the longitude `Angle`.
40-
* @param easting the easting distance value in meters.
41-
* @param northing the northing distance value in meters.
42-
* @param originLatitude the origin latitude `Angle`.
43-
* @param centralMeridian the central meridian longitude `Angle`.
44-
* @param falseEasting easting value at the center of the projection in meters.
45-
* @param falseNorthing northing value at the center of the projection in meters.
46-
* @param scale scaling factor.
47-
* @throws IllegalArgumentException if `latitude`, `longitude`, `originLatitude`
48-
* or `centralMeridian` is null.
49-
*/
50-
init {
51-
require(!(latitude == null || longitude == null)) { "Latitude Or Longitude Is Null" }
52-
require(!(originLatitude == null || centralMeridian == null)) { "Angle Is Null" }
53-
54-
this.latitude = latitude
55-
this.longitude = longitude
56-
this.easting = easting
57-
this.northing = northing
58-
this.originLatitude = originLatitude
59-
this.centralMeridian = centralMeridian
60-
this.falseEasting = falseEasting
61-
this.falseNorthing = falseNorthing
62-
this.scale = scale
63-
}
24+
) {
6425

6526
companion object {
6627
/**
@@ -89,8 +50,6 @@ class TMCoord
8950
): TMCoord {
9051
var a = a
9152
var f = f
92-
require(!(latitude == null || longitude == null)) { "Latitude Or Longitude Is Null" }
93-
require(!(originLatitude == null || centralMeridian == null)) { "Angle Is Null" }
9453

9554
val converter = TMCoordConverter()
9655
if (a == null || f == null) {
@@ -108,7 +67,7 @@ class TMCoord
10867

10968
return TMCoord(
11069
latitude, longitude, converter.easting, converter.northing,
111-
originLatitude, centralMeridian, falseEasting, falseNorthing, scale
70+
scale
11271
)
11372
}
11473

@@ -134,8 +93,6 @@ class TMCoord
13493
falseEasting: Double, falseNorthing: Double,
13594
scale: Double
13695
): TMCoord {
137-
require(!(originLatitude == null || centralMeridian == null)) { "Angle Is Null" }
138-
13996
val converter = TMCoordConverter()
14097

14198
val a = converter.a
@@ -151,7 +108,7 @@ class TMCoord
151108

152109
return TMCoord(
153110
Angle.fromRadians(converter.latitude), Angle.fromRadians(converter.longitude),
154-
easting, northing, originLatitude, centralMeridian, falseEasting, falseNorthing, scale
111+
easting, northing, scale
155112
)
156113
}
157114
}

0 commit comments

Comments
 (0)