terrana 0.1.1

Zero-config spatial API server — point it at a CSV, Parquet, or GeoJSON file and get a REST API with spatial and geometry queries.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! `POST /geometry/*` endpoints — area, convex-hull, centroid, buffer, dissolve,
//! simplify, distance, and bounds. All area/distance/buffer math is geodesic
//! (Karney / WGS 84 via the `geo` crate), never planar.

use crate::db;
use crate::db::query::MAX_RESULT_LIMIT;
use crate::error::AppError;
use crate::server::AppState;
use axum::extract::State;
use axum::Json;
use geo::{
    Bearing, BoundingRect, Centroid, ConvexHull, Destination, Distance, Geodesic, GeodesicArea,
    Simplify, SimplifyVw,
};
use geo_types::{LineString, Point, Polygon};
use serde_json::{json, Value};
use std::convert::TryInto;

pub async fn area(
    State(_state): State<AppState>,
    Json(body): Json<Value>,
) -> Result<Json<Value>, AppError> {
    let polygons = extract_polygons_from_body(&body)?;

    let mut total_area_m2 = 0.0_f64;
    let mut total_perimeter_m = 0.0_f64;

    for poly in &polygons {
        total_area_m2 += poly.geodesic_area_unsigned();
        total_perimeter_m += poly.geodesic_perimeter();
    }

    Ok(Json(json!({
        "area_m2": total_area_m2,
        "area_km2": total_area_m2 / 1_000_000.0,
        "area_ha": total_area_m2 / 10_000.0,
        "area_acres": total_area_m2 / 4_046.8564224,
        "perimeter_m": total_perimeter_m,
    })))
}

pub async fn convex_hull(
    State(state): State<AppState>,
    Json(body): Json<Value>,
) -> Result<Json<Value>, AppError> {
    let points = if let Some(query) = body.get("query") {
        let bbox = query
            .get("bbox")
            .and_then(|v| v.as_array())
            .ok_or_else(|| AppError::BadRequest("Expected query.bbox array".into()))?;
        if bbox.len() != 4 {
            return Err(AppError::BadRequest("bbox must have 4 values".into()));
        }
        let mut coords = [0.0_f64; 4];
        for (i, coord) in coords.iter_mut().enumerate() {
            *coord = bbox[i]
                .as_f64()
                .ok_or_else(|| AppError::BadRequest("bbox values must be numeric".into()))?;
        }
        let (min_lat, min_lon, max_lat, max_lon) = (coords[0], coords[1], coords[2], coords[3]);

        let snap = state.snapshot();
        let raw_pts = db::query::query_points_in_bbox(
            &state.db,
            &snap.schema.lat_col,
            &snap.schema.lon_col,
            min_lat,
            min_lon,
            max_lat,
            max_lon,
        )?;
        raw_pts
            .into_iter()
            .map(|(lat, lon)| Point::new(lon, lat))
            .collect()
    } else {
        extract_points_from_body(&body)?
    };

    if points.len() < 3 {
        return Err(AppError::BadRequest(
            "Need at least 3 points for convex hull".into(),
        ));
    }

    let multi_point = geo_types::MultiPoint::from(points.clone());
    let hull = multi_point.convex_hull();

    let area_m2 = hull.geodesic_area_unsigned();
    let perimeter_m = hull.geodesic_perimeter();

    let hull_geojson: geojson::Geometry = (&hull).into();

    Ok(Json(json!({
        "type": "Feature",
        "geometry": serde_json::to_value(&hull_geojson).unwrap_or(json!(null)),
        "properties": {
            "area_m2": area_m2,
            "area_km2": area_m2 / 1_000_000.0,
            "area_ha": area_m2 / 10_000.0,
            "perimeter_m": perimeter_m,
            "point_count": points.len(),
        }
    })))
}

pub async fn centroid(
    State(_state): State<AppState>,
    Json(body): Json<Value>,
) -> Result<Json<Value>, AppError> {
    let geojson: geojson::GeoJson = body
        .to_string()
        .parse()
        .map_err(|e| AppError::BadRequest(format!("Invalid GeoJSON: {}", e)))?;

    let geom = match geojson {
        geojson::GeoJson::Geometry(g) => g,
        geojson::GeoJson::Feature(f) => f
            .geometry
            .ok_or_else(|| AppError::BadRequest("Feature has no geometry".into()))?,
        _ => return Err(AppError::BadRequest("Expected a single geometry".into())),
    };

    let geo_geom: geo_types::Geometry<f64> = geom
        .try_into()
        .map_err(|e| AppError::Geometry(format!("{}", e)))?;

    let c = geo_geom
        .centroid()
        .ok_or_else(|| AppError::Geometry("Could not compute centroid".into()))?;

    Ok(Json(json!({
        "centroid": {
            "type": "Point",
            "coordinates": [c.x(), c.y()]
        }
    })))
}

pub async fn buffer(
    State(_state): State<AppState>,
    Json(body): Json<Value>,
) -> Result<Json<Value>, AppError> {
    let geom_val = body
        .get("geometry")
        .ok_or_else(|| AppError::BadRequest("Missing 'geometry' field".into()))?;
    let distance = body
        .get("distance")
        .and_then(|v| v.as_f64())
        .ok_or_else(|| AppError::BadRequest("Missing 'distance' field".into()))?;
    let unit = body.get("unit").and_then(|v| v.as_str()).unwrap_or("m");
    let segments = body.get("segments").and_then(|v| v.as_u64()).unwrap_or(64) as usize;

    let distance_m = match unit {
        "km" => distance * 1000.0,
        "mi" => distance * 1609.344,
        "ft" => distance * 0.3048,
        _ => distance,
    };

    let geojson: geojson::Geometry = serde_json::from_value(geom_val.clone())
        .map_err(|e| AppError::BadRequest(format!("Invalid geometry: {}", e)))?;
    let geo_geom: geo_types::Geometry<f64> = geojson
        .try_into()
        .map_err(|e| AppError::Geometry(format!("{}", e)))?;

    let center = geo_geom
        .centroid()
        .ok_or_else(|| AppError::Geometry("Cannot compute centroid for buffer".into()))?;

    let mut coords = Vec::with_capacity(segments + 1);
    for i in 0..segments {
        let bearing = (i as f64) * 360.0 / (segments as f64);
        let dest = Geodesic.destination(center, bearing, distance_m);
        coords.push(geo_types::Coord {
            x: dest.x(),
            y: dest.y(),
        });
    }
    coords.push(coords[0]);

    let ring = LineString::from(coords);
    let poly = Polygon::new(ring, vec![]);

    let area_m2 = poly.geodesic_area_unsigned();
    let poly_geojson: geojson::Geometry = (&poly).into();

    Ok(Json(json!({
        "type": "Feature",
        "geometry": serde_json::to_value(&poly_geojson).unwrap_or(json!(null)),
        "properties": {
            "area_m2": area_m2,
            "area_km2": area_m2 / 1_000_000.0,
            "distance_m": distance_m,
            "segments": segments,
        }
    })))
}

pub async fn dissolve(
    State(state): State<AppState>,
    Json(body): Json<Value>,
) -> Result<Json<Value>, AppError> {
    let by = body
        .get("by")
        .and_then(|v| v.as_str())
        .ok_or_else(|| AppError::BadRequest("Missing 'by' field".into()))?;
    let include_area = body
        .get("include_area")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    let include_count = body
        .get("include_count")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);

    let points_with_data = if let Some(query) = body.get("query") {
        let bbox = query
            .get("bbox")
            .and_then(|v| v.as_array())
            .ok_or_else(|| AppError::BadRequest("Expected query.bbox".into()))?;
        let mut coords = [0.0_f64; 4];
        for (i, coord) in coords.iter_mut().enumerate() {
            *coord = bbox[i]
                .as_f64()
                .ok_or_else(|| AppError::BadRequest("bbox values must be numeric".into()))?;
        }
        let (min_lat, min_lon, max_lat, max_lon) = (coords[0], coords[1], coords[2], coords[3]);

        db::query::query_rows_in_bbox(
            &state.db,
            min_lat,
            min_lon,
            max_lat,
            max_lon,
            MAX_RESULT_LIMIT,
        )?
    } else {
        db::query::query(
            &state.db,
            None,
            &[],
            None,
            None,
            None,
            MAX_RESULT_LIMIT,
            None,
            None,
        )?
    };

    let snap = state.snapshot();
    let lat_col = &snap.schema.lat_col;
    let lon_col = &snap.schema.lon_col;
    let mut groups: std::collections::HashMap<String, Vec<Point<f64>>> =
        std::collections::HashMap::new();

    for row in &points_with_data {
        let key = row
            .get(by)
            .map(|v| match v {
                Value::String(s) => s.clone(),
                other => other.to_string(),
            })
            .unwrap_or_else(|| "null".to_string());

        let lat = row.get(lat_col).and_then(|v| v.as_f64());
        let lon = row.get(lon_col).and_then(|v| v.as_f64());

        if let (Some(lat), Some(lon)) = (lat, lon) {
            groups.entry(key).or_default().push(Point::new(lon, lat));
        }
    }

    let mut features = Vec::new();
    for (key, pts) in &groups {
        if pts.len() < 3 {
            continue;
        }
        let multi_point = geo_types::MultiPoint::from(pts.clone());
        let hull = multi_point.convex_hull();
        let hull_geojson: geojson::Geometry = (&hull).into();

        let mut props = serde_json::Map::new();
        props.insert(by.to_string(), json!(key));
        if include_count {
            props.insert("count".to_string(), json!(pts.len()));
        }
        if include_area {
            let area_m2 = hull.geodesic_area_unsigned();
            props.insert("area_m2".to_string(), json!(area_m2));
            props.insert("area_km2".to_string(), json!(area_m2 / 1_000_000.0));
        }

        features.push(json!({
            "type": "Feature",
            "geometry": serde_json::to_value(&hull_geojson).unwrap_or(json!(null)),
            "properties": props,
        }));
    }

    Ok(Json(json!({
        "type": "FeatureCollection",
        "features": features,
    })))
}

pub async fn simplify(
    State(_state): State<AppState>,
    Json(body): Json<Value>,
) -> Result<Json<Value>, AppError> {
    let geom_val = body
        .get("geometry")
        .ok_or_else(|| AppError::BadRequest("Missing 'geometry' field".into()))?;
    let tolerance = body
        .get("tolerance")
        .and_then(|v| v.as_f64())
        .unwrap_or(0.001);
    let preserve_topology = body
        .get("preserve_topology")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);

    let geojson: geojson::Geometry = serde_json::from_value(geom_val.clone())
        .map_err(|e| AppError::BadRequest(format!("Invalid geometry: {}", e)))?;
    let geo_geom: geo_types::Geometry<f64> = geojson
        .try_into()
        .map_err(|e| AppError::Geometry(format!("{}", e)))?;

    let simplified = match geo_geom {
        geo_types::Geometry::Polygon(p) => {
            if preserve_topology {
                geo_types::Geometry::Polygon(p.simplify_vw(tolerance))
            } else {
                geo_types::Geometry::Polygon(p.simplify(tolerance))
            }
        }
        geo_types::Geometry::MultiPolygon(mp) => {
            if preserve_topology {
                geo_types::Geometry::MultiPolygon(mp.simplify_vw(tolerance))
            } else {
                geo_types::Geometry::MultiPolygon(mp.simplify(tolerance))
            }
        }
        geo_types::Geometry::LineString(ls) => {
            if preserve_topology {
                geo_types::Geometry::LineString(ls.simplify_vw(tolerance))
            } else {
                geo_types::Geometry::LineString(ls.simplify(tolerance))
            }
        }
        other => other,
    };

    let result_geojson: geojson::Geometry = (&simplified).into();

    Ok(Json(json!({
        "geometry": serde_json::to_value(&result_geojson).unwrap_or(json!(null)),
        "tolerance": tolerance,
        "preserve_topology": preserve_topology,
    })))
}

pub async fn distance(
    State(_state): State<AppState>,
    Json(body): Json<Value>,
) -> Result<Json<Value>, AppError> {
    let from_val = body
        .get("from")
        .ok_or_else(|| AppError::BadRequest("Missing 'from' field".into()))?;
    let to_val = body
        .get("to")
        .ok_or_else(|| AppError::BadRequest("Missing 'to' field".into()))?;

    let from_pt = extract_point(from_val, "from")?;
    let to_pt = extract_point(to_val, "to")?;

    let distance_m = Geodesic.distance(from_pt, to_pt);
    let bearing = Geodesic.bearing(from_pt, to_pt);

    Ok(Json(json!({
        "distance_m": distance_m,
        "distance_km": distance_m / 1000.0,
        "distance_mi": distance_m / 1609.344,
        "bearing_deg": bearing,
    })))
}

pub async fn bounds(
    State(_state): State<AppState>,
    Json(body): Json<Value>,
) -> Result<Json<Value>, AppError> {
    let geojson: geojson::GeoJson = body
        .to_string()
        .parse()
        .map_err(|e| AppError::BadRequest(format!("Invalid GeoJSON: {}", e)))?;

    let geom = match geojson {
        geojson::GeoJson::Geometry(g) => g,
        geojson::GeoJson::Feature(f) => f
            .geometry
            .ok_or_else(|| AppError::BadRequest("Feature has no geometry".into()))?,
        _ => return Err(AppError::BadRequest("Expected a single geometry".into())),
    };

    let geo_geom: geo_types::Geometry<f64> = geom
        .try_into()
        .map_err(|e| AppError::Geometry(format!("{}", e)))?;

    let rect = geo_geom
        .bounding_rect()
        .ok_or_else(|| AppError::Geometry("Could not compute bounding rect".into()))?;

    let min_lat = rect.min().y;
    let min_lon = rect.min().x;
    let max_lat = rect.max().y;
    let max_lon = rect.max().x;

    let width_m = Geodesic.distance(Point::new(min_lon, min_lat), Point::new(max_lon, min_lat));
    let height_m = Geodesic.distance(Point::new(min_lon, min_lat), Point::new(min_lon, max_lat));

    let envelope_poly = Polygon::new(
        LineString::from(vec![
            (min_lon, min_lat),
            (max_lon, min_lat),
            (max_lon, max_lat),
            (min_lon, max_lat),
            (min_lon, min_lat),
        ]),
        vec![],
    );
    let envelope_geojson: geojson::Geometry = (&envelope_poly).into();
    let area_m2 = envelope_poly.geodesic_area_unsigned();

    Ok(Json(json!({
        "bbox": [min_lat, min_lon, max_lat, max_lon],
        "envelope": serde_json::to_value(&envelope_geojson).unwrap_or(json!(null)),
        "width_km": width_m / 1000.0,
        "height_km": height_m / 1000.0,
        "area_km2": area_m2 / 1_000_000.0,
    })))
}

// --- Helpers ---

fn extract_polygons_from_body(body: &Value) -> Result<Vec<Polygon<f64>>, AppError> {
    let geojson_val = if let Some(geom) = body.get("geometry") {
        geom.to_string()
    } else {
        body.to_string()
    };
    let geojson: geojson::GeoJson = geojson_val
        .parse()
        .map_err(|e| AppError::BadRequest(format!("Invalid GeoJSON: {}", e)))?;

    let mut polygons = Vec::new();
    match geojson {
        geojson::GeoJson::Geometry(g) => collect_polygons(&g, &mut polygons)?,
        geojson::GeoJson::Feature(f) => {
            if let Some(g) = f.geometry {
                collect_polygons(&g, &mut polygons)?;
            }
        }
        geojson::GeoJson::FeatureCollection(fc) => {
            for f in fc.features {
                if let Some(g) = f.geometry {
                    collect_polygons(&g, &mut polygons)?;
                }
            }
        }
    }

    if polygons.is_empty() {
        return Err(AppError::BadRequest("No polygons found in input".into()));
    }
    Ok(polygons)
}

fn collect_polygons(
    geom: &geojson::Geometry,
    polygons: &mut Vec<Polygon<f64>>,
) -> Result<(), AppError> {
    let geo_geom: geo_types::Geometry<f64> = geom
        .clone()
        .try_into()
        .map_err(|e| AppError::Geometry(format!("{}", e)))?;

    match geo_geom {
        geo_types::Geometry::Polygon(p) => polygons.push(p),
        geo_types::Geometry::MultiPolygon(mp) => polygons.extend(mp.0),
        _ => {
            return Err(AppError::BadRequest(
                "Expected Polygon or MultiPolygon".into(),
            ))
        }
    }
    Ok(())
}

fn extract_point(val: &Value, label: &str) -> Result<Point<f64>, AppError> {
    let geojson: geojson::Geometry = serde_json::from_value(val.clone())
        .map_err(|e| AppError::BadRequest(format!("Invalid '{}' geometry: {}", label, e)))?;
    let geo_geom: geo_types::Geometry<f64> = geojson
        .try_into()
        .map_err(|e| AppError::Geometry(format!("{}", e)))?;

    match geo_geom {
        geo_types::Geometry::Point(p) => Ok(p),
        _ => Err(AppError::BadRequest(format!("'{}' must be a Point", label))),
    }
}

fn extract_points_from_body(body: &Value) -> Result<Vec<Point<f64>>, AppError> {
    let geojson: geojson::GeoJson = body
        .to_string()
        .parse()
        .map_err(|e| AppError::BadRequest(format!("Invalid GeoJSON: {}", e)))?;

    let mut points = Vec::new();
    match geojson {
        geojson::GeoJson::FeatureCollection(fc) => {
            for f in fc.features {
                if let Some(g) = f.geometry {
                    if let Ok(geo_types::Geometry::Point(p)) =
                        TryInto::<geo_types::Geometry<f64>>::try_into(g)
                    {
                        points.push(p);
                    }
                }
            }
        }
        _ => return Err(AppError::BadRequest("Expected FeatureCollection".into())),
    }
    Ok(points)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn geodesic_area_of_unit_box_at_equator() {
        // A 1° x 1° box at the equator is ~12,308 km² on the WGS 84 ellipsoid.
        let body = json!({
            "geometry": {
                "type": "Polygon",
                "coordinates": [[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]]]
            }
        });
        let polys = extract_polygons_from_body(&body).unwrap();
        assert_eq!(polys.len(), 1);
        let area_km2 = polys[0].geodesic_area_unsigned() / 1_000_000.0;
        assert!(
            (12_000.0..12_700.0).contains(&area_km2),
            "expected ~12,308 km², got {area_km2}"
        );
    }

    #[test]
    fn rejects_non_polygon_input() {
        let body = json!({ "type": "Point", "coordinates": [0.0, 0.0] });
        assert!(extract_polygons_from_body(&body).is_err());
    }
}