|
| 1 | +//! Example demonstrating different distance metrics for spatial queries. |
| 2 | +//! |
| 3 | +//! This example shows how to use Euclidean, Haversine, and Spheroid distance metrics |
| 4 | +//! for finding nearest neighbors in spatial datasets. |
| 5 | +
|
| 6 | +use geo_index::rtree::distance::{EuclideanDistance, HaversineDistance, SpheroidDistance}; |
| 7 | +use geo_index::rtree::sort::HilbertSort; |
| 8 | +use geo_index::rtree::{RTreeBuilder, RTreeIndex}; |
| 9 | + |
| 10 | +fn main() { |
| 11 | + println!("=== Distance Metrics Example ===\n"); |
| 12 | + |
| 13 | + // Example 1: Euclidean Distance (for planar coordinates) |
| 14 | + println!("1. Euclidean Distance (planar coordinates):"); |
| 15 | + euclidean_distance_example(); |
| 16 | + |
| 17 | + // Example 2: Haversine Distance (for geographic coordinates) |
| 18 | + println!("\n2. Haversine Distance (geographic coordinates):"); |
| 19 | + haversine_distance_example(); |
| 20 | + |
| 21 | + // Example 3: Spheroid Distance (for high-precision geographic coordinates) |
| 22 | + println!("\n3. Spheroid Distance (high-precision geographic coordinates):"); |
| 23 | + spheroid_distance_example(); |
| 24 | + |
| 25 | + // Example 4: Comparison of different metrics |
| 26 | + println!("\n4. Comparison of different distance metrics:"); |
| 27 | + comparison_example(); |
| 28 | +} |
| 29 | + |
| 30 | +fn euclidean_distance_example() { |
| 31 | + let mut builder = RTreeBuilder::<f64>::new(4); |
| 32 | + |
| 33 | + // Add some points in a planar coordinate system |
| 34 | + builder.add(0., 0., 1., 1.); // Point A |
| 35 | + builder.add(3., 4., 4., 5.); // Point B |
| 36 | + builder.add(6., 8., 7., 9.); // Point C |
| 37 | + builder.add(1., 1., 2., 2.); // Point D |
| 38 | + |
| 39 | + let tree = builder.finish::<HilbertSort>(); |
| 40 | + |
| 41 | + let euclidean = EuclideanDistance; |
| 42 | + let query_point = (0., 0.); |
| 43 | + |
| 44 | + println!(" Query point: {:?}", query_point); |
| 45 | + let results = |
| 46 | + tree.neighbors_with_distance(query_point.0, query_point.1, Some(3), None, &euclidean); |
| 47 | + |
| 48 | + println!(" Nearest neighbors (by insertion order): {:?}", results); |
| 49 | + println!(" Distance metric: Euclidean (straight-line distance)"); |
| 50 | +} |
| 51 | + |
| 52 | +fn haversine_distance_example() { |
| 53 | + let mut builder = RTreeBuilder::<f64>::new(5); |
| 54 | + |
| 55 | + // Add some major cities (longitude, latitude) |
| 56 | + builder.add(-74.0, 40.7, -74.0, 40.7); // New York |
| 57 | + builder.add(-0.1, 51.5, -0.1, 51.5); // London |
| 58 | + builder.add(139.7, 35.7, 139.7, 35.7); // Tokyo |
| 59 | + builder.add(-118.2, 34.1, -118.2, 34.1); // Los Angeles |
| 60 | + builder.add(2.3, 48.9, 2.3, 48.9); // Paris |
| 61 | + |
| 62 | + let tree = builder.finish::<HilbertSort>(); |
| 63 | + |
| 64 | + let haversine = HaversineDistance::default(); |
| 65 | + let query_point = (-74.0, 40.7); // New York |
| 66 | + |
| 67 | + println!(" Query point: New York {:?}", query_point); |
| 68 | + let results = |
| 69 | + tree.neighbors_with_distance(query_point.0, query_point.1, Some(3), None, &haversine); |
| 70 | + |
| 71 | + println!(" Nearest neighbors (by insertion order): {:?}", results); |
| 72 | + println!(" Distance metric: Haversine (great-circle distance on sphere)"); |
| 73 | + println!(" Earth radius: {} meters", haversine.earth_radius); |
| 74 | +} |
| 75 | + |
| 76 | +fn spheroid_distance_example() { |
| 77 | + let mut builder = RTreeBuilder::<f64>::new(5); |
| 78 | + |
| 79 | + // Add some major cities (longitude, latitude) |
| 80 | + builder.add(-74.0, 40.7, -74.0, 40.7); // New York |
| 81 | + builder.add(-0.1, 51.5, -0.1, 51.5); // London |
| 82 | + builder.add(139.7, 35.7, 139.7, 35.7); // Tokyo |
| 83 | + builder.add(-118.2, 34.1, -118.2, 34.1); // Los Angeles |
| 84 | + builder.add(2.3, 48.9, 2.3, 48.9); // Paris |
| 85 | + |
| 86 | + let tree = builder.finish::<HilbertSort>(); |
| 87 | + |
| 88 | + let spheroid = SpheroidDistance::default(); // WGS84 ellipsoid |
| 89 | + let query_point = (-74.0, 40.7); // New York |
| 90 | + |
| 91 | + println!(" Query point: New York {:?}", query_point); |
| 92 | + let results = |
| 93 | + tree.neighbors_with_distance(query_point.0, query_point.1, Some(3), None, &spheroid); |
| 94 | + |
| 95 | + println!(" Nearest neighbors (by insertion order): {:?}", results); |
| 96 | + println!(" Distance metric: Spheroid (distance on ellipsoid)"); |
| 97 | + println!(" Semi-major axis: {} meters", spheroid.semi_major_axis); |
| 98 | + println!(" Semi-minor axis: {} meters", spheroid.semi_minor_axis); |
| 99 | +} |
| 100 | + |
| 101 | +fn comparison_example() { |
| 102 | + let mut builder = RTreeBuilder::<f64>::new(3); |
| 103 | + |
| 104 | + // Add points with different characteristics |
| 105 | + builder.add(0., 0., 1., 1.); // Origin |
| 106 | + builder.add(1., 1., 2., 2.); // Close point |
| 107 | + builder.add(10., 10., 11., 11.); // Distant point |
| 108 | + |
| 109 | + let tree = builder.finish::<HilbertSort>(); |
| 110 | + |
| 111 | + let query_point = (0., 0.); |
| 112 | + |
| 113 | + // Test with different distance metrics |
| 114 | + let euclidean = EuclideanDistance; |
| 115 | + let haversine = HaversineDistance::default(); |
| 116 | + let spheroid = SpheroidDistance::default(); |
| 117 | + |
| 118 | + println!(" Query point: {:?}", query_point); |
| 119 | + |
| 120 | + let euclidean_results = |
| 121 | + tree.neighbors_with_distance(query_point.0, query_point.1, Some(2), None, &euclidean); |
| 122 | + println!(" Euclidean results: {:?}", euclidean_results); |
| 123 | + |
| 124 | + let haversine_results = |
| 125 | + tree.neighbors_with_distance(query_point.0, query_point.1, Some(2), None, &haversine); |
| 126 | + println!(" Haversine results: {:?}", haversine_results); |
| 127 | + |
| 128 | + let spheroid_results = |
| 129 | + tree.neighbors_with_distance(query_point.0, query_point.1, Some(2), None, &spheroid); |
| 130 | + println!(" Spheroid results: {:?}", spheroid_results); |
| 131 | + |
| 132 | + // Test backward compatibility |
| 133 | + let original_results = tree.neighbors(query_point.0, query_point.1, Some(2), None); |
| 134 | + println!(" Original method results: {:?}", original_results); |
| 135 | + |
| 136 | + println!(" Note: For small distances, all metrics should give similar ordering."); |
| 137 | +} |
0 commit comments