tylertoo-core 0.7.0

Core library for converting GeoParquet to PMTiles vector tiles
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
//! GeoParquet covering metadata extraction for row group filtering.
//!
//! This module extracts bounding box information from GeoParquet files to enable
//! spatial filtering of row groups. When processing bounded extracts (e.g., a city
//! from a country file), we can skip entire row groups whose bboxes don't intersect
//! the area of interest.
//!
//! # GeoParquet Covering Metadata
//!
//! GeoParquet 1.1.0+ supports a `covering` field that specifies which columns contain
//! bounding box information:
//!
//! ```json
//! {
//!   "columns": {
//!     "geometry": {
//!       "covering": {
//!         "bbox": {
//!           "xmin": ["geometry_bbox", "xmin"],
//!           "ymin": ["geometry_bbox", "ymin"],
//!           "xmax": ["geometry_bbox", "xmax"],
//!           "ymax": ["geometry_bbox", "ymax"]
//!         }
//!       }
//!     }
//!   }
//! }
//! ```
//!
//! # Row Group Bbox Extraction
//!
//! For each row group, we extract the bounding box from column statistics:
//! - `xmin` column: MIN statistic (smallest x in row group)
//! - `ymin` column: MIN statistic (smallest y in row group)
//! - `xmax` column: MAX statistic (largest x in row group)
//! - `ymax` column: MAX statistic (largest y in row group)
//!
//! This approach has ~4% overhead compared to baseline metadata reads, and is
//! 370x faster than scanning column values.

use crate::tile::{lng_lat_to_tile, TileBounds, TileCoord};
use crate::Error;
use parquet::file::metadata::ParquetMetaData;
use parquet::file::reader::{FileReader, SerializedFileReader};
use serde::Deserialize;
use std::collections::HashMap;
use std::fs::File;
use std::path::Path;

/// Specification for bbox covering columns parsed from GeoParquet metadata.
#[derive(Debug, Clone, PartialEq)]
pub struct CoveringSpec {
    /// Column path for xmin values (e.g., "geometry_bbox.xmin")
    pub xmin_path: Vec<String>,
    /// Column path for ymin values
    pub ymin_path: Vec<String>,
    /// Column path for xmax values
    pub xmax_path: Vec<String>,
    /// Column path for ymax values
    pub ymax_path: Vec<String>,
}

impl CoveringSpec {
    /// Create a new CoveringSpec from column paths.
    pub fn new(
        xmin_path: Vec<String>,
        ymin_path: Vec<String>,
        xmax_path: Vec<String>,
        ymax_path: Vec<String>,
    ) -> Self {
        Self {
            xmin_path,
            ymin_path,
            xmax_path,
            ymax_path,
        }
    }

    /// Convert a column path to a dotted string for matching against Parquet column paths.
    fn path_to_string(path: &[String]) -> String {
        path.join(".")
    }

    /// Get the dotted xmin column path.
    pub fn xmin_column(&self) -> String {
        Self::path_to_string(&self.xmin_path)
    }

    /// Get the dotted ymin column path.
    pub fn ymin_column(&self) -> String {
        Self::path_to_string(&self.ymin_path)
    }

    /// Get the dotted xmax column path.
    pub fn xmax_column(&self) -> String {
        Self::path_to_string(&self.xmax_path)
    }

    /// Get the dotted ymax column path.
    pub fn ymax_column(&self) -> String {
        Self::path_to_string(&self.ymax_path)
    }
}

/// Bounding box for a single row group.
#[derive(Debug, Clone, PartialEq)]
pub struct RowGroupBounds {
    /// Row group index (0-based)
    pub row_group_idx: usize,
    /// Minimum longitude
    pub xmin: f64,
    /// Minimum latitude
    pub ymin: f64,
    /// Maximum longitude
    pub xmax: f64,
    /// Maximum latitude
    pub ymax: f64,
    /// Number of rows in this row group (for density estimation)
    pub num_rows: usize,
}

impl RowGroupBounds {
    /// Check if this row group's bounds intersect with the given filter bounds.
    pub fn intersects(&self, filter: &TileBounds) -> bool {
        // Standard AABB intersection test
        self.xmin <= filter.lng_max
            && self.xmax >= filter.lng_min
            && self.ymin <= filter.lat_max
            && self.ymax >= filter.lat_min
    }

    /// Convert to TileBounds for compatibility with existing code.
    pub fn to_tile_bounds(&self) -> TileBounds {
        TileBounds {
            lng_min: self.xmin,
            lat_min: self.ymin,
            lng_max: self.xmax,
            lat_max: self.ymax,
        }
    }
}

// ============================================================================
// GeoParquet Metadata Parsing
// ============================================================================

/// Internal structures for parsing GeoParquet JSON metadata.
#[derive(Debug, Deserialize)]
struct GeoMetadata {
    columns: HashMap<String, ColumnMetadata>,
    #[serde(default)]
    primary_column: Option<String>,
}

#[derive(Debug, Deserialize)]
struct ColumnMetadata {
    #[serde(default)]
    covering: Option<CoveringMetadata>,
}

#[derive(Debug, Deserialize)]
struct CoveringMetadata {
    bbox: Option<BboxCovering>,
}

#[derive(Debug, Deserialize)]
struct BboxCovering {
    xmin: Vec<String>,
    ymin: Vec<String>,
    xmax: Vec<String>,
    ymax: Vec<String>,
}

/// Parse the covering specification from GeoParquet geo metadata JSON.
///
/// Returns `None` if the metadata doesn't contain covering information.
///
/// # Arguments
///
/// * `geo_json` - The JSON string from the "geo" key-value metadata
///
/// # Returns
///
/// `Ok(Some(CoveringSpec))` if covering metadata is present and valid,
/// `Ok(None)` if no covering metadata exists,
/// `Err` if the JSON is malformed.
pub fn parse_covering_metadata(geo_json: &str) -> Result<Option<CoveringSpec>, Error> {
    let metadata: GeoMetadata = serde_json::from_str(geo_json)
        .map_err(|e| Error::GeoParquetRead(format!("Failed to parse geo metadata JSON: {}", e)))?;

    // Find the geometry column (use primary_column if specified, otherwise first with covering)
    let geom_column = if let Some(ref primary) = metadata.primary_column {
        metadata.columns.get(primary)
    } else {
        // Find first column with covering metadata
        metadata.columns.values().find(|col| col.covering.is_some())
    };

    let Some(column) = geom_column else {
        return Ok(None);
    };

    let Some(covering) = &column.covering else {
        return Ok(None);
    };

    let Some(bbox) = &covering.bbox else {
        return Ok(None);
    };

    Ok(Some(CoveringSpec::new(
        bbox.xmin.clone(),
        bbox.ymin.clone(),
        bbox.xmax.clone(),
        bbox.ymax.clone(),
    )))
}

// ============================================================================
// Row Group Bbox Extraction
// ============================================================================

/// Column indices for bbox fields within a Parquet file.
#[derive(Debug, Clone, Copy)]
pub struct BboxColumnIndices {
    pub xmin: usize,
    pub ymin: usize,
    pub xmax: usize,
    pub ymax: usize,
}

/// Find the column indices for bbox fields in the Parquet schema.
pub fn find_bbox_column_indices(
    metadata: &ParquetMetaData,
    covering: &CoveringSpec,
) -> Option<BboxColumnIndices> {
    let schema = metadata.file_metadata().schema_descr();
    let num_columns = schema.num_columns();

    let mut xmin_idx = None;
    let mut ymin_idx = None;
    let mut xmax_idx = None;
    let mut ymax_idx = None;

    let xmin_col = covering.xmin_column().to_lowercase();
    let ymin_col = covering.ymin_column().to_lowercase();
    let xmax_col = covering.xmax_column().to_lowercase();
    let ymax_col = covering.ymax_column().to_lowercase();

    for col_idx in 0..num_columns {
        let col_path = schema.column(col_idx).path().string().to_lowercase();

        if col_path == xmin_col || col_path.ends_with(&format!(".{}", xmin_col)) {
            xmin_idx = Some(col_idx);
        }
        if col_path == ymin_col || col_path.ends_with(&format!(".{}", ymin_col)) {
            ymin_idx = Some(col_idx);
        }
        if col_path == xmax_col || col_path.ends_with(&format!(".{}", xmax_col)) {
            xmax_idx = Some(col_idx);
        }
        if col_path == ymax_col || col_path.ends_with(&format!(".{}", ymax_col)) {
            ymax_idx = Some(col_idx);
        }
    }

    match (xmin_idx, ymin_idx, xmax_idx, ymax_idx) {
        (Some(xmin), Some(ymin), Some(xmax), Some(ymax)) => Some(BboxColumnIndices {
            xmin,
            ymin,
            xmax,
            ymax,
        }),
        _ => None,
    }
}

/// Extract a statistic value from column metadata.
///
/// Handles both f32 and f64 physical types, converting to f64.
fn extract_stat_value(
    row_group: &parquet::file::metadata::RowGroupMetaData,
    col_idx: usize,
    use_min: bool,
) -> Option<f64> {
    let col_meta = row_group.column(col_idx);
    let stats = col_meta.statistics()?;

    let bytes = if use_min {
        stats.min_bytes_opt()?
    } else {
        stats.max_bytes_opt()?
    };

    match bytes.len() {
        4 => {
            // FLOAT (f32)
            let arr: [u8; 4] = bytes.try_into().ok()?;
            Some(f32::from_le_bytes(arr) as f64)
        }
        8 => {
            // DOUBLE (f64)
            let arr: [u8; 8] = bytes.try_into().ok()?;
            Some(f64::from_le_bytes(arr))
        }
        _ => None,
    }
}

/// Extract bounding boxes for all row groups from a Parquet file.
///
/// Returns `None` for row groups where statistics are unavailable.
///
/// # Arguments
///
/// * `path` - Path to the GeoParquet file
///
/// # Returns
///
/// A vector of `Option<RowGroupBounds>`, one per row group.
/// Returns `Err` if the file cannot be read or has no covering metadata.
pub fn extract_row_group_bounds(path: &Path) -> Result<Vec<Option<RowGroupBounds>>, Error> {
    let file = File::open(path)
        .map_err(|e| Error::GeoParquetRead(format!("Failed to open {}: {}", path.display(), e)))?;

    let reader = SerializedFileReader::new(file)
        .map_err(|e| Error::GeoParquetRead(format!("Failed to read {}: {}", path.display(), e)))?;

    extract_row_group_bounds_from_reader(&reader)
}

/// Extract bounding boxes from an already-opened Parquet reader.
///
/// This is useful when you already have a reader open and don't want to re-open the file.
pub fn extract_row_group_bounds_from_reader(
    reader: &SerializedFileReader<File>,
) -> Result<Vec<Option<RowGroupBounds>>, Error> {
    extract_row_group_bounds_from_metadata(reader.metadata())
}

/// Extract per-row-group bounding boxes directly from parsed Parquet metadata.
///
/// This is the metadata-only variant of [`extract_row_group_bounds_from_reader`]:
/// callers that already hold a [`ParquetMetaData`] (e.g. from a
/// `ParquetRecordBatchReaderBuilder`) can prune without re-opening the file.
/// Returns `None` for row groups whose covering statistics are unavailable, and
/// `vec![None; n]` when the file lacks geo/covering metadata entirely.
pub fn extract_row_group_bounds_from_metadata(
    metadata: &ParquetMetaData,
) -> Result<Vec<Option<RowGroupBounds>>, Error> {
    let num_row_groups = metadata.num_row_groups();

    // Parse geo metadata to get covering spec
    let geo_json = get_geo_metadata(metadata)?;
    let Some(geo_json) = geo_json else {
        // No geo metadata - return all None
        return Ok(vec![None; num_row_groups]);
    };

    let covering = parse_covering_metadata(&geo_json)?;
    let Some(covering) = covering else {
        // No covering metadata - return all None
        return Ok(vec![None; num_row_groups]);
    };

    // Find column indices
    let Some(indices) = find_bbox_column_indices(metadata, &covering) else {
        // Couldn't find bbox columns - return all None
        return Ok(vec![None; num_row_groups]);
    };

    // Extract bounds for each row group
    let mut bounds = Vec::with_capacity(num_row_groups);

    for rg_idx in 0..num_row_groups {
        let rg = metadata.row_group(rg_idx);

        // For row group bounds:
        // - xmin: MIN of xmin column (smallest x in this row group)
        // - ymin: MIN of ymin column (smallest y in this row group)
        // - xmax: MAX of xmax column (largest x in this row group)
        // - ymax: MAX of ymax column (largest y in this row group)
        let xmin = extract_stat_value(rg, indices.xmin, true);
        let ymin = extract_stat_value(rg, indices.ymin, true);
        let xmax = extract_stat_value(rg, indices.xmax, false);
        let ymax = extract_stat_value(rg, indices.ymax, false);

        match (xmin, ymin, xmax, ymax) {
            (Some(xmin), Some(ymin), Some(xmax), Some(ymax)) => {
                bounds.push(Some(RowGroupBounds {
                    row_group_idx: rg_idx,
                    xmin,
                    ymin,
                    xmax,
                    ymax,
                    num_rows: rg.num_rows() as usize,
                }));
            }
            _ => bounds.push(None),
        }
    }

    Ok(bounds)
}

/// Get the "geo" metadata JSON string from Parquet file metadata.
pub fn get_geo_metadata(metadata: &ParquetMetaData) -> Result<Option<String>, Error> {
    let kv = metadata.file_metadata().key_value_metadata();
    let Some(kv) = kv else {
        return Ok(None);
    };

    for pair in kv {
        if pair.key.to_lowercase() == "geo" {
            return Ok(pair.value.clone());
        }
    }

    Ok(None)
}

// ============================================================================
// Tile Coordinate Parsing (for --bounds flag)
// ============================================================================

/// Parse bounds from either tile coordinates (z/x/y) or bbox (xmin,ymin,xmax,ymax).
///
/// Tile coordinates are converted to WGS84 bounds using Web Mercator projection.
pub fn parse_bounds(input: &str) -> Result<TileBounds, Error> {
    // Try z/x/y format first
    if let Some(bounds) = parse_tile_coords(input) {
        return Ok(bounds);
    }

    // Fall back to xmin,ymin,xmax,ymax format
    parse_bbox(input)
}

/// Parse tile coordinates in z/x/y format and convert to WGS84 bounds.
fn parse_tile_coords(input: &str) -> Option<TileBounds> {
    let parts: Vec<&str> = input.split('/').collect();
    if parts.len() != 3 {
        return None;
    }

    let z: u8 = parts[0].parse().ok()?;
    let x: u32 = parts[1].parse().ok()?;
    let y: u32 = parts[2].parse().ok()?;

    Some(tile_to_bounds(z, x, y))
}

/// Convert tile coordinates to WGS84 bounds using Web Mercator projection.
pub fn tile_to_bounds(z: u8, x: u32, y: u32) -> TileBounds {
    use std::f64::consts::PI;

    let n = 2f64.powi(z as i32);

    let lng_min = x as f64 / n * 360.0 - 180.0;
    let lng_max = (x + 1) as f64 / n * 360.0 - 180.0;

    // Note: y=0 is at the top (north), so lat_max uses y, lat_min uses y+1
    let lat_max = (PI * (1.0 - 2.0 * y as f64 / n)).sinh().atan().to_degrees();
    let lat_min = (PI * (1.0 - 2.0 * (y + 1) as f64 / n))
        .sinh()
        .atan()
        .to_degrees();

    TileBounds {
        lng_min,
        lat_min,
        lng_max,
        lat_max,
    }
}

/// Iterator over tiles covering a geographic bounding box at a given zoom level.
///
/// Used for coalescing density estimation: count tiles to estimate features-per-tile.
///
/// # Arguments
///
/// * `bounds` - Geographic bounding box (lng/lat, WGS84)
/// * `zoom` - Target zoom level
///
/// # Returns
///
/// Iterator yielding `TileCoord` for each tile that intersects the bounding box.
pub fn covering_tiles(bounds: &TileBounds, zoom: u8) -> impl Iterator<Item = TileCoord> {
    // Convert corner coordinates to tile coordinates
    let min_tile = lng_lat_to_tile(bounds.lng_min, bounds.lat_max, zoom); // NW corner
    let max_tile = lng_lat_to_tile(bounds.lng_max, bounds.lat_min, zoom); // SE corner

    // Generate all tiles in the bounding rectangle
    (min_tile.x..=max_tile.x)
        .flat_map(move |x| (min_tile.y..=max_tile.y).map(move |y| TileCoord::new(x, y, zoom)))
}

/// Parse bbox in xmin,ymin,xmax,ymax format.
fn parse_bbox(input: &str) -> Result<TileBounds, Error> {
    let parts: Vec<&str> = input.split(',').collect();
    if parts.len() != 4 {
        return Err(Error::InvalidConfig(format!(
            "Invalid bounds format '{}'. Expected 'z/x/y' or 'xmin,ymin,xmax,ymax'",
            input
        )));
    }

    let xmin: f64 = parts[0].trim().parse().map_err(|_| {
        Error::InvalidConfig(format!("Invalid xmin value '{}' in bounds", parts[0]))
    })?;
    let ymin: f64 = parts[1].trim().parse().map_err(|_| {
        Error::InvalidConfig(format!("Invalid ymin value '{}' in bounds", parts[1]))
    })?;
    let xmax: f64 = parts[2].trim().parse().map_err(|_| {
        Error::InvalidConfig(format!("Invalid xmax value '{}' in bounds", parts[2]))
    })?;
    let ymax: f64 = parts[3].trim().parse().map_err(|_| {
        Error::InvalidConfig(format!("Invalid ymax value '{}' in bounds", parts[3]))
    })?;

    // Validate bounds
    if xmin >= xmax {
        return Err(Error::InvalidConfig(format!(
            "xmin ({}) must be less than xmax ({})",
            xmin, xmax
        )));
    }
    if ymin >= ymax {
        return Err(Error::InvalidConfig(format!(
            "ymin ({}) must be less than ymax ({})",
            ymin, ymax
        )));
    }

    Ok(TileBounds {
        lng_min: xmin,
        lat_min: ymin,
        lng_max: xmax,
        lat_max: ymax,
    })
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tile::TileCoord;

    // -------------------------------------------------------------------------
    // Covering Metadata Parsing Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_parse_covering_metadata_standard_format() {
        let geo_json = r#"{
            "columns": {
                "geometry": {
                    "covering": {
                        "bbox": {
                            "xmin": ["geometry_bbox", "xmin"],
                            "ymin": ["geometry_bbox", "ymin"],
                            "xmax": ["geometry_bbox", "xmax"],
                            "ymax": ["geometry_bbox", "ymax"]
                        }
                    }
                }
            },
            "primary_column": "geometry",
            "version": "1.1.0"
        }"#;

        let result = parse_covering_metadata(geo_json).unwrap();
        assert!(result.is_some());

        let spec = result.unwrap();
        assert_eq!(spec.xmin_path, vec!["geometry_bbox", "xmin"]);
        assert_eq!(spec.ymin_path, vec!["geometry_bbox", "ymin"]);
        assert_eq!(spec.xmax_path, vec!["geometry_bbox", "xmax"]);
        assert_eq!(spec.ymax_path, vec!["geometry_bbox", "ymax"]);
    }

    #[test]
    fn test_parse_covering_metadata_no_covering() {
        let geo_json = r#"{
            "columns": {
                "geometry": {
                    "bbox": [-180, -90, 180, 90]
                }
            },
            "primary_column": "geometry"
        }"#;

        let result = parse_covering_metadata(geo_json).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_parse_covering_metadata_invalid_json() {
        let result = parse_covering_metadata("not valid json");
        assert!(result.is_err());
    }

    #[test]
    fn test_covering_spec_column_paths() {
        let spec = CoveringSpec::new(
            vec!["bbox".to_string(), "xmin".to_string()],
            vec!["bbox".to_string(), "ymin".to_string()],
            vec!["bbox".to_string(), "xmax".to_string()],
            vec!["bbox".to_string(), "ymax".to_string()],
        );

        assert_eq!(spec.xmin_column(), "bbox.xmin");
        assert_eq!(spec.ymin_column(), "bbox.ymin");
        assert_eq!(spec.xmax_column(), "bbox.xmax");
        assert_eq!(spec.ymax_column(), "bbox.ymax");
    }

    // -------------------------------------------------------------------------
    // Row Group Bounds Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_row_group_bounds_intersects() {
        let bounds = RowGroupBounds {
            row_group_idx: 0,
            xmin: -10.0,
            ymin: -10.0,
            xmax: 10.0,
            ymax: 10.0,
            num_rows: 1000,
        };

        // Overlapping filter
        let filter = TileBounds {
            lng_min: -5.0,
            lat_min: -5.0,
            lng_max: 5.0,
            lat_max: 5.0,
        };
        assert!(bounds.intersects(&filter));

        // Partial overlap
        let filter = TileBounds {
            lng_min: 5.0,
            lat_min: 5.0,
            lng_max: 15.0,
            lat_max: 15.0,
        };
        assert!(bounds.intersects(&filter));

        // No overlap (completely outside)
        let filter = TileBounds {
            lng_min: 20.0,
            lat_min: 20.0,
            lng_max: 30.0,
            lat_max: 30.0,
        };
        assert!(!bounds.intersects(&filter));

        // Edge touch (should intersect)
        let filter = TileBounds {
            lng_min: 10.0,
            lat_min: 10.0,
            lng_max: 20.0,
            lat_max: 20.0,
        };
        assert!(bounds.intersects(&filter));
    }

    #[test]
    fn test_row_group_bounds_to_tile_bounds() {
        let rg_bounds = RowGroupBounds {
            row_group_idx: 5,
            xmin: -122.5,
            ymin: 37.7,
            xmax: -122.3,
            ymax: 37.9,
            num_rows: 500,
        };

        let tile_bounds = rg_bounds.to_tile_bounds();
        assert_eq!(tile_bounds.lng_min, -122.5);
        assert_eq!(tile_bounds.lat_min, 37.7);
        assert_eq!(tile_bounds.lng_max, -122.3);
        assert_eq!(tile_bounds.lat_max, 37.9);
    }

    // -------------------------------------------------------------------------
    // Tile Coordinate Parsing Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_tile_to_bounds_z0() {
        let bounds = tile_to_bounds(0, 0, 0);
        assert!((bounds.lng_min - (-180.0)).abs() < 0.001);
        assert!((bounds.lng_max - 180.0).abs() < 0.001);
        // Web Mercator bounds at z0 are approximately [-85.05, 85.05]
        assert!(bounds.lat_min > -86.0 && bounds.lat_min < -85.0);
        assert!(bounds.lat_max > 85.0 && bounds.lat_max < 86.0);
    }

    #[test]
    fn test_tile_to_bounds_sf() {
        // Tile containing San Francisco at z10
        let bounds = tile_to_bounds(10, 163, 395);

        // SF is roughly at -122.4, 37.8
        assert!(bounds.lng_min < -122.0 && bounds.lng_min > -123.0);
        assert!(bounds.lng_max < -122.0 && bounds.lng_max > -123.0);
        assert!(bounds.lat_min > 37.0 && bounds.lat_min < 38.0);
        assert!(bounds.lat_max > 37.0 && bounds.lat_max < 38.0);
    }

    #[test]
    fn test_parse_bounds_tile_coords() {
        let bounds = parse_bounds("10/163/395").unwrap();
        assert!(bounds.lng_min < -122.0);
        assert!(bounds.lat_min > 37.0);
    }

    #[test]
    fn test_parse_bounds_bbox() {
        let bounds = parse_bounds("-122.5,37.7,-122.3,37.9").unwrap();
        assert_eq!(bounds.lng_min, -122.5);
        assert_eq!(bounds.lat_min, 37.7);
        assert_eq!(bounds.lng_max, -122.3);
        assert_eq!(bounds.lat_max, 37.9);
    }

    #[test]
    fn test_parse_bounds_bbox_with_spaces() {
        let bounds = parse_bounds("-122.5, 37.7, -122.3, 37.9").unwrap();
        assert_eq!(bounds.lng_min, -122.5);
        assert_eq!(bounds.lat_min, 37.7);
    }

    #[test]
    fn test_parse_bounds_invalid_tile_coords() {
        // Invalid format falls through to bbox parsing, which also fails
        let result = parse_bounds("10/abc/395");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_bounds_invalid_bbox() {
        // Not enough values
        let result = parse_bounds("-122.5,37.7,-122.3");
        assert!(result.is_err());

        // xmin >= xmax
        let result = parse_bounds("10,37.7,-122.3,37.9");
        assert!(result.is_err());

        // ymin >= ymax
        let result = parse_bounds("-122.5,40.0,-122.3,37.9");
        assert!(result.is_err());
    }

    // -------------------------------------------------------------------------
    // Integration Tests (require test fixtures)
    // -------------------------------------------------------------------------

    // These tests require actual GeoParquet files with covering metadata.
    // They are marked as #[ignore] and can be run with:
    // cargo test --package tylertoo-core covering -- --ignored

    #[test]
    #[ignore]
    fn test_extract_row_group_bounds_from_file() {
        // This test requires ~/Downloads/adm4_polygons.parquet
        let path = std::path::PathBuf::from(
            std::env::var("HOME").unwrap() + "/Downloads/adm4_polygons.parquet",
        );

        if !path.exists() {
            eprintln!("Skipping test: {} not found", path.display());
            return;
        }

        let bounds = extract_row_group_bounds(&path).unwrap();

        // adm4_polygons.parquet has 364 row groups
        assert!(!bounds.is_empty());

        // At least some row groups should have bounds
        let with_bounds = bounds.iter().filter(|b| b.is_some()).count();
        assert!(with_bounds > 0, "Expected some row groups to have bounds");

        // Check that bounds are valid WGS84 coordinates
        for bound in bounds.iter().flatten() {
            assert!(
                bound.xmin >= -180.0 && bound.xmin <= 180.0,
                "Invalid xmin: {}",
                bound.xmin
            );
            assert!(
                bound.xmax >= -180.0 && bound.xmax <= 180.0,
                "Invalid xmax: {}",
                bound.xmax
            );
            assert!(
                bound.ymin >= -90.0 && bound.ymin <= 90.0,
                "Invalid ymin: {}",
                bound.ymin
            );
            assert!(
                bound.ymax >= -90.0 && bound.ymax <= 90.0,
                "Invalid ymax: {}",
                bound.ymax
            );
            assert!(bound.xmin <= bound.xmax, "xmin > xmax");
            assert!(bound.ymin <= bound.ymax, "ymin > ymax");
        }

        println!(
            "Extracted bounds for {}/{} row groups",
            with_bounds,
            bounds.len()
        );
    }

    // -------------------------------------------------------------------------
    // Tile Coverage Tests (for coalescing density estimation)
    // -------------------------------------------------------------------------

    #[test]
    fn test_covering_tiles_single_tile() {
        // Bounds that fit entirely within a single z10 tile
        // San Francisco downtown area
        let bounds = TileBounds::new(-122.42, 37.78, -122.40, 37.80);
        let tiles: Vec<_> = covering_tiles(&bounds, 10).collect();

        assert_eq!(tiles.len(), 1, "Expected single tile coverage");
        assert_eq!(tiles[0].z, 10);
    }

    #[test]
    fn test_covering_tiles_multiple_tiles() {
        // Bounds spanning multiple tiles at z10
        // Larger SF Bay area
        let bounds = TileBounds::new(-122.5, 37.7, -122.3, 37.9);
        let tiles: Vec<_> = covering_tiles(&bounds, 10).collect();

        assert!(tiles.len() > 1, "Expected multiple tiles");
        // All tiles should be at z10
        assert!(tiles.iter().all(|t| t.z == 10));
    }

    #[test]
    fn test_covering_tiles_world_at_z0() {
        // World bounds at z0 = exactly one tile
        let bounds = TileBounds::new(-180.0, -85.0, 180.0, 85.0);
        let tiles: Vec<_> = covering_tiles(&bounds, 0).collect();

        assert_eq!(tiles.len(), 1);
        assert_eq!(tiles[0], TileCoord::new(0, 0, 0));
    }

    #[test]
    fn test_covering_tiles_world_at_z1() {
        // World bounds at z1 = four tiles (2x2)
        let bounds = TileBounds::new(-180.0, -85.0, 180.0, 85.0);
        let tiles: Vec<_> = covering_tiles(&bounds, 1).collect();

        assert_eq!(tiles.len(), 4);
    }

    #[test]
    fn test_covering_tiles_count_matches_expected() {
        // A bbox covering roughly 2x3 tiles at z12
        let bounds = TileBounds::new(-122.5, 37.7, -122.35, 37.85);
        let tiles: Vec<_> = covering_tiles(&bounds, 12).collect();

        // Calculate expected: tiles spanning this bbox
        // We don't need exact count, just verify reasonable range
        assert!(
            tiles.len() >= 4 && tiles.len() <= 12,
            "Expected 4-12 tiles, got {}",
            tiles.len()
        );
    }
}