From 5e25435b9a42ee94bb4513eedb5089d74daa769f Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Fri, 11 Feb 2022 01:59:02 +0000 Subject: [PATCH] Use custom string for geometries output --- src/sql/geometry.rs | 106 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/src/sql/geometry.rs b/src/sql/geometry.rs index 1d8d0419..051418c7 100644 --- a/src/sql/geometry.rs +++ b/src/sql/geometry.rs @@ -253,8 +253,110 @@ impl Geometry { impl fmt::Display for Geometry { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Geometry::Point(v) => write!(f, "({}, {})", v.x(), v.y()), - _ => write!(f, "{}", serde_json::to_string(self).unwrap_or(String::from(""))), + Geometry::Point(v) => { + write!(f, "{{ type: 'Point', coordinates: [{}, {}] }}", v.x(), v.y()) + } + Geometry::Line(v) => write!( + f, + "{{ type: 'LineString', coordinates: [{}] }}", + v.points_iter() + .map(|ref v| format!("[{}, {}]", v.x(), v.y())) + .collect::>() + .join(", ") + ), + Geometry::Polygon(v) => write!( + f, + "{{ type: 'Polygon', coordinates: [[{}]{}] }}", + v.exterior() + .points_iter() + .map(|ref v| format!("[{}, {}]", v.x(), v.y())) + .collect::>() + .join(", "), + match v.interiors().len() { + 0 => format!(""), + _ => format!( + ", [{}]", + v.interiors() + .iter() + .map(|i| { + format!( + "[{}]", + i.points_iter() + .map(|ref v| format!("[{}, {}]", v.x(), v.y())) + .collect::>() + .join(", ") + ) + }) + .collect::>() + .join(", "), + ), + } + ), + Geometry::MultiPoint(v) => { + write!( + f, + "{{ type: 'MultiPoint', coordinates: [{}] }}", + v.iter() + .map(|v| format!("[{}, {}]", v.x(), v.y())) + .collect::>() + .join(", ") + ) + } + Geometry::MultiLine(v) => write!( + f, + "{{ type: 'MultiLineString', coordinates: [{}] }}", + v.iter() + .map(|v| format!( + "[{}]", + v.points_iter() + .map(|ref v| format!("[{}, {}]", v.x(), v.y())) + .collect::>() + .join(", ") + )) + .collect::>() + .join(", ") + ), + Geometry::MultiPolygon(v) => write!( + f, + "{{ type: 'MultiPolygon', coordinates: [{}] }}", + v.iter() + .map(|v| format!( + "[[{}]{}]", + v.exterior() + .points_iter() + .map(|ref v| format!("[{}, {}]", v.x(), v.y())) + .collect::>() + .join(", "), + match v.interiors().len() { + 0 => format!(""), + _ => format!( + ", [{}]", + v.interiors() + .iter() + .map(|i| { + format!( + "[{}]", + i.points_iter() + .map(|ref v| format!("[{}, {}]", v.x(), v.y())) + .collect::>() + .join(", ") + ) + }) + .collect::>() + .join(", "), + ), + } + )) + .collect::>() + .join(", "), + ), + Geometry::Collection(v) => { + write!( + f, + "{{ type: 'GeometryCollection', geometries: [{}] }}", + v.iter().map(|v| format!("{}", v)).collect::>().join(", ") + ) + } } } }