ST_DWithin(a, b, distance_meters) — are A and B within the given distance? Optimized: uses bbox expansion pre-filter to avoid expensive exact distance computation when geometries are clearly far apart.
(a: &Geometry, b: &Geometry, distance_meters: f64)
| 103 | /// Optimized: uses bbox expansion pre-filter to avoid expensive exact |
| 104 | /// distance computation when geometries are clearly far apart. |
| 105 | pub fn st_dwithin(a: &Geometry, b: &Geometry, distance_meters: f64) -> bool { |
| 106 | // Fast path for points: just haversine. |
| 107 | if let (Geometry::Point { coordinates: ca }, Geometry::Point { coordinates: cb }) = (a, b) { |
| 108 | return haversine_distance(ca[0], ca[1], cb[0], cb[1]) <= distance_meters; |
| 109 | } |
| 110 | |
| 111 | // Bbox pre-filter: expand A's bbox by distance, check if B's bbox intersects. |
| 112 | let a_bb = geometry_bbox(a).expand_meters(distance_meters); |
| 113 | let b_bb = geometry_bbox(b); |
| 114 | if !a_bb.intersects(&b_bb) { |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | // Exact check. |
| 119 | st_distance(a, b) <= distance_meters |
| 120 | } |
| 121 | |
| 122 | // ── Distance helpers ── |
| 123 |
no test coverage detected