hash_rstar 0.1.0

A concurrent spatial index combining geohash and R-tree data structures for efficient geographic point queries.
Documentation
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! # hash-rstar
//!
//! A concurrent spatial index combining geohash and R-tree data structures for efficient
//! geographic point queries. This library provides thread-safe operations and optional
//! persistence support.
//!
//! ## Features
//!
//! - Concurrent spatial indexing using thread-safe DashMap
//! - Geohash-based spatial partitioning
//! - R-tree for efficient spatial queries within partitions
//! - Optional persistence using sled database
//! - Two nearest neighbor search strategies
//! - Async loading support
//!
//! ## Usage
//!
//! ```rust
//! use hash_rstar::{AABB, GeohashRTree, GeohashRTreeObject, PointDistance, RTreeObject};
//! use geo::{Distance, Haversine};
//!
//! #[derive(Debug, PartialEq, Clone, bincode::Encode, bincode::Decode)]
//! struct Location {
//!     id: String,
//!     x_coordinate: f64,
//!     y_coordinate: f64,
//! }
//!
//! impl GeohashRTreeObject for Location {
//!     fn unique_id(&self) -> String {
//!         self.id.clone()
//!     }
//!
//!     fn x_y(&self) -> (f64, f64) {
//!         (self.x_coordinate, self.y_coordinate)
//!     }
//! }
//!
//! impl RTreeObject for Location {
//!     type Envelope = AABB<[f64; 2]>;

//!     fn envelope(&self) -> Self::Envelope {
//!         AABB::from_point([self.x_coordinate, self.y_coordinate])
//!     }
//! }
//!
//! impl PointDistance for Location {
//!     fn distance_2(&self, point: &[f64; 2]) -> f64 {
//!         let self_geo_point = geo::point!(x: self.x_coordinate, y: self.y_coordinate);
//!         let target_geo_point = geo::point!(x: point[0], y: point[1]);
//!         Haversine::distance(self_geo_point, target_geo_point)
//!     }
//! }
//!
//! let tree: GeohashRTree<Location> = GeohashRTree::new(5, None).unwrap();
//!
//! let location = Location {
//!     id: "1".into(),
//!     x_coordinate: 116.400357,
//!     y_coordinate: 39.906453,
//! };
//!
//! tree.insert(location.clone()).unwrap();
//!
//! if let Some(nearest) = tree.adjacent_cells_nearest(&location).unwrap() {
//!     println!("Found nearest point: {:?}", nearest);
//! }
//! ```
//!
//! ## Search Strategies
//!
//! The library provides two nearest neighbor search strategies:
//!
//! 1. `adjacent_cells_nearest`: Fast approximate search
//!    - Searches current and 8 adjacent geohash cells
//!    - Best for evenly distributed points
//!    - Quickest response time
//!
//! 2. `sorted_cells_nearest`: Exact search
//!    - Searches cells in order of increasing distance
//!    - Guarantees finding true nearest neighbor
//!    - Best for unevenly distributed points
//!
//! ## Persistence
//!
//! Optional persistence is supported using sled database:
//!
//! ```rust
//! use std::path::PathBuf;
//!
//! let tree = GeohashRTree::new(6, Some(PathBuf::from("data.db"))).unwrap();
//! ```
//!
//! ## Thread Safety
//!
//! All operations are thread-safe, allowing concurrent access from multiple threads:
//!
//! - Concurrent reads and writes using DashMap
//! - Thread-safe persistence with Arc<sled::Db>
//! - Async loading support
//!
//! ## Performance Considerations
//!
//! - Geohash precision affects partition size and query performance
//! - Higher precision = smaller cells = more precise but slower searches
//! - Lower precision = larger cells = faster but less precise searches
//! - Choose based on data distribution and query patterns
use dashmap::DashMap;
use geo::{Distance, Haversine};
use geohash::{Coord, encode};
use rstar::{Envelope, RTree};
use std::cmp::Ordering;
use std::thread;
use std::time::SystemTime;
use std::{path::PathBuf, sync::Arc, vec};
use thiserror::Error;

pub use rstar::{AABB, PointDistance, RTreeObject};

mod utils;

pub trait GeohashRTreeObject:
    RTreeObject
    + PointDistance
    + PartialEq
    + Clone
    + Send
    + Sync
    + bincode::Decode<()>
    + bincode::Encode
    + 'static
{
    fn unique_id(&self) -> String;

    fn x_y(&self) -> (f64, f64);

    /// Generates a geohash string representation of the point with specified precision.
    ///
    /// # Arguments
    /// * `geohash_precision` - The length of the geohash string to generate
    ///
    /// # Returns
    /// * `Ok(String)` - The geohash string representation
    /// * `Err(GeohashRTreeError)` - If geohash encoding fails
    fn gen_geohash_str(&self, geohash_precision: usize) -> Result<String, GeohashRTreeError> {
        let (x, y) = self.x_y();
        let geohash_str = encode(Coord { x, y }, geohash_precision)?;
        Ok(geohash_str)
    }
}

#[derive(Error, Debug)]
pub enum GeohashRTreeError {
    #[error("persistence file not found, {0:?}")]
    LoadError(#[from] sled::Error),
    #[error("geohash error, {0:?}")]
    GeohashError(#[from] geohash::GeohashError),
    #[error("bincode decode error, {0:?}")]
    BincodeDecodeError(#[from] bincode::error::DecodeError),
    #[error("bincode encode error, {0:?}")]
    BincodeEncodeError(#[from] bincode::error::EncodeError),
    #[error("unknown error")]
    Unknown,
}

/// A concurrent geohash-based R-tree structure for spatial indexing.
///
/// This structure combines geohashing with R-trees to provide efficient spatial queries
/// by partitioning spatial data into geohash cells, each containing an R-tree.
///
/// # Type Parameters
///
/// * `T` - The type of points stored in the tree, must implement required traits for
///         spatial operations, serialization, and thread safety.
///
/// # Fields
///
/// * `arc_dashmap` - Thread-safe concurrent map storing R-trees for each geohash cell
/// * `geohash_precision` - Precision level used for geohash encoding
/// * `db` - Optional persistent storage backend using sled database
pub struct GeohashRTree<T>
where
    T: GeohashRTreeObject,
    T::Envelope: Send + Sync,
{
    arc_dashmap: Arc<DashMap<String, RTree<T>>>,
    geohash_precision: usize,
    db: Option<Arc<sled::Db>>,
}

impl<T> GeohashRTree<T>
where
    T: GeohashRTreeObject,
    T::Envelope: Send + Sync,
{
    const DEFAULT_SLED_CACHE_CAPACITY: u64 = 1024 * 1024 * 100; // 100M

    pub fn new(
        geohash_precision: usize,
        persistence_path: Option<PathBuf>,
    ) -> Result<Self, GeohashRTreeError> {
        let mut s = Self {
            arc_dashmap: Arc::new(DashMap::new()),
            geohash_precision,
            db: None,
        };
        if let Some(persistence_path) = persistence_path {
            let db: sled::Db = sled::Config::default()
                .cache_capacity(Self::DEFAULT_SLED_CACHE_CAPACITY)
                .path(persistence_path)
                .open()?;
            s.db = Some(Arc::new(db));
        }
        Ok(s)
    }

    /// Loads a GeohashRTree from a persistence path with the specified geohash precision.
    ///
    /// # Arguments
    ///
    /// * `geohash_precision` - The precision level for geohash calculations
    /// * `persistence_path` - Path to the persistent storage location
    ///
    /// # Returns
    ///
    /// Returns a `Result` containing either the loaded GeohashRTree or a `GeohashRTreeError`
    pub fn load(
        geohash_precision: usize,
        persistence_path: PathBuf,
    ) -> Result<Self, GeohashRTreeError> {
        let db: sled::Db = sled::Config::default()
            .cache_capacity(Self::DEFAULT_SLED_CACHE_CAPACITY)
            .path(persistence_path)
            .open()?;
        let hrt = Self {
            arc_dashmap: Arc::new(DashMap::new()),
            geohash_precision,
            db: Some(Arc::new(db)),
        };

        let config = bincode::config::standard();

        if let Some(db) = &hrt.db {
            let now = SystemTime::now();
            let mut itr = db.iter();
            while let Some(entry) = itr.next() {
                let (_, value) = entry?;
                let (t, _) = bincode::decode_from_slice::<T, _>(&value, config)?;
                let geohash_str = t.gen_geohash_str(geohash_precision)?;
                hrt.arc_dashmap
                    .entry(geohash_str)
                    .or_insert(RTree::new())
                    .insert(t);
            }
            println!(
                "loaded elapsed time: {:?}, total: {}",
                now.elapsed().unwrap(),
                db.len()
            );
        }
        Ok(hrt)
    }

    /// Asynchronously loads geospatial data from a persistent storage into a Geohash R-tree structure.
    ///
    /// # Arguments
    ///
    /// * `geohash_precision` - The precision level for geohash calculations
    /// * `persistence_path` - Path to the persistent storage location
    ///
    /// # Returns
    ///
    /// * `Result<Self, GeohashRTreeError>` - Returns the initialized structure on success, or an error
    ///
    /// # Examples
    ///
    /// ```
    /// let hrt = GeohashRTree::load_async(5, PathBuf::from("data/store"))?;
    /// ```
    ///
    /// The function spawns a background thread to load data from sled database,
    /// allowing the main thread to continue execution while data is being loaded.
    pub fn load_async(
        geohash_precision: usize,
        persistence_path: PathBuf,
    ) -> Result<Self, GeohashRTreeError> {
        let db: sled::Db = sled::Config::default().path(persistence_path).open()?;
        let hrt = Self {
            arc_dashmap: Arc::new(DashMap::new()),
            geohash_precision,
            db: Some(Arc::new(db)),
        };
        let acr_dashmap = Arc::clone(&hrt.arc_dashmap);

        if let Some(db) = hrt.db.clone() {
            // Load the data from the persistence path
            thread::spawn(move || {
                let config = bincode::config::standard();
                let mut itr = db.iter();
                let now = SystemTime::now();
                while let Some(entry) = itr.next() {
                    let (_, value) = match entry {
                        Ok((uid, value)) => (uid, value),
                        Err(e) => {
                            println!("error: {}", e);
                            return ();
                        }
                    };
                    let t = match bincode::decode_from_slice::<T, _>(&value, config) {
                        Ok((t, _)) => t,
                        Err(e) => {
                            println!("error: {}", e);
                            return ();
                        }
                    };
                    let geohash_str = match t.gen_geohash_str(geohash_precision) {
                        Ok(h) => h,
                        Err(e) => {
                            println!("error: {}", e);
                            return ();
                        }
                    };
                    acr_dashmap
                        .entry(geohash_str)
                        .or_insert(RTree::new())
                        .insert(t);
                }

                println!(
                    "loaded elapsed time: {:?}, total: {}",
                    now.elapsed().unwrap(),
                    db.len()
                );
            });
        }

        Ok(hrt)
    }

    pub fn insert(&self, t: T) -> Result<(), GeohashRTreeError> {
        let geohash_str = t.gen_geohash_str(self.geohash_precision)?;
        if let Some(db) = &self.db {
            let enc = bincode::encode_to_vec(&t, bincode::config::standard())?;
            db.insert(t.unique_id(), enc)?;
        }
        let mut rtree = self.arc_dashmap.entry(geohash_str).or_insert(RTree::new());
        rtree.insert(t);
        Ok(())
    }

    pub fn remove(&self, t: &T) -> Result<Option<T>, GeohashRTreeError> {
        let geohash_str = t.gen_geohash_str(self.geohash_precision)?;
        if let Some(mut rtree) = self.arc_dashmap.get_mut(&geohash_str) {
            if let Some(rm) = rtree.remove(t) {
                if let Some(db) = &self.db {
                    db.remove(rm.unique_id())?;
                }
                return Ok(Some(rm));
            }
        }

        Ok(None)
    }

    /// use only db exist
    pub fn delete(&self, id: &str) -> Result<Option<T>, GeohashRTreeError> {
        if let Some(db) = &self.db {
            if let Some(rm) = db.remove(id)? {
                let (t, _) = bincode::decode_from_slice::<T, _>(&rm, bincode::config::standard())?;
                let geohash_str = t.gen_geohash_str(self.geohash_precision)?;
                if let Some(mut rtree) = self.arc_dashmap.get_mut(&geohash_str) {
                    let _ = rtree.remove(&t);
                }
                return Ok(Some(t));
            }
        }
        Ok(None)
    }

    /// Finds the nearest neighbor by searching in adjacent geohash cells.
    ///
    /// This method searches for the nearest neighbor in the current geohash cell and its 8 adjacent cells.
    /// It's efficient when the target point is likely to be in neighboring cells, but may miss closer
    /// points that lie just outside the adjacent cells.
    ///
    /// # Algorithm
    /// 1. Searches in the current geohash cell
    /// 2. Searches in all 8 adjacent cells (N, NE, E, SE, S, SW, W, NW)
    /// 3. Returns the closest point among all found points
    ///
    /// # Arguments
    /// * `query_point` - The point to find the nearest neighbor for
    ///
    /// # Returns
    /// * `Ok(Some(T))` - The nearest neighbor if found
    /// * `Ok(None)` - If no neighbor is found
    /// * `Err(GeohashRTreeError)` - If an error occurs during geohash operations
    ///
    /// # Example
    /// ```
    /// let tree = GeohashRTree::new(6, None)?;
    /// let point = Point::new(longitude, latitude);
    /// if let Ok(Some(nearest)) = tree.adjacent_cells_nearest(&point) {
    ///     println!("Found nearest point: {:?}", nearest);
    /// }
    /// ```
    ///
    /// # Use Case
    /// Best suited for scenarios where:
    /// - Points are relatively evenly distributed
    /// - Quick approximate results are acceptable
    /// - The target point is likely close to existing points
    pub fn adjacent_cells_nearest(&self, query_point: &T) -> Result<Option<T>, GeohashRTreeError> {
        let geohash_str = query_point.gen_geohash_str(self.geohash_precision)?;
        let mut nearests = vec![];
        if let Some(nearest) = self.nearest(query_point, &geohash_str)? {
            nearests.push(nearest);
        }
        let neighbors = geohash::neighbors(&geohash_str)?;
        if let Some(nearest) = self.nearest(query_point, &neighbors.n)? {
            nearests.push(nearest);
        }
        if let Some(nearest) = self.nearest(query_point, &neighbors.ne)? {
            nearests.push(nearest);
        }
        if let Some(nearest) = self.nearest(query_point, &neighbors.e)? {
            nearests.push(nearest);
        }
        if let Some(nearest) = self.nearest(query_point, &neighbors.se)? {
            nearests.push(nearest);
        }
        if let Some(nearest) = self.nearest(query_point, &neighbors.s)? {
            nearests.push(nearest);
        }
        if let Some(nearest) = self.nearest(query_point, &neighbors.sw)? {
            nearests.push(nearest);
        }
        if let Some(nearest) = self.nearest(query_point, &neighbors.w)? {
            nearests.push(nearest);
        }
        if let Some(nearest) = self.nearest(query_point, &neighbors.nw)? {
            nearests.push(nearest);
        }
        nearests.sort_by(|a, b| {
            let a_distance = a.distance_2(&query_point.envelope().center());
            let b_distance = b.distance_2(&query_point.envelope().center());
            a_distance
                .partial_cmp(&b_distance)
                .unwrap_or(Ordering::Equal)
        });
        Ok(nearests.first().cloned())
    }

    /// Finds the nearest point to the given query point within the specified geohash region.
    ///
    /// # Arguments
    ///
    /// * `query_point` - The reference point to find the nearest neighbor for
    /// * `geohash_str` - The geohash string representing the region to search in
    ///
    /// # Returns
    ///
    /// * `Ok(Some(T))` - The nearest point if found
    /// * `Ok(None)` - If no points exist in the specified geohash region
    /// * `Err(GeohashRTreeError)` - If an error occurs during the search
    fn nearest(&self, query_point: &T, geohash_str: &str) -> Result<Option<T>, GeohashRTreeError> {
        if let Some(rtree) = self.arc_dashmap.get(geohash_str) {
            let nearest = rtree.nearest_neighbor(&query_point.envelope().center());
            return Ok(nearest.cloned());
        }
        Ok(None)
    }

    /// Finds the nearest neighbor using distance-sorted geohash cells.
    ///
    /// This method sorts all possible geohash cells by their minimum distance from the query point
    /// and searches them in order. It guarantees finding the true nearest neighbor but may be slower
    /// than `adjacent_cells_nearest` when searching large areas.
    ///
    /// # Algorithm
    /// 1. Sorts geohash cells by minimum distance from query point
    /// 2. Searches cells in order of increasing distance
    /// 3. Early terminates when finding a point closer than the next cell's minimum distance
    ///
    /// # Arguments
    /// * `query_point` - The point to find the nearest neighbor for
    ///
    /// # Returns
    /// * `Ok(Some(T))` - The nearest neighbor if found
    /// * `Ok(None)` - If no neighbor is found
    /// * `Err(GeohashRTreeError)` - If an error occurs during operations
    ///
    /// # Example
    /// ```
    /// let tree = GeohashRTree::new(5, None)?;
    /// let point = Point::new(longitude, latitude);
    /// if let Ok(Some(nearest)) = tree.sorted_cells_nearest(&point) {
    ///     println!("Found nearest point: {:?}", nearest);
    /// }
    /// ```
    ///
    /// # Use Case
    /// Best suited for scenarios where:
    /// - Points are unevenly distributed
    /// - Exact nearest neighbor is required
    /// - Performance is less critical than accuracy
    /// - Large search areas need to be covered
    pub fn sorted_cells_nearest(&self, query: &T) -> Result<Option<T>, GeohashRTreeError> {
        let (x, y) = query.x_y();
        let point = geo::point!(x: x, y: y);
        let sorted_geohash_cells = utils::sort_geohash_neighbors(point, self.geohash_precision)?;

        for idx in 0..sorted_geohash_cells.len() {
            let cursor = sorted_geohash_cells.get(idx).unwrap();
            if let Some(nearest) = self.nearest(query, &cursor.0)? {
                if let Some(next) = sorted_geohash_cells.get(idx + 1) {
                    let nearest_p = nearest.x_y();
                    let dist =
                        Haversine::distance(point, geo::point!(x: nearest_p.0 , y: nearest_p.1));
                    if dist <= next.1 {
                        return Ok(Some(nearest));
                    }
                } else {
                    return Ok(Some(nearest));
                }
            }
        }

        Ok(None)
    }

    pub fn len(&self) -> usize {
        self.arc_dashmap.len()
    }
}

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

    #[derive(Debug, PartialEq, Clone, bincode::Encode, bincode::Decode)]
    struct Location {
        id: String,
        x_coordinate: f64,
        y_coordinate: f64,
    }

    impl GeohashRTreeObject for Location {
        fn unique_id(&self) -> String {
            self.id.clone()
        }

        fn x_y(&self) -> (f64, f64) {
            (self.x_coordinate, self.y_coordinate)
        }
    }

    impl RTreeObject for Location {
        type Envelope = AABB<[f64; 2]>;

        fn envelope(&self) -> Self::Envelope {
            AABB::from_point([self.x_coordinate, self.y_coordinate])
        }
    }

    impl PointDistance for Location {
        fn distance_2(&self, point: &[f64; 2]) -> f64 {
            let self_geo_point = geo::point!(x: self.x_coordinate, y: self.y_coordinate);
            let target_geo_point = geo::point!(x: point[0], y: point[1]);
            Haversine::distance(self_geo_point, target_geo_point)
        }
    }

    #[test]
    fn test_insert() {
        let hrt: GeohashRTree<Location> = GeohashRTree::new(5, None).unwrap();
        let locations = vec![
            Location {
                id: "1".into(),
                x_coordinate: 116.400357,
                y_coordinate: 39.906453,
            },
            Location {
                id: "2".into(),
                x_coordinate: 116.401633,
                y_coordinate: 39.906302,
            },
            Location {
                id: "3".into(),
                x_coordinate: 116.401645,
                y_coordinate: 39.904753,
            },
        ];
        hrt.insert(locations[0].clone()).unwrap();
        hrt.insert(locations[1].clone()).unwrap();
        hrt.insert(locations[2].clone()).unwrap();

        let nearest = hrt
            .adjacent_cells_nearest(&Location {
                id: "1".into(),
                x_coordinate: 116.400357,
                y_coordinate: 39.906453,
            })
            .unwrap()
            .unwrap();

        assert_eq!(locations[0], nearest);
    }

    #[test]
    fn test_remove() {
        let hrt: GeohashRTree<Location> = GeohashRTree::new(5, None).unwrap();
        let locations = vec![
            Location {
                id: "1".into(),
                x_coordinate: 116.400357,
                y_coordinate: 39.906453,
            },
            Location {
                id: "2".into(),
                x_coordinate: 116.401633,
                y_coordinate: 39.906302,
            },
            Location {
                id: "3".into(),
                x_coordinate: 116.401645,
                y_coordinate: 39.904753,
            },
        ];
        hrt.insert(locations[0].clone()).unwrap();
        hrt.insert(locations[1].clone()).unwrap();
        hrt.insert(locations[2].clone()).unwrap();

        assert_eq!(locations[0], hrt.remove(&locations[0]).unwrap().unwrap());
        assert_eq!(locations[1], hrt.remove(&locations[1]).unwrap().unwrap());
        assert_eq!(locations[2], hrt.remove(&locations[2]).unwrap().unwrap());
    }

    #[test]
    fn test_sorted_cells_nearest() {
        let hrt: GeohashRTree<Location> = GeohashRTree::new(5, None).unwrap();
        let locations = vec![
            Location {
                id: "1".into(),
                x_coordinate: 116.400357,
                y_coordinate: 39.906453,
            },
            Location {
                id: "2".into(),
                x_coordinate: 116.401633,
                y_coordinate: 39.906302,
            },
            Location {
                id: "3".into(),
                x_coordinate: 116.401645,
                y_coordinate: 39.904753,
            },
        ];
        hrt.insert(locations[0].clone()).unwrap();
        hrt.insert(locations[1].clone()).unwrap();
        hrt.insert(locations[2].clone()).unwrap();

        let nearest = hrt
            .sorted_cells_nearest(&Location {
                id: "1".into(),
                x_coordinate: 116.400357,
                y_coordinate: 39.906453,
            })
            .unwrap()
            .unwrap();

        assert_eq!(locations[0], nearest);
    }
}