Fix DMM
This commit is contained in:
parent
bef1fec27c
commit
88fb2e0770
@ -4,11 +4,9 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use dms_coordinates::{Bearing, DMS};
|
use dms_coordinates::{Bearing, DMS};
|
||||||
use nom::{
|
use nom::{
|
||||||
branch::alt,
|
character::complete::{self},
|
||||||
character::complete::{self, space0, space1},
|
combinator::{map, map_opt},
|
||||||
combinator::{eof, map, map_opt, map_res, opt},
|
sequence::tuple,
|
||||||
number::complete::double,
|
|
||||||
sequence::{pair, tuple},
|
|
||||||
IResult,
|
IResult,
|
||||||
};
|
};
|
||||||
use std::{convert::Infallible, str::FromStr};
|
use std::{convert::Infallible, str::FromStr};
|
||||||
@ -42,98 +40,62 @@ impl FromStr for Coordinate {
|
|||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub struct DMM {
|
pub struct DMM {
|
||||||
pub degrees: i16,
|
pub degrees: i32,
|
||||||
pub minutes: f64,
|
pub minutes: f64,
|
||||||
pub direction: Direction,
|
pub direction: Direction,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_dmm(i: &str) -> IResult<&str, DMM> {
|
impl DMM {
|
||||||
map(
|
pub fn parse(i: &str) -> IResult<&str, DMM> {
|
||||||
tuple((
|
map(
|
||||||
// Degrees
|
tuple((
|
||||||
complete::i32,
|
// Degrees
|
||||||
optional_separator('°'),
|
complete::i32,
|
||||||
// Minutes
|
optional_separator('°'),
|
||||||
parse_f64,
|
// Minutes
|
||||||
optional_separator('\''),
|
parse_f64,
|
||||||
// Direction
|
optional_separator('\''),
|
||||||
parse_direction,
|
// Direction
|
||||||
)),
|
parse_direction,
|
||||||
|(degrees, (), minutes, (), direction)| {
|
)),
|
||||||
let seconds = minutes.fract() * 60_f64;
|
|(degrees, (), minutes, (), direction)| DMM {
|
||||||
let dms = DMS::new(
|
|
||||||
degrees,
|
degrees,
|
||||||
minutes,
|
minutes,
|
||||||
seconds,
|
direction,
|
||||||
match direction {
|
},
|
||||||
Direction::North => Bearing::North,
|
)(i)
|
||||||
Direction::South => Bearing::South,
|
}
|
||||||
Direction::East => Bearing::East,
|
|
||||||
Direction::West => Bearing::West,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
},
|
|
||||||
)(i)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl DMM {
|
|
||||||
// pub fn parse(i: &str) -> IResult<&str, DMM> {
|
|
||||||
// map(
|
|
||||||
// tuple((
|
|
||||||
// // Degrees
|
|
||||||
// complete::i16,
|
|
||||||
// optional_separator('°'),
|
|
||||||
// // Minutes
|
|
||||||
// double,
|
|
||||||
// optional_separator('\''),
|
|
||||||
// // Direction
|
|
||||||
// parse_direction,
|
|
||||||
// )),
|
|
||||||
// |(degrees, (), minutes, (), direction)| DMM {
|
|
||||||
// degrees,
|
|
||||||
// minutes,
|
|
||||||
// direction,
|
|
||||||
// },
|
|
||||||
// )(i)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// impl FromStr for DMM {
|
|
||||||
// type Err = ();
|
|
||||||
|
|
||||||
// /// Recognizes DMS in the formats:
|
|
||||||
// ///
|
|
||||||
// /// * `40° 31' 21" N, 105° 5' 39" W`
|
|
||||||
// /// * `40 31 21 N, 105 5 39 W`
|
|
||||||
// ///
|
|
||||||
// /// ```rust
|
|
||||||
// /// use spatial_coordinate_systems::dmm::DMM;
|
|
||||||
// /// use spatial_coordinate_systems::Direction;
|
|
||||||
// /// use std::str::FromStr;
|
|
||||||
// ///
|
|
||||||
// /// assert_eq!(DMM::from_str("40 31.3 N").unwrap(), DMM {
|
|
||||||
// /// degrees: 40,
|
|
||||||
// /// minutes: 31.3_f64,
|
|
||||||
// /// direction: Direction::North,
|
|
||||||
// /// });
|
|
||||||
// /// assert_eq!(DMM::from_str("40°31' N").unwrap(), DMM {
|
|
||||||
// /// degrees: 40,
|
|
||||||
// /// minutes: 31_f64,
|
|
||||||
// /// direction: Direction::North,
|
|
||||||
// /// });
|
|
||||||
// /// ```
|
|
||||||
// fn from_str(i: &str) -> Result<Self, Self::Err> {
|
|
||||||
// DMM::parse(i).map_err(|_| ()).map(|(_, ret)| ret)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
impl TryInto<LatLon> for Coordinate {
|
impl TryInto<LatLon> for Coordinate {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
|
|
||||||
fn try_into(self) -> Result<LatLon, Self::Error> {
|
fn try_into(self) -> Result<LatLon, Self::Error> {
|
||||||
LatLon::from(
|
LatLon::from(
|
||||||
self.0.degrees as f64 + self.0.minutes * 60.0_f64,
|
DMS::new(
|
||||||
self.1.degrees as f64 + self.1.minutes * 60.0_f64,
|
self.0.degrees,
|
||||||
|
self.0.minutes as i32,
|
||||||
|
self.0.minutes.fract() * 60_f64,
|
||||||
|
match self.0.direction {
|
||||||
|
Direction::North => Bearing::North,
|
||||||
|
Direction::South => Bearing::South,
|
||||||
|
Direction::East => Bearing::East,
|
||||||
|
Direction::West => Bearing::West,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.to_decimal_degrees(),
|
||||||
|
DMS::new(
|
||||||
|
self.1.degrees,
|
||||||
|
self.1.minutes as i32,
|
||||||
|
self.1.minutes.fract() * 60_f64,
|
||||||
|
match self.1.direction {
|
||||||
|
Direction::North => Bearing::North,
|
||||||
|
Direction::South => Bearing::South,
|
||||||
|
Direction::East => Bearing::East,
|
||||||
|
Direction::West => Bearing::West,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.to_decimal_degrees(),
|
||||||
)
|
)
|
||||||
.ok_or(())
|
.ok_or(())
|
||||||
}
|
}
|
||||||
@ -143,15 +105,19 @@ impl TryFrom<LatLon> for Coordinate {
|
|||||||
type Error = Infallible;
|
type Error = Infallible;
|
||||||
|
|
||||||
fn try_from(value: LatLon) -> Result<Self, Self::Error> {
|
fn try_from(value: LatLon) -> Result<Self, Self::Error> {
|
||||||
|
let lat = DMS::from_decimal_degrees(value.lat, true);
|
||||||
|
let lon = DMS::from_decimal_degrees(value.lon, false);
|
||||||
|
|
||||||
Ok(Self(
|
Ok(Self(
|
||||||
DMM {
|
DMM {
|
||||||
degrees: value.lat as i16,
|
degrees: lat.degrees,
|
||||||
minutes: value.lat.fract() * 60_f64,
|
minutes: lat.minutes as f64 + lat.seconds / 60_f64,
|
||||||
|
// TODO: Fix the bearing information for these
|
||||||
direction: Direction::North,
|
direction: Direction::North,
|
||||||
},
|
},
|
||||||
DMM {
|
DMM {
|
||||||
degrees: value.lon as i16,
|
degrees: lon.degrees,
|
||||||
minutes: value.lon.fract() * 60_f64,
|
minutes: lon.minutes as f64 + lon.seconds / 60_f64,
|
||||||
direction: Direction::North,
|
direction: Direction::North,
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
mod common;
|
mod common;
|
||||||
pub mod dd;
|
pub mod dd;
|
||||||
// pub mod dmm;
|
pub mod dmm;
|
||||||
pub mod dms;
|
pub mod dms;
|
||||||
pub mod plus;
|
pub mod plus;
|
||||||
pub mod utm;
|
pub mod utm;
|
||||||
@ -38,7 +38,7 @@ impl Direction {
|
|||||||
pub enum Coordinate {
|
pub enum Coordinate {
|
||||||
DD(dd::Coordinate),
|
DD(dd::Coordinate),
|
||||||
DMS(dms::Coordinate),
|
DMS(dms::Coordinate),
|
||||||
// DMM(dmm::Coordinate),
|
DMM(dmm::Coordinate),
|
||||||
UTM(utm::Coordinate),
|
UTM(utm::Coordinate),
|
||||||
Plus(plus::Coordinate),
|
Plus(plus::Coordinate),
|
||||||
}
|
}
|
||||||
@ -51,7 +51,7 @@ impl Coordinate {
|
|||||||
alt((
|
alt((
|
||||||
map(dd::Coordinate::parse, Coordinate::DD),
|
map(dd::Coordinate::parse, Coordinate::DD),
|
||||||
map(dms::Coordinate::parse, Coordinate::DMS),
|
map(dms::Coordinate::parse, Coordinate::DMS),
|
||||||
// map(dmm::Coordinate::parse, Coordinate::DMM),
|
map(dmm::Coordinate::parse, Coordinate::DMM),
|
||||||
map(utm::Coordinate::parse, Coordinate::UTM),
|
map(utm::Coordinate::parse, Coordinate::UTM),
|
||||||
map(plus::Coordinate::parse, Coordinate::Plus),
|
map(plus::Coordinate::parse, Coordinate::Plus),
|
||||||
)),
|
)),
|
||||||
@ -69,7 +69,7 @@ impl TryInto<LatLon> for Coordinate {
|
|||||||
fn try_into(self) -> Result<LatLon, Self::Error> {
|
fn try_into(self) -> Result<LatLon, Self::Error> {
|
||||||
match self {
|
match self {
|
||||||
Coordinate::DD(dd) => dd.try_into().or(Err(())),
|
Coordinate::DD(dd) => dd.try_into().or(Err(())),
|
||||||
// Coordinate::DMM(dm) => dm.try_into(),
|
Coordinate::DMM(dm) => dm.try_into(),
|
||||||
Coordinate::DMS(dms) => dms.try_into(),
|
Coordinate::DMS(dms) => dms.try_into(),
|
||||||
Coordinate::UTM(utm) => utm.try_into(),
|
Coordinate::UTM(utm) => utm.try_into(),
|
||||||
Coordinate::Plus(plus) => plus.try_into(),
|
Coordinate::Plus(plus) => plus.try_into(),
|
||||||
@ -87,11 +87,11 @@ impl From<dms::Coordinate> for Coordinate {
|
|||||||
Self::DMS(c)
|
Self::DMS(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// impl From<dmm::Coordinate> for Coordinate {
|
impl From<dmm::Coordinate> for Coordinate {
|
||||||
// fn from(c: dmm::Coordinate) -> Self {
|
fn from(c: dmm::Coordinate) -> Self {
|
||||||
// Self::DMM(c)
|
Self::DMM(c)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
impl From<utm::Coordinate> for Coordinate {
|
impl From<utm::Coordinate> for Coordinate {
|
||||||
fn from(c: utm::Coordinate) -> Self {
|
fn from(c: utm::Coordinate) -> Self {
|
||||||
Self::UTM(c)
|
Self::UTM(c)
|
||||||
|
Loading…
Reference in New Issue
Block a user