2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::comment::mightbespace;
|
|
|
|
use crate::sql::common::commas;
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2022-10-04 21:51:18 +00:00
|
|
|
use crate::sql::fmt::Fmt;
|
2022-05-21 00:35:59 +00:00
|
|
|
use crate::sql::serde::is_internal_serialization;
|
2022-01-13 17:36:41 +00:00
|
|
|
use geo::algorithm::contains::Contains;
|
|
|
|
use geo::algorithm::intersects::Intersects;
|
|
|
|
use geo::{LineString, Point, Polygon};
|
|
|
|
use geo::{MultiLineString, MultiPoint, MultiPolygon};
|
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
2022-03-16 23:52:25 +00:00
|
|
|
use nom::character::complete::char;
|
2022-01-13 17:36:41 +00:00
|
|
|
use nom::multi::separated_list0;
|
|
|
|
use nom::multi::separated_list1;
|
|
|
|
use nom::number::complete::double;
|
|
|
|
use nom::sequence::delimited;
|
|
|
|
use nom::sequence::preceded;
|
|
|
|
use serde::ser::SerializeMap;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
use std::fmt;
|
|
|
|
use std::iter::FromIterator;
|
|
|
|
|
2022-03-16 23:52:25 +00:00
|
|
|
const SINGLE: char = '\'';
|
|
|
|
const DOUBLE: char = '\"';
|
2022-01-13 17:36:41 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize)]
|
|
|
|
pub enum Geometry {
|
|
|
|
Point(Point<f64>),
|
|
|
|
Line(LineString<f64>),
|
|
|
|
Polygon(Polygon<f64>),
|
|
|
|
MultiPoint(MultiPoint<f64>),
|
|
|
|
MultiLine(MultiLineString<f64>),
|
|
|
|
MultiPolygon(MultiPolygon<f64>),
|
|
|
|
Collection(Vec<Geometry>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for Geometry {
|
|
|
|
#[inline]
|
|
|
|
fn partial_cmp(&self, _: &Self) -> Option<Ordering> {
|
2022-03-23 11:56:39 +00:00
|
|
|
None
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<(f64, f64)> for Geometry {
|
|
|
|
fn from(v: (f64, f64)) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Point(v.into())
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<[f64; 2]> for Geometry {
|
|
|
|
fn from(v: [f64; 2]) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Point(v.into())
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Point<f64>> for Geometry {
|
|
|
|
fn from(v: Point<f64>) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Point(v)
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<LineString<f64>> for Geometry {
|
|
|
|
fn from(v: LineString<f64>) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Line(v)
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Polygon<f64>> for Geometry {
|
|
|
|
fn from(v: Polygon<f64>) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Polygon(v)
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<MultiPoint<f64>> for Geometry {
|
|
|
|
fn from(v: MultiPoint<f64>) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiPoint(v)
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<MultiLineString<f64>> for Geometry {
|
|
|
|
fn from(v: MultiLineString<f64>) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiLine(v)
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<MultiPolygon<f64>> for Geometry {
|
|
|
|
fn from(v: MultiPolygon<f64>) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiPolygon(v)
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<Geometry>> for Geometry {
|
|
|
|
fn from(v: Vec<Geometry>) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Collection(v)
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<Point<f64>>> for Geometry {
|
|
|
|
fn from(v: Vec<Point<f64>>) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiPoint(MultiPoint(v))
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<LineString<f64>>> for Geometry {
|
|
|
|
fn from(v: Vec<LineString<f64>>) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiLine(MultiLineString(v))
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<Polygon<f64>>> for Geometry {
|
|
|
|
fn from(v: Vec<Polygon<f64>>) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiPolygon(MultiPolygon(v))
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Geometry> for geo::Geometry<f64> {
|
|
|
|
fn from(v: Geometry) -> Self {
|
|
|
|
match v {
|
|
|
|
Geometry::Point(v) => v.into(),
|
|
|
|
Geometry::Line(v) => v.into(),
|
|
|
|
Geometry::Polygon(v) => v.into(),
|
|
|
|
Geometry::MultiPoint(v) => v.into(),
|
|
|
|
Geometry::MultiLine(v) => v.into(),
|
|
|
|
Geometry::MultiPolygon(v) => v.into(),
|
2022-03-04 16:01:32 +00:00
|
|
|
Geometry::Collection(v) => v.into_iter().collect::<geo::Geometry<f64>>(),
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromIterator<Geometry> for geo::Geometry<f64> {
|
|
|
|
fn from_iter<I: IntoIterator<Item = Geometry>>(iter: I) -> Self {
|
|
|
|
let mut c: Vec<geo::Geometry<f64>> = vec![];
|
|
|
|
for i in iter {
|
|
|
|
c.push(i.into())
|
|
|
|
}
|
|
|
|
geo::Geometry::GeometryCollection(geo::GeometryCollection(c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Geometry {
|
|
|
|
// -----------------------------------
|
|
|
|
// Value operations
|
|
|
|
// -----------------------------------
|
|
|
|
|
2022-10-04 21:51:18 +00:00
|
|
|
pub fn contains(&self, other: &Self) -> bool {
|
2022-01-13 17:36:41 +00:00
|
|
|
match self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Point(v) => match other {
|
|
|
|
Self::Point(w) => v.contains(w),
|
|
|
|
Self::MultiPoint(w) => w.iter().all(|x| v.contains(x)),
|
|
|
|
Self::Collection(w) => w.iter().all(|x| self.contains(x)),
|
2022-01-13 17:36:41 +00:00
|
|
|
_ => false,
|
|
|
|
},
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Line(v) => match other {
|
|
|
|
Self::Point(w) => v.contains(w),
|
|
|
|
Self::Line(w) => v.contains(w),
|
|
|
|
Self::MultiLine(w) => w.iter().all(|x| w.contains(x)),
|
|
|
|
Self::Collection(w) => w.iter().all(|x| self.contains(x)),
|
2022-01-13 17:36:41 +00:00
|
|
|
_ => false,
|
|
|
|
},
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Polygon(v) => match other {
|
|
|
|
Self::Point(w) => v.contains(w),
|
|
|
|
Self::Line(w) => v.contains(w),
|
|
|
|
Self::Polygon(w) => v.contains(w),
|
|
|
|
Self::MultiPolygon(w) => w.iter().all(|x| w.contains(x)),
|
|
|
|
Self::Collection(w) => w.iter().all(|x| self.contains(x)),
|
2022-01-13 17:36:41 +00:00
|
|
|
_ => false,
|
|
|
|
},
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiPoint(v) => match other {
|
|
|
|
Self::Point(w) => v.contains(w),
|
|
|
|
Self::MultiPoint(w) => w.iter().all(|x| w.contains(x)),
|
|
|
|
Self::Collection(w) => w.iter().all(|x| self.contains(x)),
|
2022-01-13 17:36:41 +00:00
|
|
|
_ => false,
|
|
|
|
},
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiLine(v) => match other {
|
|
|
|
Self::Point(w) => v.contains(w),
|
|
|
|
Self::Line(w) => v.contains(w),
|
|
|
|
Self::MultiLine(w) => w.iter().all(|x| w.contains(x)),
|
|
|
|
Self::Collection(w) => w.iter().all(|x| self.contains(x)),
|
2022-01-13 17:36:41 +00:00
|
|
|
_ => false,
|
|
|
|
},
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiPolygon(v) => match other {
|
|
|
|
Self::Point(w) => v.contains(w),
|
|
|
|
Self::Line(w) => v.contains(w),
|
|
|
|
Self::Polygon(w) => v.contains(w),
|
|
|
|
Self::MultiPoint(w) => v.contains(w),
|
|
|
|
Self::MultiLine(w) => v.contains(w),
|
|
|
|
Self::MultiPolygon(w) => v.contains(w),
|
|
|
|
Self::Collection(w) => w.iter().all(|x| self.contains(x)),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Collection(v) => v.iter().all(|x| x.contains(other)),
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 21:51:18 +00:00
|
|
|
pub fn intersects(&self, other: &Self) -> bool {
|
2022-01-13 17:36:41 +00:00
|
|
|
match self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Point(v) => match other {
|
|
|
|
Self::Point(w) => v.intersects(w),
|
|
|
|
Self::Line(w) => v.intersects(w),
|
|
|
|
Self::Polygon(w) => v.intersects(w),
|
|
|
|
Self::MultiPoint(w) => v.intersects(w),
|
|
|
|
Self::MultiLine(w) => w.iter().any(|x| v.intersects(x)),
|
|
|
|
Self::MultiPolygon(w) => v.intersects(w),
|
|
|
|
Self::Collection(w) => w.iter().all(|x| self.intersects(x)),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Line(v) => match other {
|
|
|
|
Self::Point(w) => v.intersects(w),
|
|
|
|
Self::Line(w) => v.intersects(w),
|
|
|
|
Self::Polygon(w) => v.intersects(w),
|
|
|
|
Self::MultiPoint(w) => v.intersects(w),
|
|
|
|
Self::MultiLine(w) => w.iter().any(|x| v.intersects(x)),
|
|
|
|
Self::MultiPolygon(w) => v.intersects(w),
|
|
|
|
Self::Collection(w) => w.iter().all(|x| self.intersects(x)),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Polygon(v) => match other {
|
|
|
|
Self::Point(w) => v.intersects(w),
|
|
|
|
Self::Line(w) => v.intersects(w),
|
|
|
|
Self::Polygon(w) => v.intersects(w),
|
|
|
|
Self::MultiPoint(w) => v.intersects(w),
|
|
|
|
Self::MultiLine(w) => v.intersects(w),
|
|
|
|
Self::MultiPolygon(w) => v.intersects(w),
|
|
|
|
Self::Collection(w) => w.iter().all(|x| self.intersects(x)),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiPoint(v) => match other {
|
|
|
|
Self::Point(w) => v.intersects(w),
|
|
|
|
Self::Line(w) => v.intersects(w),
|
|
|
|
Self::Polygon(w) => v.intersects(w),
|
|
|
|
Self::MultiPoint(w) => v.intersects(w),
|
|
|
|
Self::MultiLine(w) => w.iter().any(|x| v.intersects(x)),
|
|
|
|
Self::MultiPolygon(w) => v.intersects(w),
|
|
|
|
Self::Collection(w) => w.iter().all(|x| self.intersects(x)),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiLine(v) => match other {
|
|
|
|
Self::Point(w) => v.intersects(w),
|
|
|
|
Self::Line(w) => v.intersects(w),
|
|
|
|
Self::Polygon(w) => v.intersects(w),
|
|
|
|
Self::MultiPoint(w) => v.intersects(w),
|
|
|
|
Self::MultiLine(w) => w.iter().any(|x| v.intersects(x)),
|
|
|
|
Self::MultiPolygon(w) => v.intersects(w),
|
|
|
|
Self::Collection(w) => w.iter().all(|x| self.intersects(x)),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiPolygon(v) => match other {
|
|
|
|
Self::Point(w) => v.intersects(w),
|
|
|
|
Self::Line(w) => v.intersects(w),
|
|
|
|
Self::Polygon(w) => v.intersects(w),
|
|
|
|
Self::MultiPoint(w) => v.intersects(w),
|
|
|
|
Self::MultiLine(w) => v.intersects(w),
|
|
|
|
Self::MultiPolygon(w) => v.intersects(w),
|
|
|
|
Self::Collection(w) => w.iter().all(|x| self.intersects(x)),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Collection(v) => v.iter().all(|x| x.intersects(other)),
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Geometry {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Point(v) => {
|
2022-02-13 23:37:54 +00:00
|
|
|
write!(f, "({}, {})", v.x(), v.y())
|
2022-02-11 01:59:02 +00:00
|
|
|
}
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Line(v) => write!(
|
2022-02-11 01:59:02 +00:00
|
|
|
f,
|
|
|
|
"{{ type: 'LineString', coordinates: [{}] }}",
|
2022-10-04 21:51:18 +00:00
|
|
|
Fmt::comma_separated(v.points().map(|v| Fmt::new(v, |v, f| write!(
|
|
|
|
f,
|
|
|
|
"[{}, {}]",
|
|
|
|
v.x(),
|
|
|
|
v.y()
|
|
|
|
))))
|
2022-02-11 01:59:02 +00:00
|
|
|
),
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Polygon(v) => write!(
|
2022-02-11 01:59:02 +00:00
|
|
|
f,
|
|
|
|
"{{ type: 'Polygon', coordinates: [[{}]{}] }}",
|
2022-10-04 21:51:18 +00:00
|
|
|
Fmt::comma_separated(v.exterior().points().map(|v| Fmt::new(v, |v, f| write!(
|
|
|
|
f,
|
|
|
|
"[{}, {}]",
|
|
|
|
v.x(),
|
|
|
|
v.y()
|
|
|
|
)))),
|
|
|
|
Fmt::new(v.interiors(), |interiors, f| {
|
|
|
|
match interiors.len() {
|
|
|
|
0 => Ok(()),
|
|
|
|
_ => write!(
|
|
|
|
f,
|
|
|
|
", [{}]",
|
|
|
|
Fmt::comma_separated(interiors.iter().map(|i| Fmt::new(i, |i, f| {
|
|
|
|
write!(
|
|
|
|
f,
|
2022-02-11 01:59:02 +00:00
|
|
|
"[{}]",
|
2022-10-04 21:51:18 +00:00
|
|
|
Fmt::comma_separated(i.points().map(|v| Fmt::new(
|
|
|
|
v,
|
|
|
|
|v, f| write!(f, "[{}, {}]", v.x(), v.y())
|
|
|
|
)))
|
2022-02-11 01:59:02 +00:00
|
|
|
)
|
2022-10-04 21:51:18 +00:00
|
|
|
})))
|
|
|
|
),
|
|
|
|
}
|
|
|
|
})
|
2022-02-11 01:59:02 +00:00
|
|
|
),
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiPoint(v) => {
|
2022-02-11 01:59:02 +00:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{{ type: 'MultiPoint', coordinates: [{}] }}",
|
2022-10-04 21:51:18 +00:00
|
|
|
Fmt::comma_separated(v.iter().map(|v| Fmt::new(v, |v, f| write!(
|
|
|
|
f,
|
|
|
|
"[{}, {}]",
|
|
|
|
v.x(),
|
|
|
|
v.y()
|
|
|
|
))))
|
2022-02-11 01:59:02 +00:00
|
|
|
)
|
|
|
|
}
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiLine(v) => write!(
|
2022-02-11 01:59:02 +00:00
|
|
|
f,
|
|
|
|
"{{ type: 'MultiLineString', coordinates: [{}] }}",
|
2022-10-04 21:51:18 +00:00
|
|
|
Fmt::comma_separated(v.iter().map(|v| Fmt::new(v, |v, f| write!(
|
|
|
|
f,
|
|
|
|
"[{}]",
|
|
|
|
Fmt::comma_separated(v.points().map(|v| Fmt::new(v, |v, f| write!(
|
|
|
|
f,
|
|
|
|
"[{}, {}]",
|
|
|
|
v.x(),
|
|
|
|
v.y()
|
|
|
|
))))
|
|
|
|
))))
|
2022-02-11 01:59:02 +00:00
|
|
|
),
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiPolygon(v) => write!(
|
2022-02-11 01:59:02 +00:00
|
|
|
f,
|
|
|
|
"{{ type: 'MultiPolygon', coordinates: [{}] }}",
|
2022-10-04 21:51:18 +00:00
|
|
|
Fmt::comma_separated(v.iter().map(|v| Fmt::new(v, |v, f| {
|
|
|
|
write!(
|
|
|
|
f,
|
2022-02-11 01:59:02 +00:00
|
|
|
"[[{}]{}]",
|
2022-10-04 21:51:18 +00:00
|
|
|
Fmt::comma_separated(
|
|
|
|
v.exterior().points().map(|v| Fmt::new(v, |v, f| write!(
|
|
|
|
f,
|
|
|
|
"[{}, {}]",
|
|
|
|
v.x(),
|
|
|
|
v.y()
|
|
|
|
)))
|
|
|
|
),
|
|
|
|
Fmt::new(v.interiors(), |interiors, f| {
|
|
|
|
match interiors.len() {
|
|
|
|
0 => Ok(()),
|
|
|
|
_ => write!(
|
|
|
|
f,
|
|
|
|
", [{}]",
|
|
|
|
Fmt::comma_separated(interiors.iter().map(|i| Fmt::new(
|
|
|
|
i,
|
|
|
|
|i, f| {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"[{}]",
|
|
|
|
Fmt::comma_separated(i.points().map(|v| Fmt::new(
|
|
|
|
v,
|
|
|
|
|v, f| write!(f, "[{}, {}]", v.x(), v.y())
|
|
|
|
)))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)))
|
|
|
|
),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}))),
|
2022-02-11 01:59:02 +00:00
|
|
|
),
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Collection(v) => {
|
2022-02-11 01:59:02 +00:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{{ type: 'GeometryCollection', geometries: [{}] }}",
|
2022-10-04 21:51:18 +00:00
|
|
|
Fmt::comma_separated(v)
|
2022-02-11 01:59:02 +00:00
|
|
|
)
|
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Geometry {
|
|
|
|
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
2022-05-21 00:35:59 +00:00
|
|
|
if is_internal_serialization() {
|
|
|
|
match self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Point(v) => s.serialize_newtype_variant("Geometry", 0, "Point", v),
|
|
|
|
Self::Line(v) => s.serialize_newtype_variant("Geometry", 1, "Line", v),
|
|
|
|
Self::Polygon(v) => s.serialize_newtype_variant("Geometry", 2, "Polygon", v),
|
|
|
|
Self::MultiPoint(v) => s.serialize_newtype_variant("Geometry", 3, "MultiPoint", v),
|
|
|
|
Self::MultiLine(v) => s.serialize_newtype_variant("Geometry", 4, "MultiLine", v),
|
|
|
|
Self::MultiPolygon(v) => {
|
2022-09-02 10:32:34 +00:00
|
|
|
s.serialize_newtype_variant("Geometry", 5, "MultiPolygon", v)
|
2022-05-21 00:35:59 +00:00
|
|
|
}
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Collection(v) => s.serialize_newtype_variant("Geometry", 6, "Collection", v),
|
2022-05-21 00:35:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-13 17:36:41 +00:00
|
|
|
match self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Point(v) => {
|
2022-01-13 17:36:41 +00:00
|
|
|
let mut map = s.serialize_map(Some(2))?;
|
|
|
|
map.serialize_key("type")?;
|
|
|
|
map.serialize_value("Point")?;
|
|
|
|
map.serialize_key("coordinates")?;
|
|
|
|
map.serialize_value(vec![v.x(), v.y()].as_slice())?;
|
|
|
|
map.end()
|
|
|
|
}
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Line(v) => {
|
2022-01-13 17:36:41 +00:00
|
|
|
let mut map = s.serialize_map(Some(2))?;
|
|
|
|
map.serialize_key("type")?;
|
|
|
|
map.serialize_value("LineString")?;
|
|
|
|
map.serialize_key("coordinates")?;
|
|
|
|
map.serialize_value(
|
2022-02-22 16:30:02 +00:00
|
|
|
v.points()
|
2022-01-13 17:36:41 +00:00
|
|
|
.map(|p| vec![p.x(), p.y()])
|
|
|
|
.collect::<Vec<Vec<f64>>>()
|
|
|
|
.as_slice(),
|
|
|
|
)?;
|
|
|
|
map.end()
|
|
|
|
}
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Polygon(v) => {
|
2022-01-13 17:36:41 +00:00
|
|
|
let mut map = s.serialize_map(Some(2))?;
|
|
|
|
map.serialize_key("type")?;
|
|
|
|
map.serialize_value("Polygon")?;
|
|
|
|
map.serialize_key("coordinates")?;
|
|
|
|
map.serialize_value(
|
|
|
|
vec![v
|
|
|
|
.exterior()
|
2022-02-22 16:30:02 +00:00
|
|
|
.points()
|
2022-01-13 17:36:41 +00:00
|
|
|
.map(|p| vec![p.x(), p.y()])
|
|
|
|
.collect::<Vec<Vec<f64>>>()]
|
|
|
|
.into_iter()
|
|
|
|
.chain(
|
|
|
|
v.interiors()
|
2022-03-04 16:01:32 +00:00
|
|
|
.iter()
|
2022-01-13 17:36:41 +00:00
|
|
|
.map(|i| {
|
2022-02-22 16:30:02 +00:00
|
|
|
i.points()
|
2022-01-13 17:36:41 +00:00
|
|
|
.map(|p| vec![p.x(), p.y()])
|
|
|
|
.collect::<Vec<Vec<f64>>>()
|
|
|
|
})
|
|
|
|
.collect::<Vec<Vec<Vec<f64>>>>(),
|
|
|
|
)
|
|
|
|
.collect::<Vec<Vec<Vec<f64>>>>()
|
|
|
|
.as_slice(),
|
|
|
|
)?;
|
|
|
|
map.end()
|
|
|
|
}
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiPoint(v) => {
|
2022-01-13 17:36:41 +00:00
|
|
|
let mut map = s.serialize_map(Some(2))?;
|
|
|
|
map.serialize_key("type")?;
|
|
|
|
map.serialize_value("MultiPoint")?;
|
|
|
|
map.serialize_key("coordinates")?;
|
2022-09-02 10:32:34 +00:00
|
|
|
map.serialize_value(
|
|
|
|
v.0.iter()
|
|
|
|
.map(|v| vec![v.x(), v.y()])
|
|
|
|
.collect::<Vec<Vec<f64>>>()
|
|
|
|
.as_slice(),
|
|
|
|
)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
map.end()
|
|
|
|
}
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiLine(v) => {
|
2022-01-13 17:36:41 +00:00
|
|
|
let mut map = s.serialize_map(Some(2))?;
|
|
|
|
map.serialize_key("type")?;
|
|
|
|
map.serialize_value("MultiLineString")?;
|
|
|
|
map.serialize_key("coordinates")?;
|
2022-09-02 10:32:34 +00:00
|
|
|
map.serialize_value(
|
|
|
|
v.0.iter()
|
|
|
|
.map(|v| {
|
|
|
|
v.points().map(|v| vec![v.x(), v.y()]).collect::<Vec<Vec<f64>>>()
|
|
|
|
})
|
|
|
|
.collect::<Vec<Vec<Vec<f64>>>>()
|
|
|
|
.as_slice(),
|
|
|
|
)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
map.end()
|
|
|
|
}
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::MultiPolygon(v) => {
|
2022-01-13 17:36:41 +00:00
|
|
|
let mut map = s.serialize_map(Some(2))?;
|
|
|
|
map.serialize_key("type")?;
|
|
|
|
map.serialize_value("MultiPolygon")?;
|
|
|
|
map.serialize_key("coordinates")?;
|
2022-09-02 10:32:34 +00:00
|
|
|
map.serialize_value(
|
|
|
|
v.0.iter()
|
|
|
|
.map(|v| {
|
|
|
|
vec![v
|
|
|
|
.exterior()
|
|
|
|
.points()
|
|
|
|
.map(|p| vec![p.x(), p.y()])
|
|
|
|
.collect::<Vec<Vec<f64>>>()]
|
|
|
|
.into_iter()
|
|
|
|
.chain(
|
|
|
|
v.interiors()
|
|
|
|
.iter()
|
|
|
|
.map(|i| {
|
|
|
|
i.points()
|
|
|
|
.map(|p| vec![p.x(), p.y()])
|
|
|
|
.collect::<Vec<Vec<f64>>>()
|
|
|
|
})
|
|
|
|
.collect::<Vec<Vec<Vec<f64>>>>(),
|
|
|
|
)
|
|
|
|
.collect::<Vec<Vec<Vec<f64>>>>()
|
|
|
|
})
|
|
|
|
.collect::<Vec<Vec<Vec<Vec<f64>>>>>()
|
|
|
|
.as_slice(),
|
|
|
|
)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
map.end()
|
|
|
|
}
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Collection(v) => {
|
2022-01-13 17:36:41 +00:00
|
|
|
let mut map = s.serialize_map(Some(2))?;
|
|
|
|
map.serialize_key("type")?;
|
|
|
|
map.serialize_value("GeometryCollection")?;
|
2022-02-11 01:57:52 +00:00
|
|
|
map.serialize_key("geometries")?;
|
2022-01-13 17:36:41 +00:00
|
|
|
map.serialize_value(v)?;
|
|
|
|
map.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn geometry(i: &str) -> IResult<&str, Geometry> {
|
|
|
|
alt((simple, point, line, polygon, multipoint, multiline, multipolygon, collection))(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn simple(i: &str) -> IResult<&str, Geometry> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('(')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, x) = double(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(',')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, y) = double(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(')')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, Geometry::Point((x, y).into())))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn point(i: &str) -> IResult<&str, Geometry> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('{')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = alt((
|
|
|
|
|i| {
|
|
|
|
let (i, _) = preceded(key_type, point_type)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, v) = preceded(key_vals, point_vals)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
|i| {
|
|
|
|
let (i, v) = preceded(key_vals, point_vals)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = preceded(key_type, point_type)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
))(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('}')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn line(i: &str) -> IResult<&str, Geometry> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('{')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = alt((
|
|
|
|
|i| {
|
|
|
|
let (i, _) = preceded(key_type, line_type)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, v) = preceded(key_vals, line_vals)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
|i| {
|
|
|
|
let (i, v) = preceded(key_vals, line_vals)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = preceded(key_type, line_type)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
))(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('}')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn polygon(i: &str) -> IResult<&str, Geometry> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('{')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = alt((
|
|
|
|
|i| {
|
|
|
|
let (i, _) = preceded(key_type, polygon_type)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, v) = preceded(key_vals, polygon_vals)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
|i| {
|
|
|
|
let (i, v) = preceded(key_vals, polygon_vals)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = preceded(key_type, polygon_type)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
))(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('}')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn multipoint(i: &str) -> IResult<&str, Geometry> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('{')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = alt((
|
|
|
|
|i| {
|
|
|
|
let (i, _) = preceded(key_type, multipoint_type)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, v) = preceded(key_vals, multipoint_vals)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
|i| {
|
|
|
|
let (i, v) = preceded(key_vals, multipoint_vals)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = preceded(key_type, multipoint_type)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
))(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('}')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn multiline(i: &str) -> IResult<&str, Geometry> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('{')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = alt((
|
|
|
|
|i| {
|
|
|
|
let (i, _) = preceded(key_type, multiline_type)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, v) = preceded(key_vals, multiline_vals)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
|i| {
|
|
|
|
let (i, v) = preceded(key_vals, multiline_vals)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = preceded(key_type, multiline_type)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
))(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('}')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn multipolygon(i: &str) -> IResult<&str, Geometry> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('{')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = alt((
|
|
|
|
|i| {
|
|
|
|
let (i, _) = preceded(key_type, multipolygon_type)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, v) = preceded(key_vals, multipolygon_vals)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
|i| {
|
|
|
|
let (i, v) = preceded(key_vals, multipolygon_vals)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = preceded(key_type, multipolygon_type)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
))(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('}')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collection(i: &str) -> IResult<&str, Geometry> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('{')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = alt((
|
|
|
|
|i| {
|
|
|
|
let (i, _) = preceded(key_type, collection_type)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-02-11 01:57:52 +00:00
|
|
|
let (i, v) = preceded(key_geom, collection_vals)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
|i| {
|
2022-02-11 01:57:52 +00:00
|
|
|
let (i, v) = preceded(key_geom, collection_vals)(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = delimited(mightbespace, char(','), mightbespace)(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = preceded(key_type, collection_type)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
},
|
|
|
|
))(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('}')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
fn point_vals(i: &str) -> IResult<&str, Point<f64>> {
|
|
|
|
let (i, v) = coordinate(i)?;
|
|
|
|
Ok((i, v.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn line_vals(i: &str) -> IResult<&str, LineString<f64>> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('[')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = separated_list1(commas, coordinate)(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(']')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn polygon_vals(i: &str) -> IResult<&str, Polygon<f64>> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('[')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, e) = line_vals(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(']')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, v) = separated_list0(commas, |i| {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('[')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = line_vals(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(']')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v))
|
|
|
|
})(i)?;
|
|
|
|
Ok((i, Polygon::new(e, v)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn multipoint_vals(i: &str) -> IResult<&str, Vec<Point<f64>>> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('[')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = separated_list1(commas, point_vals)(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(']')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn multiline_vals(i: &str) -> IResult<&str, Vec<LineString<f64>>> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('[')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = separated_list1(commas, line_vals)(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(']')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn multipolygon_vals(i: &str) -> IResult<&str, Vec<Polygon<f64>>> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('[')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = separated_list1(commas, polygon_vals)(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(']')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collection_vals(i: &str) -> IResult<&str, Vec<Geometry>> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('[')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, v) = separated_list1(commas, geometry)(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(']')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
fn coordinate(i: &str) -> IResult<&str, (f64, f64)> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('[')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, x) = double(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(',')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, y) = double(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(']')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
Ok((i, (x, y)))
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
fn point_type(i: &str) -> IResult<&str, &str> {
|
|
|
|
let (i, v) = alt((
|
2022-03-16 23:52:25 +00:00
|
|
|
delimited(char(SINGLE), tag("Point"), char(SINGLE)),
|
|
|
|
delimited(char(DOUBLE), tag("Point"), char(DOUBLE)),
|
2022-01-13 17:36:41 +00:00
|
|
|
))(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn line_type(i: &str) -> IResult<&str, &str> {
|
|
|
|
let (i, v) = alt((
|
2022-03-16 23:52:25 +00:00
|
|
|
delimited(char(SINGLE), tag("LineString"), char(SINGLE)),
|
|
|
|
delimited(char(DOUBLE), tag("LineString"), char(DOUBLE)),
|
2022-01-13 17:36:41 +00:00
|
|
|
))(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn polygon_type(i: &str) -> IResult<&str, &str> {
|
|
|
|
let (i, v) = alt((
|
2022-03-16 23:52:25 +00:00
|
|
|
delimited(char(SINGLE), tag("Polygon"), char(SINGLE)),
|
|
|
|
delimited(char(DOUBLE), tag("Polygon"), char(DOUBLE)),
|
2022-01-13 17:36:41 +00:00
|
|
|
))(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn multipoint_type(i: &str) -> IResult<&str, &str> {
|
|
|
|
let (i, v) = alt((
|
2022-03-16 23:52:25 +00:00
|
|
|
delimited(char(SINGLE), tag("MultiPoint"), char(SINGLE)),
|
|
|
|
delimited(char(DOUBLE), tag("MultiPoint"), char(DOUBLE)),
|
2022-01-13 17:36:41 +00:00
|
|
|
))(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn multiline_type(i: &str) -> IResult<&str, &str> {
|
|
|
|
let (i, v) = alt((
|
2022-03-16 23:52:25 +00:00
|
|
|
delimited(char(SINGLE), tag("MultiLineString"), char(SINGLE)),
|
|
|
|
delimited(char(DOUBLE), tag("MultiLineString"), char(DOUBLE)),
|
2022-01-13 17:36:41 +00:00
|
|
|
))(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn multipolygon_type(i: &str) -> IResult<&str, &str> {
|
|
|
|
let (i, v) = alt((
|
2022-03-16 23:52:25 +00:00
|
|
|
delimited(char(SINGLE), tag("MultiPolygon"), char(SINGLE)),
|
|
|
|
delimited(char(DOUBLE), tag("MultiPolygon"), char(DOUBLE)),
|
2022-01-13 17:36:41 +00:00
|
|
|
))(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collection_type(i: &str) -> IResult<&str, &str> {
|
|
|
|
let (i, v) = alt((
|
2022-03-16 23:52:25 +00:00
|
|
|
delimited(char(SINGLE), tag("GeometryCollection"), char(SINGLE)),
|
|
|
|
delimited(char(DOUBLE), tag("GeometryCollection"), char(DOUBLE)),
|
2022-01-13 17:36:41 +00:00
|
|
|
))(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
fn key_type(i: &str) -> IResult<&str, &str> {
|
|
|
|
let (i, v) = alt((
|
|
|
|
tag("type"),
|
2022-03-16 23:52:25 +00:00
|
|
|
delimited(char(SINGLE), tag("type"), char(SINGLE)),
|
|
|
|
delimited(char(DOUBLE), tag("type"), char(DOUBLE)),
|
2022-01-13 17:36:41 +00:00
|
|
|
))(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(':')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn key_vals(i: &str) -> IResult<&str, &str> {
|
|
|
|
let (i, v) = alt((
|
|
|
|
tag("coordinates"),
|
2022-03-16 23:52:25 +00:00
|
|
|
delimited(char(SINGLE), tag("coordinates"), char(SINGLE)),
|
|
|
|
delimited(char(DOUBLE), tag("coordinates"), char(DOUBLE)),
|
2022-01-13 17:36:41 +00:00
|
|
|
))(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(':')(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
2022-02-11 01:57:52 +00:00
|
|
|
fn key_geom(i: &str) -> IResult<&str, &str> {
|
|
|
|
let (i, v) = alt((
|
|
|
|
tag("geometries"),
|
2022-03-16 23:52:25 +00:00
|
|
|
delimited(char(SINGLE), tag("geometries"), char(SINGLE)),
|
|
|
|
delimited(char(DOUBLE), tag("geometries"), char(DOUBLE)),
|
2022-02-11 01:57:52 +00:00
|
|
|
))(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(':')(i)?;
|
2022-02-11 01:57:52 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple() {
|
|
|
|
let sql = "(51.509865, -0.118092)";
|
|
|
|
let res = geometry(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("(51.509865, -0.118092)", format!("{}", out));
|
|
|
|
}
|
|
|
|
}
|