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
//! # n3gb-rs
//!
//! Rust implementation of hex-based spatial indexing for British National Grid.
//!
//! Inspired by [GDS NUAR n3gb](https://github.com/national-underground-asset-register/n3gb) and [h3o](https://github.com/HydroniumLabs/h3o).
//!
//! ## Core Structs
//!
//! ### HexCell
//!
//! A single hexagonal cell with a unique ID, center point, and zoom level.
//!
//! **Create one:**
//!
//! ```no_run
//! use n3gb_rs::HexCell;
//! use geo_types::Point;
//!
//! // From BNG coordinates
//! let point = Point::new(383640.0, 398260.0);
//! let cell = HexCell::from_bng(&point, 12).unwrap();
//!
//! // From an existing ID
//! let cell = HexCell::from_hex_id("AAF3kQBBMZQM").unwrap();
//! ```
//!
//! **What you can do:**
//!
//! ```ignore
//! cell.id // Unique Base64 identifier
//! cell.easting() // Center X coordinate
//! cell.northing() // Center Y coordinate
//! cell.to_polygon() // Get the hex as a geo_types Polygon
//! ```
//!
//! ### HexGrid
//!
//! A collection of HexCells covering a bounding box.
//!
//! **Create one:**
//!
//! ```
//! use n3gb_rs::HexGrid;
//!
//! # fn main() -> Result<(), n3gb_rs::N3gbError> {
//! // Using builder
//! let grid = HexGrid::builder()
//! .zoom_level(12)
//! .bng_extent(&(300000.0, 300000.0), &(350000.0, 350000.0))
//! .build()?;
//!
//! // Direct construction
//! let grid = HexGrid::from_bng_extent(&(300000.0, 300000.0), &(350000.0, 350000.0), 12)?;
//! # Ok(())
//! # }
//! ```
//!
//! **What you can do:**
//!
//! ```no_run
//! use n3gb_rs::HexGrid;
//! use geo_types::point;
//!
//! # let grid = HexGrid::from_bng_extent(&(300000.0, 300000.0), &(350000.0, 350000.0), 12).unwrap();
//! // Look up a cell by point
//! let pt = point! { x: 325000.0, y: 325000.0 };
//! if let Some(cell) = grid.get_cell_at(&pt) {
//! println!("{}", cell.id);
//! }
//!
//! // Iterate over all cells
//! for cell in grid.cells() {
//! println!("{}", cell.id);
//! }
//!
//! // Export to GeoParquet
//! grid.to_geoparquet("output.parquet").unwrap();
//!
//! // Export to Arrow RecordBatch
//! let batch = grid.to_record_batch().unwrap();
//! ```
//!
//! ## API Reference
//!
//! For people used to similar hexagonal indexing systems (like H3), here is the mapping to n3gb-rs.
//!
//! ### Indexing functions
//!
//! | Concept | n3gb-rs |
//! | :----------------------- | :--------------------------------------- |
//! | Point to cell (BNG) | `HexCell::from_bng` |
//! | Point to cell (WGS84) | `HexCell::from_wgs84` |
//! | Geometry to cells | `HexCell::from_geometry` |
//! | Cell ID to cell | `HexCell::from_hex_id` |
//! | Generate cell ID | `generate_hex_identifier` |
//! | Decode cell ID | `decode_hex_identifier` |
//! | Point to row/col | `point_to_row_col` |
//! | Row/col to center | `row_col_to_center` |
//!
//! ### Cell inspection functions
//!
//! | Concept | n3gb-rs |
//! | :----------------------- | :--------------------------------------- |
//! | Get zoom level | `HexCell::zoom_level` |
//! | Get cell ID | `HexCell::id` |
//! | Get center point | `HexCell::center` |
//! | Get easting | `HexCell::easting` |
//! | Get northing | `HexCell::northing` |
//! | Get row index | `HexCell::row` |
//! | Get column index | `HexCell::col` |
//! | Cell to polygon | `HexCell::to_polygon` |
//!
//! ### Grid functions
//!
//! | Concept | n3gb-rs |
//! | :------------------------ | :-------------------------------------- |
//! | Grid from extent (BNG) | `HexGrid::from_bng_extent` |
//! | Grid from extent (WGS84) | `HexGrid::from_wgs84_extent` |
//! | Grid from rect | `HexGrid::from_rect` |
//! | Grid from polygon (BNG) | `HexGrid::from_bng_polygon` |
//! | Grid from polygon (WGS84) | `HexGrid::from_wgs84_polygon` |
//! | Grid from multipolygon | `HexGrid::from_bng_multipolygon` |
//! | Grid builder | `HexGridBuilder` |
//! | Get cells | `HexGrid::cells` |
//! | Get cell count | `HexGrid::len` |
//! | Find cell at point | `HexGrid::get_cell_at` |
//! | Filter cells | `HexGrid::filter` |
//! | Grid to polygons | `HexGrid::to_polygons` |
//!
//! ### Line coverage functions
//!
//! | Concept | n3gb-rs |
//! | :----------------------- | :--------------------------------------- |
//! | Line to cells (BNG) | `HexCell::from_line_string_bng` |
//! | Line to cells (WGS84) | `HexCell::from_line_string_wgs84` |
//!
//! ### Coordinate transformation functions
//!
//! | Concept | n3gb-rs |
//! | :------------------------ | :-------------------------------------- |
//! | WGS84 to BNG point | `wgs84_to_bng` |
//! | WGS84 to BNG polygon | `wgs84_polygon_to_bng` |
//! | WGS84 to BNG multipolygon | `wgs84_multipolygon_to_bng` |
//! | WGS84 to BNG line | `wgs84_line_to_bng` |
//!
//! ### Hexagon dimension functions
//!
//! | Concept | n3gb-rs |
//! | :------------------------- | :------------------------------------- |
//! | Dims from side length | `HexagonDims::from_side` |
//! | Dims from circumradius | `HexagonDims::from_circumradius` |
//! | Dims from apothem | `HexagonDims::from_apothem` |
//! | Dims from flat-to-flat | `HexagonDims::from_across_flats` |
//! | Dims from corner-to-corner | `HexagonDims::from_across_corners` |
//! | Dims from area | `HexagonDims::from_area` |
//! | Bounding box | `bounding_box` |
//!
//! ### Geometry functions
//!
//! | Concept | n3gb-rs |
//! | :----------------------- | :--------------------------------------- |
//! | Create hex cell polygon | `create_hexagon` (used in to_polygon) |
//! | Parse WKT/GeoJSON | `parse_geometry` |
//!
//! ### Arrow/Parquet I/O functions
//!
//! | Concept | n3gb-rs |
//! | :----------------------- | :--------------------------------------- |
//! | Cell to Arrow points | `HexCell::to_arrow_points` |
//! | Cell to Arrow polygons | `HexCell::to_arrow_polygons` |
//! | Cell to RecordBatch | `HexCell::to_record_batch` |
//! | Cell to GeoParquet | `HexCell::to_geoparquet` |
//! | Grid to Arrow points | `HexGrid::to_arrow_points` |
//! | Grid to Arrow polygons | `HexGrid::to_arrow_polygons` |
//! | Grid to RecordBatch | `HexGrid::to_record_batch` |
//! | Grid to GeoParquet | `HexGrid::to_geoparquet` |
//! | Write GeoParquet | `write_geoparquet` |
//!
//! ### CSV I/O functions
//!
//! | Concept | n3gb-rs |
//! | :----------------------- | :--------------------------------------- |
//! | CSV to hex-indexed CSV | `csv_to_hex_csv` |
//! | CSV config (geometry) | `CsvHexConfig::new` |
//! | CSV config (coords) | `CsvHexConfig::from_coords` |
//!
//! ### Constants
//!
//! | Concept | n3gb-rs |
//! | :----------------------- | :--------------------------------------- |
//! | Max zoom level | `MAX_ZOOM_LEVEL` |
//! | Cell radii by zoom | `CELL_RADIUS` |
//! | Cell widths by zoom | `CELL_WIDTHS` |
//! | Grid extents (BNG) | `GRID_EXTENTS` |
//! | Identifier version | `IDENTIFIER_VERSION` |
pub use HexCell;
pub use ;
pub use ;
pub use N3gbError;
pub use ;
pub use ;
pub use ;
pub use ;
pub use geo_types;
pub use geoarrow_array;
pub use geoarrow_schema;
pub use geoparquet;