use crate::geometry::hull::compute_convex_hull;
use geo_types::Point;
use serde_json::{json, Value};
use std::collections::HashMap;
pub fn dissolve_by(
groups: &HashMap<String, Vec<Point<f64>>>,
by: &str,
include_area: bool,
include_count: bool,
) -> Vec<Value> {
let mut features = Vec::new();
for (key, pts) in groups {
if pts.len() < 3 {
continue;
}
let result = compute_convex_hull(pts);
let hull_geojson: geojson::Geometry = (&result.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 {
props.insert("area_m2".to_string(), json!(result.area_m2));
props.insert("area_km2".to_string(), json!(result.area_km2));
}
features.push(json!({
"type": "Feature",
"geometry": serde_json::to_value(&hull_geojson).unwrap_or(json!(null)),
"properties": props,
}));
}
features
}