diff --git a/client/src/assets/constants.ts b/client/src/assets/constants.ts index ab3f7ca7..1142d18d 100644 --- a/client/src/assets/constants.ts +++ b/client/src/assets/constants.ts @@ -209,4 +209,5 @@ export const SORT_BY = [ 'Geohash', 'LatLon', 'PointCount', + 'Hilbert', ] as const diff --git a/server/algorithms/src/routing/hilbert.rs b/server/algorithms/src/routing/hilbert.rs new file mode 100644 index 00000000..fee30a62 --- /dev/null +++ b/server/algorithms/src/routing/hilbert.rs @@ -0,0 +1,15 @@ +use super::sorting::SortS2; +use crate::s2; +use model::api::single_vec::SingleVec; + +pub fn run(points: SingleVec) -> SingleVec { + let mut l15_map: Vec<(u64, SingleVec)> = s2::create_cell_map(&points, 15).into_iter().collect(); + l15_map.sort_by(|a, b| a.0.cmp(&b.0)); + let mut result = vec![]; + + for (_, mut points) in l15_map { + points.sort_s2_mut(); + result.append(&mut points); + } + result +} diff --git a/server/algorithms/src/routing/mod.rs b/server/algorithms/src/routing/mod.rs index 67cb04cf..046c2ea0 100644 --- a/server/algorithms/src/routing/mod.rs +++ b/server/algorithms/src/routing/mod.rs @@ -9,6 +9,7 @@ use crate::{ utils, }; +mod hilbert; mod join; pub mod sorting; // pub mod vrp; @@ -29,6 +30,7 @@ pub fn main( SortBy::GeoHash => clusters.sort_geohash(), SortBy::S2Cell => clusters.sort_s2(), SortBy::Random => clusters.sort_random(), + SortBy::Hilbert => hilbert::run(clusters), SortBy::Unset => clusters, SortBy::Custom(plugin) => { let clusters = clusters.sort_s2(); diff --git a/server/model/src/api/sort_by.rs b/server/model/src/api/sort_by.rs index 4a4fba87..0fdfeacb 100644 --- a/server/model/src/api/sort_by.rs +++ b/server/model/src/api/sort_by.rs @@ -8,6 +8,7 @@ pub enum SortBy { Random, S2Cell, LatLon, + Hilbert, Custom(String), } @@ -42,6 +43,7 @@ impl<'de> Deserialize<'de> for SortBy { "s2" | "s2cell" => Ok(SortBy::S2Cell), "latlon" => Ok(SortBy::LatLon), "" | "none" | "unset" => Ok(SortBy::Unset), + "hilbert" => Ok(SortBy::Hilbert), // This is for backwards compatibility since the custom below would end up with a value of "TSP" "tsp" => Ok(SortBy::Custom("tsp".to_string())), _ => Ok(SortBy::Custom(s)),