|
| 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