pub struct Mgrs { /* private fields */ }Expand description
Representation of a WGS84
Military Grid Reference System
point. Stored internally as a UtmUps point with a precision.
Implementations§
Source§impl Mgrs
impl Mgrs
Sourcepub fn create(
zone: i32,
northp: bool,
easting: f64,
northing: f64,
precision: i32,
) -> Result<Mgrs, Error>
pub fn create( zone: i32, northp: bool, easting: f64, northing: f64, precision: i32, ) -> Result<Mgrs, Error>
Tries to create a MGRS point from its constituent parts. Validates the
arguments to ensure a valid MGRS point can be created. You most likely
want to instantiate this via parse_str or UtmUps instead
of manually specifying the values.
§Errors
Returns Error::InvalidMgrs if the position is invalid.
Returns Error::InvalidPrecision if the precision is not in range [1, 11].
§Usage
use geoconvert::Mgrs;
let coord = Mgrs::create(18, true, 585664.121, 4511315.422, 6);
assert!(coord.is_ok());
let coord = coord.unwrap();
assert_eq!(coord.zone(), 18);
assert_eq!(coord.is_north(), true);
assert!((coord.easting() - 585664.121).abs() < 1e-3);
assert!((coord.northing() - 4511315.422).abs() < 1e-3);
assert_eq!(coord.precision(), 6);
let invalid_coord_zone_neg = Mgrs::create(-10, true, 585664.121, 4511315.422, 6);
assert!(invalid_coord_zone_neg.is_err());
let invalid_coord_zone_too_big = Mgrs::create(70, true, 585664.121, 4511315.422, 6);
assert!(invalid_coord_zone_too_big.is_err());Sourcepub fn is_utm(&self) -> bool
pub fn is_utm(&self) -> bool
Returns whether the MGRS is stored as UTM or UPS.
§Example
use geoconvert::Mgrs;
let coord = Mgrs::parse_str("18TWL856641113154").unwrap();
assert_eq!(coord.is_utm(), true);Sourcepub fn zone(&self) -> i32
pub fn zone(&self) -> i32
Returns the UTM zone.
§Example
use geoconvert::Mgrs;
let coord = Mgrs::parse_str("18TWL856641113154").unwrap();
assert_eq!(coord.zone(), 18);Sourcepub fn is_north(&self) -> bool
pub fn is_north(&self) -> bool
Returns whether the coordinate is in the northern hemisphere.
§Example
use geoconvert::Mgrs;
let coord = Mgrs::parse_str("18TWL856641113154").unwrap();
assert_eq!(coord.is_north(), true);Sourcepub fn easting(&self) -> f64
pub fn easting(&self) -> f64
Returns the UTM easting.
§Example
use geoconvert::Mgrs;
let coord = Mgrs::parse_str("18TWL856641113154").unwrap();
assert!((coord.easting() - 585664.15).abs() < 1e-2);Sourcepub fn northing(&self) -> f64
pub fn northing(&self) -> f64
Returns the UTM northing.
§Example
use geoconvert::Mgrs;
let coord = Mgrs::parse_str("18TWL856641113154").unwrap();
assert!((coord.northing() - 4511315.45).abs() < 1e-2);Sourcepub fn precision(&self) -> i32
pub fn precision(&self) -> i32
Returns the current precision for outputting to a string.
§Example
use geoconvert::Mgrs;
let coord = Mgrs::parse_str("18TWL856641113154").unwrap();
assert_eq!(coord.precision(), 6);Sourcepub fn set_precision(&mut self, precision: i32) -> Result<(), Error>
pub fn set_precision(&mut self, precision: i32) -> Result<(), Error>
Set the precision.
Must be in range [1, 11].
§Errors
Error::InvalidPrecision:precisionis not in the valid range
§Example
use geoconvert::Mgrs;
let mut coord = Mgrs::parse_str("18TWL856641113154").unwrap();
coord.set_precision(7);
assert_eq!(coord.precision(), 7);Sourcepub fn parse_str(mgrs_str: &str) -> Result<Mgrs, Error>
pub fn parse_str(mgrs_str: &str) -> Result<Mgrs, Error>
Parses a string as MGRS. Assumes the string is only composed of the MGRS coordinate (e.g. no preceding/trailing whitespace) and there are no spaces in the string. Example valid strings:
27UXQ0314512982YXL6143481146
§Errors
Error::InvalidMgrs: the string couldn’t be parsed to a valid MGRS coordinate.
Sourcepub fn from_latlon(value: &LatLon, precision: i32) -> Mgrs
pub fn from_latlon(value: &LatLon, precision: i32) -> Mgrs
§Usage
use geoconvert::{LatLon, Mgrs};
let coord = LatLon::create(40.748333, -73.985278).unwrap();
let coord_mgrs = Mgrs::parse_str("18TWL856641113154").unwrap();
let converted = LatLon::from_mgrs(&coord_mgrs);
// Check if the converted coordinate is accurate to 6 decimals (same as reference)
assert!((converted.latitude() - coord.latitude()).abs() < 1e-6);
assert!((converted.longitude() - coord.longitude()).abs() < 1e-6);Sourcepub fn from_utmups(value: &UtmUps, precision: i32) -> Mgrs
pub fn from_utmups(value: &UtmUps, precision: i32) -> Mgrs
§Usage
use geoconvert::{Mgrs, UtmUps};
let coord = Mgrs::parse_str("18TWL856641113154").unwrap();
let coord_utm = UtmUps::create(18, true, 585664.15, 4511315.45).unwrap();
let converted = Mgrs::from_utmups(&coord_utm, 6);
// Check if the converted coordinate is accurate to 6 decimals (same as reference)
assert_eq!(coord.zone(), converted.zone());
assert_eq!(coord.is_north(), converted.is_north());
assert!((coord.easting() - converted.easting()).abs() < 1e-2);
assert!((coord.northing() - converted.northing()).abs() < 1e-2);
assert_eq!(coord.precision(), converted.precision());Sourcepub fn to_utmups(&self) -> UtmUps
pub fn to_utmups(&self) -> UtmUps
§Usage
use geoconvert::{Mgrs, UtmUps};
let coord = Mgrs::parse_str("18TWL856641113154").unwrap();
let coord_utm = UtmUps::create(18, true, 585664.15, 4511315.45).unwrap();
let converted = coord.to_utmups();
// Check if the converted coordinate is accurate to 6 decimals (same as reference)
assert_eq!(coord_utm.zone(), converted.zone());
assert_eq!(coord_utm.is_north(), converted.is_north());
assert!((coord_utm.easting() - converted.easting()).abs() < 1e-2);
assert!((coord_utm.northing() - converted.northing()).abs() < 1e-2);