Mgrs

Struct Mgrs 

Source
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

Source

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());
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

pub fn set_precision(&mut self, precision: i32) -> Result<(), Error>

Set the precision.

Must be in range [1, 11].

§Errors
§Example
use geoconvert::Mgrs;
 
let mut coord = Mgrs::parse_str("18TWL856641113154").unwrap();
coord.set_precision(7);
 
assert_eq!(coord.precision(), 7);
Source

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:

  • 27UXQ0314512982
  • YXL6143481146
§Errors
Source

pub fn from_latlon(value: &LatLon, precision: i32) -> Mgrs

Converts from LatLon to 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);
Source

pub fn to_latlon(&self) -> LatLon

Converts from Mgrs to LatLon

§Usage
use geoconvert::{LatLon, Mgrs};
 
let coord = LatLon::create(40.748333, -73.985278).unwrap();
 
let converted = coord.to_mgrs(6);
 
assert_eq!(converted.to_string(), "18TWL856641113154");
Source

pub fn from_utmups(value: &UtmUps, precision: i32) -> Mgrs

Converts from UtmUps to 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());
Source

pub fn to_utmups(&self) -> UtmUps

Converts from Mgrs to 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);

Trait Implementations§

Source§

impl Clone for Mgrs

Source§

fn clone(&self) -> Mgrs

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Mgrs

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Mgrs

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for Mgrs

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Copy for Mgrs

Auto Trait Implementations§

§

impl Freeze for Mgrs

§

impl RefUnwindSafe for Mgrs

§

impl Send for Mgrs

§

impl Sync for Mgrs

§

impl Unpin for Mgrs

§

impl UnwindSafe for Mgrs

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.