Skip to content

Commit

Permalink
surrealdb#98 Added support for dms format
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkmcc committed May 1, 2023
1 parent 5d49379 commit 3dddc0e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 16 deletions.
21 changes: 17 additions & 4 deletions lib/src/sql/geometry.rs
Expand Up @@ -4,7 +4,7 @@ use crate::sql::comment::mightbespace;
use crate::sql::common::commas;
use crate::sql::error::IResult;
use crate::sql::fmt::Fmt;
use crate::sql::latlng::decimal_degree;
use crate::sql::latlng::{decimal_degree, dms};
use geo::algorithm::contains::Contains;
use geo::algorithm::intersects::Intersects;
use geo::{Coord, LineString, Point, Polygon};
Expand All @@ -26,8 +26,8 @@ use std::{fmt, hash};

pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Geometry";

const SINGLE: char = '\'';
const DOUBLE: char = '\"';
pub(crate) const SINGLE: char = '\'';
pub(crate) const DOUBLE: char = '\"';

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename = "$surrealdb::private::sql::Geometry")]
Expand Down Expand Up @@ -562,7 +562,11 @@ fn normal(i: &str) -> IResult<&str, Geometry> {
}

fn latlng(i: &str) -> IResult<&str, Geometry> {
map(decimal_degree, |dd| Geometry::Point(dd.into())).parse(i)
alt((
map(decimal_degree, |dd| Geometry::Point(dd.into())),
map(dms, |dd| Geometry::Point(dd.into())),
))
.parse(i)
}

fn point(i: &str) -> IResult<&str, Geometry> {
Expand Down Expand Up @@ -915,4 +919,13 @@ mod tests {
let out = res.unwrap().1;
assert_eq!("(-74.0445, 40.6892)", format!("{}", out));
}

#[test]
fn dms() {
let sql = r#"40°60'3600"N 79°60'3600"W"#;
let res = geometry(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("(-81, 42)", format!("{}", out));
}
}
85 changes: 73 additions & 12 deletions lib/src/sql/latlng.rs
@@ -1,14 +1,17 @@
use crate::sql::comment::mightbespace;
use crate::sql::error::{Error, IResult};
use crate::sql::geometry::{DOUBLE, SINGLE};
use crate::sql::number::integer;
use crate::sql::Operator::Dec;
use geo::Point;
use nom::branch::alt;
use nom::character::complete::i64;
use nom::character::streaming::char;
use nom::combinator::map;
use nom::complete::tag;
use nom::error::ParseError;
use nom::number::complete::double;
use nom::sequence::tuple;
use nom::sequence::{delimited, tuple};
use nom::Parser;
use std::fmt;
use std::fmt::Formatter;
Expand Down Expand Up @@ -72,6 +75,13 @@ impl CardinalDegree {
}
}

impl From<(f64, f64, f64, CardinalDirection)> for CardinalDegree {
fn from(value: (f64, f64, f64, CardinalDirection)) -> Self {
let (degrees, minutes, seconds, direction) = value;
CardinalDegree(direction, degrees + minutes / 60.0 + seconds / 3600.0)
}
}

impl fmt::Display for CardinalDegree {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
// todo: precision?
Expand All @@ -83,15 +93,15 @@ impl fmt::Display for CardinalDegree {
/// https://en.wikipedia.org/wiki/Decimal_degrees
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct DecimalDegrees {
vertical: CardinalDegree,
horizontal: CardinalDegree,
lat: CardinalDegree,
lng: CardinalDegree,
}

impl From<(CardinalDegree, CardinalDegree)> for DecimalDegrees {
fn from(value: (CardinalDegree, CardinalDegree)) -> Self {
DecimalDegrees {
vertical: value.0,
horizontal: value.1,
lat: value.0,
lng: value.1,
}
}
}
Expand All @@ -100,16 +110,13 @@ impl Into<Point> for DecimalDegrees {
/// Converts the decimal degrees into a [`Point`] by taking into account
/// the direction of each degree.
fn into(self) -> Point {
Point::new(
self.horizontal.value() * self.horizontal.direction(),
self.vertical.value() * self.vertical.direction(),
)
Point::new(self.lng.value() * self.lng.direction(), self.lat.value() * self.lat.direction())
}
}

impl fmt::Display for DecimalDegrees {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{} {}", self.vertical, self.horizontal)
write!(f, "{} {}", self.lat, self.lng)
}
}

Expand Down Expand Up @@ -157,6 +164,39 @@ fn cardinal_degree<'i: 't, 't>(
))(i)
}

fn dms_components(i: &str) -> IResult<&str, (f64, f64, f64)> {
let (i, degrees) = degree(i)?;
let (i, _) = mightbespace(i)?;
let (i, minutes) = double(i)?;
let (i, _) = char(SINGLE)(i)?;
let (i, _) = mightbespace(i)?;
let (i, seconds) = double(i)?;
let (i, _) = char(DOUBLE)(i)?;
Ok((i, (degrees, minutes, seconds)))
}

/// Parses a single [`CardinalDegree`] in DMS format. The following formats are supported.
/// - 40°60'3600"N
/// - 40.0°60.0'3600.0"N
/// - 40° 60' 3600" N
/// - N40°60'3600"
/// - N 40° 60' 3600"
fn dms_dir(
i: &str,
direction: fn(input: &str) -> IResult<&str, CardinalDirection>,
) -> IResult<&str, CardinalDegree> {
alt((
map(
tuple((mightbespace, direction, mightbespace, dms_components)),
|(_, dir, _, (deg, min, sec))| (deg, min, sec, dir).into(),
),
map(
tuple((mightbespace, dms_components, mightbespace, direction)),
|(_, (deg, min, sec), _, dir)| (deg, min, sec, dir).into(),
),
))(i)
}

/// Parses a latitude or longitude represented in decimal degrees.
/// The following formats are supported.
///
Expand All @@ -170,14 +210,22 @@ pub(crate) fn decimal_degree(i: &str) -> IResult<&str, DecimalDegrees> {
Ok((i, (vertical, horizontal).into()))
}

/// Parses a latitude or longitude represented in DMS format.
pub(crate) fn dms(i: &str) -> IResult<&str, DecimalDegrees> {
let (i, vertical) = dms_dir(i, vertical_dir)?;
let (i, _) = mightbespace(i)?;
let (i, horizontal) = dms_dir(i, horizontal_dir)?;
(Ok((i, (vertical, horizontal).into())))
}

#[cfg(test)]
mod tests {
use crate::sql::latlng::*;

fn example_dd() -> DecimalDegrees {
DecimalDegrees {
vertical: CardinalDegree(CardinalDirection::North, 40.6892),
horizontal: CardinalDegree(CardinalDirection::West, 74.0445),
lat: CardinalDegree(CardinalDirection::North, 40.6892),
lng: CardinalDegree(CardinalDirection::West, 74.0445),
}
}

Expand Down Expand Up @@ -220,4 +268,17 @@ mod tests {
assert_eq!(decimal_degree("N40.6892° W74.0445°").unwrap().1, dd);
assert_eq!(decimal_degree("N 40.6892° W 74.0445°").unwrap().1, dd);
}

#[test]
fn test_parse_dms() {
let dd = DecimalDegrees {
lat: CardinalDegree(CardinalDirection::North, 42.0),
lng: CardinalDegree(CardinalDirection::West, 81.0),
};
assert_eq!(dms(r#"40°60'3600"N 79°60'3600"W"#).unwrap().1, dd);
assert_eq!(dms(r#"40.0°60.0'3600.0"N 79.0°60.0'3600.0"W"#).unwrap().1, dd);
assert_eq!(dms(r#"40° 60' 3600" N 79° 60' 3600" W"#).unwrap().1, dd);
assert_eq!(dms(r#"N40°60'3600" W79°60'3600""#).unwrap().1, dd);
assert_eq!(dms(r#"N 40° 60' 3600" W 79° 60' 3600""#).unwrap().1, dd);
}
}

0 comments on commit 3dddc0e

Please sign in to comment.