LatLon

Struct LatLon 

Source
pub struct LatLon { /* private fields */ }
Expand description

Representation of a WGS84 Latitude/Longitude point. Can be converted to/from UtmUps and Mgrs.

Implementations§

Source§

impl LatLon

Source

pub fn create(lat: f64, lon: f64) -> Result<LatLon, Error>

Tries to create a latitude/longitude point from a lat/lon pair. First checks if the values are valid:

  • Latitude must be in range [-90,90]
  • Longitude must be in range [-180,180]
§Errors

Returns Error::InvalidCoord if either latitude or longitude are invalid.

§Usage
use geoconvert::LatLon;
 
let coord = LatLon::create(40.748333, -73.985278);
 
assert!(coord.is_ok());
 
let coord = coord.unwrap();
 
assert_eq!(coord.latitude(), 40.748333);
assert_eq!(coord.longitude(), -73.985278);
 
let invalid_coord_lat = LatLon::create(100.0, 0.0);
assert!(invalid_coord_lat.is_err());
 
let invalid_coord_lon = LatLon::create(0.0, -200.0);
assert!(invalid_coord_lon.is_err());
Source

pub fn latitude(&self) -> f64

Returns the latitude value.

§Example
use geoconvert::LatLon;
 
let coord = LatLon::create(40.748333, -73.985278).unwrap();
assert_eq!(coord.latitude(), 40.748333);
Source

pub fn longitude(&self) -> f64

Returns the longitude value.

§Example
use geoconvert::LatLon;
 
let coord = LatLon::create(40.748333, -73.985278).unwrap();
assert_eq!(coord.longitude(), -73.985278);
Source

pub fn is_north(&self) -> bool

Returns whether the current point is in the northern hemisphere.

§Example
use geoconvert::LatLon;
 
let coord = LatLon::create(40.748333, -73.985278).unwrap();
assert!(coord.is_north());
 
let coord = LatLon::create(-40.748333, -73.985278).unwrap();
assert!(!coord.is_north());
Source

pub fn haversine(&self, other: &LatLon) -> f64

Returns the distance in meters between two LatLon points using the haversine formula. Uses the mean radius of the Earth in the calculation: 6371.0088

Source

pub fn from_utmups(value: &UtmUps) -> LatLon

Converts from UtmUps to LatLon

§Usage
use geoconvert::{LatLon, UtmUps};
 
let coord = LatLon::create(40.748333, -73.985278).unwrap();
let coord_utm = UtmUps::create(18, true, 585664.121, 4511315.422).unwrap();
 
let converted = LatLon::from_utmups(&coord_utm);
 
// 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_utmups(&self) -> UtmUps

Converts from LatLon to UtmUps

§Usage
use geoconvert::{LatLon, UtmUps};
 
let coord = LatLon::create(40.748333, -73.985278).unwrap();
let coord_utm = UtmUps::create(18, true, 585664.121, 4511315.422).unwrap();
 
let converted = coord.to_utmups();
 
assert_eq!(converted.zone(), coord_utm.zone());
assert_eq!(converted.is_north(), coord_utm.is_north());
// Check if the converted coordinate is accurate to 3 decimals (same as reference)
assert!((converted.easting() - coord_utm.easting()).abs() < 1e-3);
assert!((converted.northing() - coord_utm.northing()).abs() < 1e-3);
Source

pub fn from_mgrs(value: &Mgrs) -> LatLon

Converts from Mgrs to LatLon

§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_mgrs(&self, precision: i32) -> Mgrs

Converts from LatLon to Mgrs

§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");

Trait Implementations§

Source§

impl Clone for LatLon

Source§

fn clone(&self) -> LatLon

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 LatLon

Source§

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

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

impl Display for LatLon

Source§

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

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

impl Copy for LatLon

Auto Trait Implementations§

§

impl Freeze for LatLon

§

impl RefUnwindSafe for LatLon

§

impl Send for LatLon

§

impl Sync for LatLon

§

impl Unpin for LatLon

§

impl UnwindSafe for LatLon

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.