use meters in the filters

This commit is contained in:
Tamo 2021-09-07 12:11:03 +02:00
parent 4f69b190bc
commit e5ef0cad9a
No known key found for this signature in database
GPG Key ID: 20CD8020AFA88D69
3 changed files with 17 additions and 5 deletions

View File

@ -16,6 +16,7 @@ flate2 = "1.0.20"
fst = "0.4.5" fst = "0.4.5"
fxhash = "0.2.1" fxhash = "0.2.1"
grenad = { version = "0.3.1", default-features = false, features = ["tempfile"] } grenad = { version = "0.3.1", default-features = false, features = ["tempfile"] }
haversine = "0.2.1"
heed = { git = "https://github.com/Kerollmops/heed", tag = "v0.12.1", default-features = false, features = ["lmdb", "sync-read-txn"] } heed = { git = "https://github.com/Kerollmops/heed", tag = "v0.12.1", default-features = false, features = ["lmdb", "sync-read-txn"] }
human_format = "1.0.3" human_format = "1.0.3"
levenshtein_automata = { version = "0.2.0", features = ["fst_automaton"] } levenshtein_automata = { version = "0.2.0", features = ["fst_automaton"] }

View File

@ -142,6 +142,14 @@ where
Some((head, tail)) Some((head, tail))
} }
/// Return the distance between two points in meters
fn distance_between_two_points(a: &[f64; 2], b: &[f64; 2]) -> f64 {
let a = haversine::Location { latitude: a[0], longitude: a[1] };
let b = haversine::Location { latitude: b[0], longitude: b[1] };
haversine::distance(a, b, haversine::Units::Kilometers) * 1000.
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::json; use serde_json::json;

View File

@ -504,17 +504,20 @@ impl FilterCondition {
LowerThan(val) => (Included(f64::MIN), Excluded(*val)), LowerThan(val) => (Included(f64::MIN), Excluded(*val)),
LowerThanOrEqual(val) => (Included(f64::MIN), Included(*val)), LowerThanOrEqual(val) => (Included(f64::MIN), Included(*val)),
Between(left, right) => (Included(*left), Included(*right)), Between(left, right) => (Included(*left), Included(*right)),
GeoLowerThan(point, distance) => { GeoLowerThan(base_point, distance) => {
let mut result = RoaringBitmap::new(); let mut result = RoaringBitmap::new();
let rtree = match index.geo_rtree(rtxn)? { let rtree = match index.geo_rtree(rtxn)? {
Some(rtree) => rtree, Some(rtree) => rtree,
None => return Ok(result), None => return Ok(result),
}; };
let iter = rtree rtree
.nearest_neighbor_iter_with_distance_2(point) .nearest_neighbor_iter(base_point)
.take_while(|(_, dist)| dist <= distance); .take_while(|point| {
iter.for_each(|(point, _)| drop(result.insert(point.data))); dbg!(crate::distance_between_two_points(base_point, point.geom()))
< *distance
})
.for_each(|point| drop(result.insert(point.data)));
return Ok(result); return Ok(result);
} }