MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / geometry_to_value

Function geometry_to_value

nodedb/src/data/executor/handlers/spatial_sync.rs:205–268  ·  view source on GitHub ↗

Convert a `Geometry` into a `nodedb_types::Value` for msgpack serialisation. Produces a JSON-compatible nested structure matching what a pgwire INSERT with a GeoJSON geometry value would produce.

(geometry: &Geometry)

Source from the content-addressed store, hash-verified

203/// Produces a JSON-compatible nested structure matching what a pgwire INSERT
204/// with a GeoJSON geometry value would produce.
205fn geometry_to_value(geometry: &Geometry) -> nodedb_types::Value {
206 use nodedb_types::Value;
207 use nodedb_types::geometry::Geometry::*;
208
209 match geometry {
210 Point { coordinates } => {
211 let mut m = std::collections::HashMap::new();
212 m.insert("type".to_string(), Value::String("Point".to_string()));
213 m.insert(
214 "coordinates".to_string(),
215 Value::Array(vec![
216 Value::Float(coordinates[0]),
217 Value::Float(coordinates[1]),
218 ]),
219 );
220 Value::Object(m)
221 }
222 LineString { coordinates } => {
223 let mut m = std::collections::HashMap::new();
224 m.insert("type".to_string(), Value::String("LineString".to_string()));
225 m.insert(
226 "coordinates".to_string(),
227 Value::Array(
228 coordinates
229 .iter()
230 .map(|c| Value::Array(vec![Value::Float(c[0]), Value::Float(c[1])]))
231 .collect(),
232 ),
233 );
234 Value::Object(m)
235 }
236 Polygon { coordinates } => {
237 let mut m = std::collections::HashMap::new();
238 m.insert("type".to_string(), Value::String("Polygon".to_string()));
239 m.insert(
240 "coordinates".to_string(),
241 Value::Array(
242 coordinates
243 .iter()
244 .map(|ring| {
245 Value::Array(
246 ring.iter()
247 .map(|c| {
248 Value::Array(vec![Value::Float(c[0]), Value::Float(c[1])])
249 })
250 .collect(),
251 )
252 })
253 .collect(),
254 ),
255 );
256 Value::Object(m)
257 }
258 // For other geometry types, fall back to JSON serialisation then
259 // parse as Value. These are less common in sync workloads.
260 other => match sonic_rs::to_string(other) {
261 Ok(json) => match sonic_rs::from_str::<serde_json::Value>(&json) {
262 Ok(v) => nodedb_types::conversion::json_to_value(v),

Callers 1

Calls 5

to_stringMethod · 0.80
collectMethod · 0.80
json_to_valueFunction · 0.50
insertMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected