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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2026 Torgeir Børresen <tb@starkad.no>
// Rust port of Google's S2 Geometry library — a derivative work, modified from
// the upstream Apache-2.0 source(s) below (Copyright Google Inc.). See LICENSE.
// - C++: google/s2geometry
// - Java: google/s2-geometry-library-java
// S2PointIndex: a spatial index for S2Points sorted by leaf S2CellId.
//
// Each point can optionally store auxiliary data. Points can be added or
// removed dynamically, and a seekable iterator provides efficient spatial
// traversal.
//
// C++ ref: s2point_index.h
use std::collections::BTreeMap;
use crate::s2::{CellId, Point};
/// An entry in the point index: a point with associated data.
#[derive(Clone, Debug)]
pub struct PointData<D: Clone> {
/// The point on the sphere.
pub point: Point,
/// The associated data value.
pub data: D,
}
impl<D: Clone + PartialEq> PartialEq for PointData<D> {
fn eq(&self, other: &Self) -> bool {
self.point == other.point && self.data == other.data
}
}
/// A spatial index of `S2Points` sorted by leaf `S2CellId`.
///
/// Each point can store auxiliary data of type `D`. Use `()` if no extra
/// data is needed.
#[derive(Debug)]
pub struct S2PointIndex<D: Clone> {
// BTreeMap from CellId to a list of point-data pairs at that cell.
map: BTreeMap<CellId, Vec<PointData<D>>>,
num_points: usize,
}
impl<D: Clone> Default for S2PointIndex<D> {
fn default() -> Self {
Self::new()
}
}
impl<D: Clone> S2PointIndex<D> {
/// Creates an empty index.
pub fn new() -> Self {
S2PointIndex {
map: BTreeMap::new(),
num_points: 0,
}
}
/// Returns the number of points in the index.
pub fn num_points(&self) -> usize {
self.num_points
}
/// Adds a point with associated data to the index.
pub fn add(&mut self, point: Point, data: D) {
let id = CellId::from_point(&point);
self.map
.entry(id)
.or_default()
.push(PointData { point, data });
self.num_points += 1;
}
/// Removes the first matching (point, data) pair. Returns false if not found.
pub fn remove(&mut self, point: Point, data: &D) -> bool
where
D: PartialEq,
{
let id = CellId::from_point(&point);
if let Some(entries) = self.map.get_mut(&id)
&& let Some(pos) = entries
.iter()
.position(|e| e.point == point && e.data == *data)
{
entries.swap_remove(pos);
if entries.is_empty() {
self.map.remove(&id);
}
self.num_points -= 1;
return true;
}
false
}
/// Removes all points from the index.
pub fn clear(&mut self) {
self.map.clear();
self.num_points = 0;
}
/// Returns an iterator positioned at the first entry.
pub fn iter(&self) -> PointIndexIterator<'_, D> {
PointIndexIterator::new(self)
}
}
impl<'a, D: Clone> IntoIterator for &'a S2PointIndex<D> {
type Item = (CellId, &'a PointData<D>);
type IntoIter = PointIndexIterator<'a, D>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
/// Iterator over an `S2PointIndex`. Walks entries in `CellId` order.
///
/// The iterator provides seek-based traversal suitable for spatial queries.
#[derive(Debug)]
pub struct PointIndexIterator<'a, D: Clone> {
// Flattened view: we iterate over the BTreeMap entries, and for each
// CellId entry we iterate over its Vec of PointData.
entries: Vec<(CellId, &'a PointData<D>)>,
pos: usize,
}
impl<'a, D: Clone> PointIndexIterator<'a, D> {
/// Creates a new iterator positioned at the first entry.
pub fn new(index: &'a S2PointIndex<D>) -> Self {
let mut entries = Vec::with_capacity(index.num_points);
for (&cell_id, point_datas) in &index.map {
for pd in point_datas {
entries.push((cell_id, pd));
}
}
PointIndexIterator { entries, pos: 0 }
}
/// Returns true if the iterator is past the last entry.
pub fn done(&self) -> bool {
self.pos >= self.entries.len()
}
/// Returns the `CellId` of the current entry.
pub fn id(&self) -> CellId {
self.entries[self.pos].0
}
/// Returns the point of the current entry.
pub fn point(&self) -> Point {
self.entries[self.pos].1.point
}
/// Returns the data of the current entry.
pub fn data(&self) -> &D {
&self.entries[self.pos].1.data
}
/// Returns the `PointData` of the current entry.
pub fn point_data(&self) -> &'a PointData<D> {
self.entries[self.pos].1
}
/// Positions the iterator at the first entry.
pub fn begin(&mut self) {
self.pos = 0;
}
/// Positions the iterator past the last entry.
pub fn finish(&mut self) {
self.pos = self.entries.len();
}
/// Advances to the next entry.
pub fn next(&mut self) {
self.pos += 1;
}
/// Moves to the previous entry. Returns false if already at beginning.
pub fn prev(&mut self) -> bool {
if self.pos == 0 {
return false;
}
self.pos -= 1;
true
}
/// Positions at the first entry with `id()` >= target.
pub fn seek(&mut self, target: CellId) {
self.pos = self.entries.partition_point(|&(id, _)| id < target);
}
}
impl<'a, D: Clone> Iterator for PointIndexIterator<'a, D> {
type Item = (CellId, &'a PointData<D>);
fn next(&mut self) -> Option<Self::Item> {
if self.done() {
return None;
}
let item = self.entries[self.pos];
self.pos += 1;
Some(item)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::s2::LatLng;
#[test]
fn test_empty_index() {
let index = S2PointIndex::<i32>::new();
assert_eq!(index.num_points(), 0);
let iter = index.iter();
assert!(iter.done());
}
#[test]
fn test_add_and_iterate() {
let mut index = S2PointIndex::new();
let p1 = LatLng::from_degrees(0.0, 0.0).to_point();
let p2 = LatLng::from_degrees(10.0, 20.0).to_point();
let p3 = LatLng::from_degrees(-5.0, 50.0).to_point();
index.add(p1, 1);
index.add(p2, 2);
index.add(p3, 3);
assert_eq!(index.num_points(), 3);
let mut iter = index.iter();
let mut count = 0;
while !iter.done() {
count += 1;
iter.next();
}
assert_eq!(count, 3);
}
#[test]
fn test_remove() {
let mut index = S2PointIndex::new();
let p = LatLng::from_degrees(0.0, 0.0).to_point();
index.add(p, 42);
assert_eq!(index.num_points(), 1);
assert!(index.remove(p, &42));
assert_eq!(index.num_points(), 0);
assert!(!index.remove(p, &42));
}
#[test]
fn test_seek() {
let mut index = S2PointIndex::new();
// Add points at various locations.
for i in 0..20 {
let lat = -90.0 + f64::from(i) * 9.0;
let p = LatLng::from_degrees(lat, 0.0).to_point();
index.add(p, i);
}
let mut iter = index.iter();
// Seek to the CellId of a point near the equator.
let target = CellId::from_point(&LatLng::from_degrees(0.0, 0.0).to_point());
iter.seek(target);
assert!(!iter.done());
assert!(iter.id() >= target);
}
#[test]
fn test_duplicate_points() {
let mut index = S2PointIndex::new();
let p = Point::from_coords(1.0, 0.0, 0.0);
for i in 0..100 {
index.add(p, i);
}
assert_eq!(index.num_points(), 100);
let mut iter = index.iter();
let mut count = 0;
while !iter.done() {
count += 1;
iter.next();
}
assert_eq!(count, 100);
}
#[test]
fn test_prev() {
let mut index = S2PointIndex::new();
let p1 = LatLng::from_degrees(10.0, 0.0).to_point();
let p2 = LatLng::from_degrees(20.0, 0.0).to_point();
index.add(p1, 1);
index.add(p2, 2);
let mut iter = index.iter();
assert!(!iter.prev()); // at beginning
iter.finish();
assert!(iter.prev()); // go to last
assert!(!iter.done());
}
}