use std::collections::HashMap;
use surge_network::Network;
use surge_network::network::model::GeoPoint;
use super::types::ObjMap;
pub(crate) fn build_geo_locations(objects: &ObjMap, network: &mut Network) {
let loc_to_eq: HashMap<String, String> = objects
.iter()
.filter(|(_, o)| o.class == "Location")
.filter_map(|(loc_id, o)| {
o.get_ref("PowerSystemResource")
.map(|eq_id| (loc_id.clone(), eq_id.to_string()))
})
.collect();
let mut points_by_loc: HashMap<String, Vec<(u32, f64, f64)>> = HashMap::new();
for (_, pp_obj) in objects.iter().filter(|(_, o)| o.class == "PositionPoint") {
let Some(loc_id) = pp_obj.get_ref("Location").map(|s| s.to_string()) else {
continue;
};
let seq = pp_obj
.get_text("sequenceNumber")
.and_then(|s| s.parse::<u32>().ok())
.unwrap_or(0);
let x = pp_obj.parse_f64("xPosition").unwrap_or(0.0);
let y = pp_obj.parse_f64("yPosition").unwrap_or(0.0);
points_by_loc.entry(loc_id).or_default().push((seq, x, y));
}
for (loc_id, mut pts) in points_by_loc {
let Some(eq_id) = loc_to_eq.get(&loc_id) else {
continue;
};
pts.sort_by_key(|(seq, _, _)| *seq);
let coords: Vec<GeoPoint> = pts.into_iter().map(|(_, x, y)| GeoPoint { x, y }).collect();
network.cim.geo_locations.insert(eq_id.clone(), coords);
}
}