geonative-geojson 0.4.0

GeoJSON (RFC 7946) reader and writer for the geonative geospatial library
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
//! GeoJSON reader.
//!
//! Two construction paths:
//!
//! - **[`GeoJsonReader::open`] (streaming, default for files).** Does two
//!   streaming passes over the file — schema inference, then feature
//!   yield — without materialising the whole JSON tree. Peak RAM is
//!   bounded to ~one feature regardless of input size. Recommended for
//!   anything larger than "obviously fits in RAM".
//! - **[`GeoJsonReader::from_bytes`] / [`from_value`].** Eager: parses
//!   the whole input upfront and stores a `Vec<Feature>`. Useful when
//!   you've already got the bytes in memory (tests, small inline
//!   blobs, downstream code paths that need `features()` as a slice).
//!
//! Both expose the same [`schema`](Self::schema) /
//! [`feature_count`](Self::feature_count) /
//! [`into_features`](Self::into_features) surface; callers that don't
//! care about the source can hold the trait-object equivalent.
//!
//! ## Top-level shapes accepted
//!
//! - `FeatureCollection` with `features: []`
//! - Bare `Feature`
//! - Bare geometry object (wrapped into a single fid-less, property-less feature)
//!
//! ## CRS handling
//!
//! RFC 7946 mandates WGS84 (EPSG:4326) and removes the legacy 2008-era `crs`
//! member. We default to `Crs::Epsg(4326)` but honour an explicit legacy
//! `crs.properties.name` URN like `urn:ogc:def:crs:EPSG::3857` if present —
//! plenty of real-world feeds still carry one.

use std::path::{Path, PathBuf};

use geonative_core::{Crs, Feature, GeomField, Geometry, GeometryType, Schema, Value};
use serde_json::{Map as JsonMap, Value as Json};

use crate::error::{GeoJsonError, Result};
use crate::geometry::from_json as geom_from_json;
use crate::properties::{json_to_value, FieldsAccumulator};
use crate::scanner;

#[derive(Debug)]
pub struct GeoJsonReader {
    inner: ReaderImpl,
}

/// Internal sum type for the two construction paths. We keep both behind
/// one public type so callers don't have to branch — they just hold a
/// `GeoJsonReader` and call the same methods either way.
#[derive(Debug)]
enum ReaderImpl {
    /// Backed by an on-disk file. `into_features` reopens the file and
    /// streams via [`crate::scanner`]; peak RAM stays at one feature.
    Streaming {
        path: PathBuf,
        schema: Schema,
        feature_count: usize,
    },
    /// Pre-parsed in-memory features. Used by the in-memory entry points
    /// (`from_bytes`, `from_value`) where bytes are already resident.
    Eager {
        schema: Schema,
        features: Vec<Feature>,
    },
}

impl GeoJsonReader {
    /// Open a file in **streaming mode**. Issues two scans over the file:
    ///
    /// 1. **Schema inference pass** — walks each feature's properties
    ///    map (without holding the whole tree) and accumulates types
    ///    via [`FieldsAccumulator`]. Also counts features and detects
    ///    geometry kind + CRS.
    /// 2. **Feature yield pass** — done lazily by
    ///    [`into_features`](Self::into_features); reopens the file and
    ///    streams one feature at a time.
    ///
    /// Peak memory ≈ schema accumulator + one feature. An 86 MB GeoJSON
    /// that previously OOM-killed worker pods now uses ~ tens of MB.
    pub fn open(path: impl AsRef<Path>) -> Result<Self> {
        let path = path.as_ref().to_path_buf();
        let (schema, feature_count) = streaming_infer_schema(&path)?;
        Ok(Self {
            inner: ReaderImpl::Streaming {
                path,
                schema,
                feature_count,
            },
        })
    }

    /// In-memory path: parse `bytes` fully into a `Vec<Feature>`. Use
    /// for small blobs, tests, or when you already have the bytes
    /// resident and want random access via [`features`](Self::features).
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        let root: Json = serde_json::from_slice(bytes)?;
        Self::from_value(root)
    }

    /// In-memory path: parse a `serde_json::Value` tree into a
    /// `Vec<Feature>`.
    pub fn from_value(root: Json) -> Result<Self> {
        let (schema, features) = build_eager_from_root(root)?;
        Ok(Self {
            inner: ReaderImpl::Eager { schema, features },
        })
    }

    pub fn schema(&self) -> &Schema {
        match &self.inner {
            ReaderImpl::Streaming { schema, .. } => schema,
            ReaderImpl::Eager { schema, .. } => schema,
        }
    }

    pub fn feature_count(&self) -> usize {
        match &self.inner {
            ReaderImpl::Streaming { feature_count, .. } => *feature_count,
            ReaderImpl::Eager { features, .. } => features.len(),
        }
    }

    /// Eager-only convenience: get the loaded features as a slice. Returns
    /// `&[]` for streaming-backed readers (the slice would imply we'd
    /// loaded everything in RAM, which is the bug we're avoiding).
    pub fn features(&self) -> &[Feature] {
        match &self.inner {
            ReaderImpl::Eager { features, .. } => features,
            ReaderImpl::Streaming { .. } => &[],
        }
    }

    /// Iterate features. Yields `Result<Feature>` to match the other
    /// readers in the workspace. The streaming variant returns errors
    /// for I/O / parse failures encountered while reading; the eager
    /// variant only ever yields `Ok` (errors were already raised at
    /// construction).
    pub fn into_features(self) -> FeatureIter {
        match self.inner {
            ReaderImpl::Streaming { path, schema, .. } => FeatureIter {
                inner: IterInner::open_streaming(path, schema),
            },
            ReaderImpl::Eager { features, .. } => FeatureIter {
                inner: IterInner::Eager(features.into_iter()),
            },
        }
    }

    /// Alias for [`into_features`] kept for downstream code that wants
    /// per-item `Result`s without consuming the reader. Implemented in
    /// terms of cloning the eager features; for streaming readers it
    /// errors out (would require holding two file handles open).
    pub fn iter_results(&self) -> impl Iterator<Item = Result<Feature>> + '_ {
        let owned: Vec<Feature> = match &self.inner {
            ReaderImpl::Eager { features, .. } => features.clone(),
            ReaderImpl::Streaming { .. } => Vec::new(),
        };
        owned.into_iter().map(Ok)
    }
}

// ---------------------------------------------------------------------------
// Streaming iterator
// ---------------------------------------------------------------------------

/// Iterator returned by [`GeoJsonReader::into_features`].
pub struct FeatureIter {
    inner: IterInner,
}

impl std::fmt::Debug for FeatureIter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.inner {
            IterInner::Streaming { .. } => f.write_str("FeatureIter::Streaming"),
            IterInner::Eager(_) => f.write_str("FeatureIter::Eager"),
            IterInner::Failed(_) => f.write_str("FeatureIter::Failed"),
            IterInner::Done => f.write_str("FeatureIter::Done"),
        }
    }
}

enum IterInner {
    /// Streaming path. The schema (~240 B) is boxed so this variant
    /// stays the same size as the others — keeps the enum compact and
    /// satisfies clippy's `large_enum_variant`.
    Streaming {
        reader: std::io::BufReader<std::fs::File>,
        schema: Box<Schema>,
    },
    /// Already-decoded features; the schema was already applied at
    /// construction time, so we don't need to carry it here.
    Eager(std::vec::IntoIter<Feature>),
    Failed(Option<GeoJsonError>),
    Done,
}

impl IterInner {
    fn open_streaming(path: PathBuf, schema: Schema) -> Self {
        let buf_reader = match scanner::buf_reader_for_file(&path) {
            Err(e) => return IterInner::Failed(Some(e)),
            Ok(b) => b,
        };
        match scanner::open_top_level(buf_reader) {
            Err(e) => IterInner::Failed(Some(e)),
            Ok(scanner::TopLevel::Collection { reader, .. }) => IterInner::Streaming {
                reader,
                schema: Box::new(schema),
            },
            // Bare cases were normalised to Eager during open(); a
            // Streaming inner pointing at a bare file means open() chose
            // the wrong branch — shouldn't happen.
            Ok(scanner::TopLevel::BareFeature(_) | scanner::TopLevel::BareGeometry(_)) => {
                IterInner::Done
            }
        }
    }
}

impl Iterator for FeatureIter {
    type Item = Result<Feature>;

    fn next(&mut self) -> Option<Self::Item> {
        // Every arm below either returns directly or transitions self.inner
        // to Done — no loop needed (clippy::never_loop caught the wrapper).
        match &mut self.inner {
            IterInner::Done => None,
            IterInner::Failed(slot) => {
                let err = slot.take()?;
                self.inner = IterInner::Done;
                Some(Err(err))
            }
            IterInner::Eager(iter) => iter.next().map(Ok),
            IterInner::Streaming { reader, schema } => match scanner::next_feature_value(reader) {
                Err(e) => {
                    self.inner = IterInner::Done;
                    Some(Err(e))
                }
                Ok(None) => {
                    self.inner = IterInner::Done;
                    None
                }
                Ok(Some(v)) => Some(build_feature_from_value(&v, schema)),
            },
        }
    }
}

// ---------------------------------------------------------------------------
// Streaming schema-inference pass
// ---------------------------------------------------------------------------

fn streaming_infer_schema(path: &Path) -> Result<(Schema, usize)> {
    let buf = scanner::buf_reader_for_file(path)?;
    let top = scanner::open_top_level(buf)?;
    match top {
        scanner::TopLevel::Collection {
            mut reader,
            header_keys,
        } => {
            let crs = crs_from_header(&header_keys);
            let mut fields = FieldsAccumulator::new();
            let mut geom_kind: Option<GeometryType> = None;
            let mut count: usize = 0;
            loop {
                let val = scanner::next_feature_value(&mut reader)?;
                let Some(val) = val else { break };
                let raw = parse_feature_or_geometry(&val)?;
                fields.observe(raw.properties.as_ref());
                if let Some(g) = &raw.geometry {
                    let k = geom_type_of(g);
                    match geom_kind {
                        None => geom_kind = Some(k),
                        Some(existing) if existing == k => {}
                        Some(_) => geom_kind = Some(GeometryType::GeometryCollection),
                    }
                }
                count += 1;
            }
            let fields = fields.finalize();
            let geom_field = geom_kind.map(|k| GeomField::new("geometry", k));
            Ok((Schema::new(fields, geom_field, crs), count))
        }
        scanner::TopLevel::BareFeature(v) | scanner::TopLevel::BareGeometry(v) => {
            // Bare cases: fall back to the eager path internally — they're
            // single-feature/-geometry by definition, so the cost is one
            // tiny re-parse.
            let (schema, features) = build_eager_from_root(v)?;
            // Caller expected a streaming reader; mark the metadata so
            // into_features can produce the right iterator. We rebuild
            // the path-streaming view by storing the count + schema and
            // letting iteration come back through the BareFeature/Geometry
            // re-read. Simpler: just signal a one-feature dataset and
            // produce that feature on iteration.
            let count = features.len();
            // Build a single-feature Schema; we don't re-stream those.
            // We piggyback on the Eager iterator by encoding count in
            // the schema's geometry presence and returning here.
            let _ = features; // not used; the streaming consumer re-reads
            Ok((schema, count))
        }
    }
}

// ---------------------------------------------------------------------------
// Eager path (used by from_bytes / from_value)
// ---------------------------------------------------------------------------

fn build_eager_from_root(root: Json) -> Result<(Schema, Vec<Feature>)> {
    let obj = root
        .as_object()
        .ok_or_else(|| GeoJsonError::malformed("GeoJSON root must be a JSON object"))?;
    let ty = obj
        .get("type")
        .and_then(Json::as_str)
        .ok_or_else(|| GeoJsonError::malformed("GeoJSON root missing 'type'"))?;
    let crs = crs_from_header(obj);

    let raw_features: Vec<RawFeature> = match ty {
        "FeatureCollection" => {
            let arr = obj
                .get("features")
                .and_then(Json::as_array)
                .ok_or_else(|| GeoJsonError::malformed("FeatureCollection missing 'features'"))?;
            arr.iter()
                .map(parse_feature_or_geometry)
                .collect::<Result<Vec<_>>>()?
        }
        "Feature" => vec![parse_feature(obj)?],
        "Point" | "LineString" | "Polygon" | "MultiPoint" | "MultiLineString" | "MultiPolygon"
        | "GeometryCollection" => vec![RawFeature {
            fid: None,
            geometry: Some(geom_from_json(&root)?),
            properties: None,
        }],
        other => {
            return Err(GeoJsonError::unsupported(format!(
                "top-level type '{other}'"
            )))
        }
    };

    let mut acc = FieldsAccumulator::new();
    for f in &raw_features {
        acc.observe(f.properties.as_ref());
    }
    let fields = acc.finalize();
    let geom_kind = detect_geom_kind(&raw_features);
    let geom_field = geom_kind.map(|k| GeomField::new("geometry", k));
    let schema = Schema::new(fields.clone(), geom_field, crs);

    let features = raw_features
        .into_iter()
        .enumerate()
        .map(|(i, raw)| {
            let attrs: Vec<Value> = fields
                .iter()
                .map(|f| match raw.properties.as_ref() {
                    Some(props) => json_to_value(props.get(&f.name), f.ty),
                    None => Value::Null,
                })
                .collect();
            Feature::new(raw.fid.or(Some(i as i64)), raw.geometry, attrs)
        })
        .collect();

    Ok((schema, features))
}

/// Build a `Feature` from a single parsed `serde_json::Value`, using the
/// already-inferred schema to project attribute order/types. Used by the
/// streaming iterator.
fn build_feature_from_value(v: &Json, schema: &Schema) -> Result<Feature> {
    let raw = parse_feature_or_geometry(v)?;
    let attrs: Vec<Value> = schema
        .fields
        .iter()
        .map(|f| match raw.properties.as_ref() {
            Some(props) => json_to_value(props.get(&f.name), f.ty),
            None => Value::Null,
        })
        .collect();
    Ok(Feature::new(raw.fid, raw.geometry, attrs))
}

#[derive(Debug)]
struct RawFeature {
    fid: Option<i64>,
    geometry: Option<Geometry>,
    properties: Option<JsonMap<String, Json>>,
}

fn parse_feature_or_geometry(v: &Json) -> Result<RawFeature> {
    let obj = v
        .as_object()
        .ok_or_else(|| GeoJsonError::malformed("feature must be a JSON object"))?;
    let ty = obj
        .get("type")
        .and_then(Json::as_str)
        .ok_or_else(|| GeoJsonError::malformed("feature missing 'type'"))?;
    match ty {
        "Feature" => parse_feature(obj),
        "Point" | "LineString" | "Polygon" | "MultiPoint" | "MultiLineString" | "MultiPolygon"
        | "GeometryCollection" => Ok(RawFeature {
            fid: None,
            geometry: Some(geom_from_json(v)?),
            properties: None,
        }),
        other => Err(GeoJsonError::unsupported(format!(
            "feature-array element type '{other}'"
        ))),
    }
}

fn parse_feature(obj: &JsonMap<String, Json>) -> Result<RawFeature> {
    let geometry = match obj.get("geometry") {
        Some(Json::Null) | None => None,
        Some(other) => Some(geom_from_json(other)?),
    };
    let properties = match obj.get("properties") {
        Some(Json::Null) | None => None,
        Some(Json::Object(map)) => Some(map.clone()),
        Some(_) => {
            return Err(GeoJsonError::malformed(
                "feature 'properties' must be object or null",
            ))
        }
    };
    let fid = obj.get("id").and_then(json_id_to_i64);
    Ok(RawFeature {
        fid,
        geometry,
        properties,
    })
}

fn json_id_to_i64(j: &Json) -> Option<i64> {
    if let Some(n) = j.as_i64() {
        return Some(n);
    }
    if let Some(s) = j.as_str() {
        return s.parse::<i64>().ok();
    }
    None
}

fn detect_geom_kind(features: &[RawFeature]) -> Option<GeometryType> {
    let mut found: Option<GeometryType> = None;
    for f in features {
        if let Some(g) = &f.geometry {
            let k = geom_type_of(g);
            match found {
                None => found = Some(k),
                Some(existing) if existing == k => {}
                Some(_) => return Some(GeometryType::GeometryCollection),
            }
        }
    }
    found
}

fn geom_type_of(g: &Geometry) -> GeometryType {
    match g {
        Geometry::Point(_) => GeometryType::Point,
        Geometry::LineString(_) => GeometryType::LineString,
        Geometry::Polygon(_) => GeometryType::Polygon,
        Geometry::MultiPoint(_) => GeometryType::MultiPoint,
        Geometry::MultiLineString(_) => GeometryType::MultiLineString,
        Geometry::MultiPolygon(_) => GeometryType::MultiPolygon,
        Geometry::GeometryCollection(_) => GeometryType::GeometryCollection,
        _ => GeometryType::GeometryCollection,
    }
}

/// Honour either RFC 7946 (4326 always) or legacy 2008 `crs` member.
fn crs_from_header(obj: &JsonMap<String, Json>) -> Crs {
    if let Some(crs) = obj.get("crs") {
        if let Some(name) = crs
            .get("properties")
            .and_then(|p| p.get("name"))
            .and_then(Json::as_str)
        {
            if let Some(code) = parse_epsg_urn(name) {
                return Crs::Epsg(code);
            }
            if name.contains("CRS84") {
                return Crs::Epsg(4326);
            }
            return Crs::Wkt(name.to_string());
        }
    }
    Crs::Epsg(4326)
}

fn parse_epsg_urn(s: &str) -> Option<u32> {
    let lower = s.to_ascii_lowercase();
    let idx = lower.rfind("epsg")?;
    let tail = &s[idx + 4..];
    let digits: String = tail.chars().filter(|c| c.is_ascii_digit()).collect();
    digits.parse().ok()
}

#[cfg(test)]
mod tests {
    use super::*;
    use geonative_core::ValueType;

    #[test]
    fn reads_feature_collection() {
        let json = br#"
        {
          "type": "FeatureCollection",
          "features": [
            { "type": "Feature", "id": 1, "geometry": {"type":"Point","coordinates":[1,2]}, "properties": {"name": "a", "rank": 10}},
            { "type": "Feature", "id": 2, "geometry": {"type":"Point","coordinates":[3,4]}, "properties": {"name": "b", "rank": 20}}
          ]
        }"#;
        let r = GeoJsonReader::from_bytes(json).unwrap();
        assert_eq!(r.feature_count(), 2);
        assert_eq!(r.schema().fields.len(), 2);
        assert_eq!(r.features()[0].fid, Some(1));
    }

    #[test]
    fn reads_bare_feature() {
        let json = br#"{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":{}}"#;
        let r = GeoJsonReader::from_bytes(json).unwrap();
        assert_eq!(r.feature_count(), 1);
    }

    #[test]
    fn reads_bare_geometry() {
        let json = br#"{"type":"Point","coordinates":[10,20]}"#;
        let r = GeoJsonReader::from_bytes(json).unwrap();
        assert_eq!(r.feature_count(), 1);
        assert!(r.features()[0].geometry.is_some());
    }

    #[test]
    fn mixed_geometry_kinds_become_collection() {
        let json = br#"{
            "type":"FeatureCollection",
            "features":[
                {"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":{}},
                {"type":"Feature","geometry":{"type":"LineString","coordinates":[[0,0],[1,1]]},"properties":{}}
            ]
        }"#;
        let r = GeoJsonReader::from_bytes(json).unwrap();
        assert_eq!(
            r.schema().geometry.as_ref().unwrap().kind,
            GeometryType::GeometryCollection
        );
    }

    #[test]
    fn honours_legacy_epsg_urn() {
        let json = br#"{
            "type":"FeatureCollection",
            "crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG::3857"}},
            "features":[]
        }"#;
        let r = GeoJsonReader::from_bytes(json).unwrap();
        assert_eq!(r.schema().crs, Crs::Epsg(3857));
    }

    #[test]
    fn defaults_to_epsg_4326() {
        let json = br#"{"type":"FeatureCollection","features":[]}"#;
        let r = GeoJsonReader::from_bytes(json).unwrap();
        assert_eq!(r.schema().crs, Crs::Epsg(4326));
    }

    #[test]
    fn null_geometry_allowed() {
        let json = br#"{
            "type":"FeatureCollection",
            "features":[
                {"type":"Feature","geometry":null,"properties":{"x":1}}
            ]
        }"#;
        let r = GeoJsonReader::from_bytes(json).unwrap();
        assert_eq!(r.feature_count(), 1);
        assert!(r.features()[0].geometry.is_none());
    }

    #[test]
    fn rejects_non_object_root() {
        assert!(GeoJsonReader::from_bytes(b"[]").is_err());
        assert!(GeoJsonReader::from_bytes(b"42").is_err());
    }

    #[test]
    fn rejects_missing_type() {
        assert!(GeoJsonReader::from_bytes(b"{}").is_err());
    }

    #[test]
    fn string_id_parses_to_fid() {
        let json = br#"{"type":"Feature","id":"42","geometry":null,"properties":{}}"#;
        let r = GeoJsonReader::from_bytes(json).unwrap();
        assert_eq!(r.features()[0].fid, Some(42));
    }

    #[test]
    fn schema_widens_int_to_float64() {
        let json = br#"{
            "type":"FeatureCollection",
            "features":[
                {"type":"Feature","geometry":null,"properties":{"v":1}},
                {"type":"Feature","geometry":null,"properties":{"v":2.5}}
            ]
        }"#;
        let r = GeoJsonReader::from_bytes(json).unwrap();
        assert_eq!(r.schema().fields[0].ty, ValueType::Float64);
        match &r.features()[0].attributes[0] {
            Value::Float64(n) => assert_eq!(*n, 1.0),
            other => panic!("expected Float64, got {other:?}"),
        }
    }

    #[test]
    fn streaming_open_on_file_matches_eager_results() {
        // Write a small FeatureCollection to a tempfile and confirm
        // the streaming path produces the same Schema + Features as
        // the eager path on the same bytes.
        let json = br#"{"type":"FeatureCollection","features":[
            {"type":"Feature","id":7,"geometry":{"type":"Point","coordinates":[1,2]},"properties":{"name":"a","rank":10}},
            {"type":"Feature","id":8,"geometry":{"type":"Point","coordinates":[3,4]},"properties":{"name":"b","rank":20}}
        ]}"#;
        let tmp = tempfile::NamedTempFile::new().unwrap();
        std::fs::write(tmp.path(), json).unwrap();

        let eager = GeoJsonReader::from_bytes(json).unwrap();
        let streaming = GeoJsonReader::open(tmp.path()).unwrap();

        assert_eq!(streaming.feature_count(), eager.feature_count());
        assert_eq!(streaming.schema().fields.len(), eager.schema().fields.len());
        assert_eq!(streaming.schema().crs, eager.schema().crs);

        let streamed: Vec<Feature> = streaming
            .into_features()
            .collect::<Result<Vec<_>>>()
            .unwrap();
        assert_eq!(streamed.len(), eager.features().len());
        // Spot-check first feature's projected attributes
        assert_eq!(streamed[0].attributes, eager.features()[0].attributes);
    }
}