city3d_stac 0.1.0

Generate STAC metadata for CityJSON datasets
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
//! CityJSON Text Sequences (CityJSONSeq) format reader
//!
//! Reads `.city.jsonl` or `.cjseq` files which contain JSON Text Sequences
//! as specified in the CityJSON 2.0 specification.
//!
//! The format consists of:
//! - First line: CityJSON header with metadata, transform, and empty CityObjects/vertices
//! - Subsequent lines: CityJSONFeature objects, each with their own vertices

use crate::error::{CityJsonStacError, Result};
use crate::metadata::{AttributeDefinition, AttributeType, BBox3D, Transform, CRS};
use crate::reader::CityModelMetadataReader;

use serde_json::Value;
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::sync::RwLock;

/// Aggregated metadata computed from all features in the CityJSONSeq file
#[derive(Clone)]
struct AggregatedMetadata {
    /// All unique LODs across all features.
    lods: HashSet<String>,
    /// All unique city object types (excluding extension types starting with '+')
    city_object_types: HashSet<String>,
    /// Total count of city objects across all features
    city_object_count: usize,
    /// All attributes found across features
    attributes: HashMap<String, AttributeType>,
    /// Whether any feature has semantic surfaces
    has_semantic_surfaces: bool,
    /// Whether any feature has textures
    has_textures: bool,
    /// Whether any feature has materials
    has_materials: bool,
}

/// Reader for CityJSON Text Sequences format files (.city.jsonl, .jsonl, .cjseq)
///
/// Uses streaming approach: reads the first line as metadata header,
/// then streams through remaining features to aggregate statistics.
pub struct CityJSONSeqReader {
    file_path: PathBuf,
    /// Metadata header from first line (typed CityJSON struct)
    metadata_header: cjseq::CityJSON,
    /// Aggregated statistics (computed during construction)
    aggregated: RwLock<Option<AggregatedMetadata>>,
}

impl CityJSONSeqReader {
    /// Create a new CityJSONSeq reader
    ///
    /// This reads the first line as the metadata header and then streams
    /// through all features to aggregate statistics.
    pub fn new(file_path: &Path) -> Result<Self> {
        if !file_path.exists() {
            return Err(CityJsonStacError::IoError(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("File not found: {}", file_path.display()),
            )));
        }

        let file = File::open(file_path)?;
        let reader = BufReader::new(file);
        let mut lines = reader.lines();

        // First line: CityJSON header (metadata only)
        let first_line = lines
            .next()
            .ok_or_else(|| CityJsonStacError::Other("Empty CityJSONSeq file".to_string()))??;

        let metadata_header =
            super::parse_cityjson(&first_line).map_err(CityJsonStacError::Other)?;

        // Stream through remaining lines to aggregate statistics
        let mut aggregated = AggregatedMetadata {
            lods: HashSet::new(),
            city_object_types: HashSet::new(),
            city_object_count: 0,
            attributes: HashMap::new(),
            has_semantic_surfaces: false,
            has_textures: false,
            has_materials: false,
        };

        // Process each feature line
        for line_result in lines {
            let line = line_result?;
            if line.trim().is_empty() {
                continue; // Skip empty lines
            }

            let feature = cjseq::CityJSONFeature::from_str(&line).map_err(|e| {
                CityJsonStacError::Other(format!("Failed to parse CityJSONFeature: {e}"))
            })?;
            Self::process_feature(&feature, &mut aggregated);
        }

        Ok(Self {
            file_path: file_path.to_path_buf(),
            metadata_header,
            aggregated: RwLock::new(Some(aggregated)),
        })
    }

    /// Create a CityJSONSeq reader from in-memory content
    ///
    /// This is used for remote files that have been downloaded as strings.
    /// The `virtual_path` is used for display purposes (e.g., the original filename).
    pub fn from_content(content: &str, virtual_path: PathBuf) -> Result<Self> {
        let mut lines = content.lines();

        // First line: CityJSON header (metadata only)
        let first_line = lines
            .next()
            .ok_or_else(|| CityJsonStacError::Other("Empty CityJSONSeq content".to_string()))?;

        let metadata_header =
            super::parse_cityjson(first_line).map_err(CityJsonStacError::Other)?;

        // Stream through remaining lines to aggregate statistics
        let mut aggregated = AggregatedMetadata {
            lods: HashSet::new(),
            city_object_types: HashSet::new(),
            city_object_count: 0,
            attributes: HashMap::new(),
            has_semantic_surfaces: false,
            has_textures: false,
            has_materials: false,
        };

        for line in lines {
            if line.trim().is_empty() {
                continue;
            }

            let feature = cjseq::CityJSONFeature::from_str(line).map_err(|e| {
                CityJsonStacError::Other(format!("Failed to parse CityJSONFeature: {e}"))
            })?;
            Self::process_feature(&feature, &mut aggregated);
        }

        Ok(Self {
            file_path: virtual_path,
            metadata_header,
            aggregated: RwLock::new(Some(aggregated)),
        })
    }

    /// Create a CityJSONSeq reader by streaming from a remote URL
    ///
    /// Instead of downloading the entire file into memory, this streams the
    /// data line-by-line. For HTTP/HTTPS URLs, uses `reqwest` directly for
    /// broader server compatibility. For cloud storage URLs (s3://, gs://, az://),
    /// uses `object_store` for native protocol support.
    ///
    /// This is more memory-efficient for large remote `.jsonl` files since
    /// only one line is held in memory at a time.
    pub async fn from_url_stream(url: &str, virtual_path: PathBuf) -> Result<Self> {
        use futures::TryStreamExt;
        use tokio::io::AsyncBufReadExt;
        use tokio_util::io::StreamReader;

        log::info!("Streaming CityJSONSeq from: {}", url);

        let parsed_url = url::Url::parse(url).map_err(CityJsonStacError::UrlError)?;

        // Choose streaming backend based on URL scheme
        let stream: Box<
            dyn futures::Stream<Item = std::result::Result<bytes::Bytes, std::io::Error>>
                + Send
                + Unpin,
        > = match parsed_url.scheme() {
            "s3" | "gs" | "az" | "azure" => {
                let (store, path) =
                    object_store::parse_url_opts(&parsed_url, Vec::<(String, String)>::new())
                        .map_err(|e| {
                            CityJsonStacError::StorageError(format!(
                                "Failed to create object store: {e}"
                            ))
                        })?;

                let result = store.get(&path).await?;
                Box::new(result.into_stream().map_err(std::io::Error::other))
            }
            "http" | "https" => {
                let response = reqwest::get(url).await.map_err(|e| {
                    CityJsonStacError::StorageError(format!("HTTP request failed: {e}"))
                })?;

                if !response.status().is_success() {
                    return Err(CityJsonStacError::StorageError(format!(
                        "HTTP {} for {}",
                        response.status(),
                        url
                    )));
                }

                Box::new(response.bytes_stream().map_err(std::io::Error::other))
            }
            scheme => {
                return Err(CityJsonStacError::StorageError(format!(
                    "Unsupported URL scheme: {scheme}"
                )));
            }
        };

        let async_reader = StreamReader::new(stream);
        let buf_reader = tokio::io::BufReader::new(async_reader);
        let mut lines = buf_reader.lines();

        // First line: CityJSON header (metadata only)
        let first_line = lines
            .next_line()
            .await
            .map_err(|e| CityJsonStacError::Other(format!("Failed to read stream: {e}")))?
            .ok_or_else(|| CityJsonStacError::Other("Empty CityJSONSeq stream".to_string()))?;

        let metadata_header =
            super::parse_cityjson(&first_line).map_err(CityJsonStacError::Other)?;

        // Stream through remaining lines to aggregate statistics
        let mut aggregated = AggregatedMetadata {
            lods: HashSet::new(),
            city_object_types: HashSet::new(),
            city_object_count: 0,
            attributes: HashMap::new(),
            has_semantic_surfaces: false,
            has_textures: false,
            has_materials: false,
        };

        while let Some(line) = lines
            .next_line()
            .await
            .map_err(|e| CityJsonStacError::Other(format!("Failed to read stream: {e}")))?
        {
            if line.trim().is_empty() {
                continue;
            }

            let feature = cjseq::CityJSONFeature::from_str(&line).map_err(|e| {
                CityJsonStacError::Other(format!("Failed to parse CityJSONFeature: {e}"))
            })?;
            Self::process_feature(&feature, &mut aggregated);
        }

        log::debug!(
            "Streamed {} city objects from {}",
            aggregated.city_object_count,
            url
        );

        Ok(Self {
            file_path: virtual_path,
            metadata_header,
            aggregated: RwLock::new(Some(aggregated)),
        })
    }

    /// Process a single feature and update aggregated statistics
    fn process_feature(feature: &cjseq::CityJSONFeature, aggregated: &mut AggregatedMetadata) {
        for city_object in feature.city_objects.values() {
            // Collect city object types (excluding extension types starting with '+')
            if !city_object.thetype.starts_with('+') {
                aggregated
                    .city_object_types
                    .insert(city_object.thetype.clone());
            }

            // Collect LODs from geometry and check for semantic surfaces
            if let Some(ref geometries) = city_object.geometry {
                for geom in geometries {
                    if let Some(ref lod) = geom.lod {
                        aggregated.lods.insert(lod.clone());
                    }
                    // Check for semantic surfaces
                    if geom.semantics.is_some() {
                        aggregated.has_semantic_surfaces = true;
                    }
                }
            }

            // Collect attributes
            if let Some(ref attrs) = city_object.attributes {
                if let Some(attrs_obj) = attrs.as_object() {
                    for (attr_name, attr_value) in attrs_obj {
                        let attr_type = match attr_value {
                            Value::String(_) => AttributeType::String,
                            Value::Number(_) => AttributeType::Number,
                            Value::Bool(_) => AttributeType::Boolean,
                            Value::Array(_) => AttributeType::Array,
                            Value::Object(_) => AttributeType::Object,
                            Value::Null => continue,
                        };

                        // Merge attribute types: if conflicting types, use String
                        aggregated
                            .attributes
                            .entry(attr_name.clone())
                            .and_modify(|existing| {
                                if *existing != attr_type {
                                    *existing = AttributeType::String;
                                }
                            })
                            .or_insert(attr_type);
                    }
                }
            }

            // Increment count
            aggregated.city_object_count += 1;
        }

        // Check for textures and materials in appearance
        if let Some(ref appearance) = feature.appearance {
            if appearance.textures.is_some() {
                aggregated.has_textures = true;
            }
            if appearance.materials.is_some() {
                aggregated.has_materials = true;
            }
        }
    }

    /// Get the aggregated metadata (lazy loaded)
    fn get_aggregated(&self) -> Result<AggregatedMetadata> {
        // First check with a read lock
        {
            let aggregated = self
                .aggregated
                .read()
                .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
            if let Some(ref agg) = *aggregated {
                return Ok(agg.clone());
            }
        }

        // This shouldn't happen since we populate it in new(), but handle gracefully
        Err(CityJsonStacError::Other(
            "Aggregated metadata not initialized".to_string(),
        ))
    }

    /// Extract CRS from metadata header
    fn extract_crs_from_header(&self) -> CRS {
        if let Some(ref metadata) = self.metadata_header.metadata {
            if let Some(ref rs) = metadata.reference_system {
                // cjseq::ReferenceSystem has authority and code fields
                if rs.authority == "EPSG" {
                    if let Ok(code) = rs.code.parse::<u32>() {
                        return CRS::from_epsg(code);
                    }
                }
            }
        }
        CRS::default()
    }

    /// Extract transform from metadata header
    fn extract_transform_from_header(&self) -> Option<Transform> {
        let scale = &self.metadata_header.transform.scale;
        let translate = &self.metadata_header.transform.translate;

        if scale.len() == 3 && translate.len() == 3 {
            // Check if transform is the default identity (no actual transform)
            let is_default = scale[0] == 1.0
                && scale[1] == 1.0
                && scale[2] == 1.0
                && translate[0] == 0.0
                && translate[1] == 0.0
                && translate[2] == 0.0;

            if is_default {
                return None;
            }

            Some(Transform::new(
                [scale[0], scale[1], scale[2]],
                [translate[0], translate[1], translate[2]],
            ))
        } else {
            None
        }
    }

    /// Extract bbox from metadata header
    fn extract_bbox_from_header(&self) -> Result<BBox3D> {
        if let Some(ref metadata) = self.metadata_header.metadata {
            if let Some(extent) = metadata.geographical_extent {
                return Ok(BBox3D::new(
                    extent[0], extent[1], extent[2], extent[3], extent[4], extent[5],
                ));
            }
        }
        // Default bbox if not found
        Ok(BBox3D::new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0))
    }
}

impl CityModelMetadataReader for CityJSONSeqReader {
    fn bbox(&self) -> Result<BBox3D> {
        self.extract_bbox_from_header()
    }

    fn crs(&self) -> Result<CRS> {
        Ok(self.extract_crs_from_header())
    }

    fn lods(&self) -> Result<Vec<String>> {
        let aggregated = self.get_aggregated()?;
        let mut lods: Vec<String> = aggregated.lods.into_iter().collect();
        lods.sort();
        Ok(lods)
    }

    fn city_object_types(&self) -> Result<Vec<String>> {
        let aggregated = self.get_aggregated()?;
        let mut types: Vec<String> = aggregated.city_object_types.into_iter().collect();
        types.sort();
        Ok(types)
    }

    fn city_object_count(&self) -> Result<usize> {
        let aggregated = self.get_aggregated()?;
        Ok(aggregated.city_object_count)
    }

    fn attributes(&self) -> Result<Vec<AttributeDefinition>> {
        let aggregated = self.get_aggregated()?;
        let mut attributes: Vec<_> = aggregated
            .attributes
            .into_iter()
            .map(|(name, attr_type)| AttributeDefinition::new(&name, attr_type))
            .collect();
        attributes.sort_by(|a, b| a.name.cmp(&b.name));
        Ok(attributes)
    }

    fn encoding(&self) -> &'static str {
        "CityJSONSeq"
    }

    fn version(&self) -> Result<String> {
        Ok(self.metadata_header.version.clone())
    }

    fn file_path(&self) -> &Path {
        &self.file_path
    }

    fn transform(&self) -> Result<Option<Transform>> {
        Ok(self.extract_transform_from_header())
    }

    fn metadata(&self) -> Result<Option<Value>> {
        match &self.metadata_header.metadata {
            Some(m) => {
                let value = serde_json::to_value(m).map_err(|e| {
                    CityJsonStacError::Other(format!("Failed to serialize metadata: {e}"))
                })?;
                Ok(Some(value))
            }
            None => Ok(None),
        }
    }

    fn extensions(&self) -> Result<Vec<String>> {
        if let Some(ref ext) = self.metadata_header.extensions {
            if let Some(ext_obj) = ext.as_object() {
                let mut extensions: Vec<String> = ext_obj.keys().cloned().collect();
                extensions.sort();
                return Ok(extensions);
            }
        }
        Ok(Vec::new())
    }

    fn semantic_surfaces(&self) -> Result<bool> {
        let aggregated = self.get_aggregated()?;
        Ok(aggregated.has_semantic_surfaces)
    }

    fn textures(&self) -> Result<bool> {
        let aggregated = self.get_aggregated()?;
        Ok(aggregated.has_textures)
    }

    fn materials(&self) -> Result<bool> {
        let aggregated = self.get_aggregated()?;
        Ok(aggregated.has_materials)
    }

    fn streamable(&self) -> bool {
        true
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    fn create_test_cityjsonseq() -> NamedTempFile {
        let mut temp_file = NamedTempFile::new().unwrap();

        // Header line (CityJSON with metadata but no CityObjects)
        let header = r#"{"type":"CityJSON","version":"2.0","transform":{"scale":[0.01,0.01,0.01],"translate":[100000,200000,0]},"CityObjects":{},"vertices":[],"metadata":{"geographicalExtent":[1.0,2.0,0.0,10.0,20.0,30.0],"referenceSystem":"https://www.opengis.net/def/crs/EPSG/0/7415"}}"#;

        // Feature line 1
        let feature1 = r#"{"type":"CityJSONFeature","id":"building1","CityObjects":{"building1":{"type":"Building","geometry":[{"type":"Solid","lod":"2","boundaries":[[[[0,0,0]]]]}],"attributes":{"yearOfConstruction":2020,"function":"residential"}}},"vertices":[[1000,2000,3000]]}"#;

        // Feature line 2
        let feature2 = r#"{"type":"CityJSONFeature","id":"building2","CityObjects":{"building2":{"type":"Building","geometry":[{"type":"Solid","lod":"2.2","boundaries":[[[[0,0,0]]]]}],"attributes":{"yearOfConstruction":2021}}},"vertices":[[2000,3000,4000]]}"#;

        writeln!(temp_file, "{}", header).unwrap();
        writeln!(temp_file, "{}", feature1).unwrap();
        writeln!(temp_file, "{}", feature2).unwrap();
        temp_file.flush().unwrap();
        temp_file
    }

    #[test]
    fn test_cityjsonseq_reader_creation() {
        let temp_file = create_test_cityjsonseq();
        let reader = CityJSONSeqReader::new(temp_file.path());
        assert!(reader.is_ok());
    }

    #[test]
    fn test_cityjsonseq_reader_not_found() {
        let reader = CityJSONSeqReader::new(Path::new("/nonexistent/file.jsonl"));
        assert!(reader.is_err());
    }

    #[test]
    fn test_cityjsonseq_extract_version() {
        let temp_file = create_test_cityjsonseq();
        let reader = CityJSONSeqReader::new(temp_file.path()).unwrap();
        let version = reader.version().unwrap();
        assert_eq!(version, "2.0");
    }

    #[test]
    fn test_cityjsonseq_extract_city_objects_count() {
        let temp_file = create_test_cityjsonseq();
        let reader = CityJSONSeqReader::new(temp_file.path()).unwrap();
        let count = reader.city_object_count().unwrap();
        assert_eq!(count, 2); // 2 features, each with 1 city object
    }

    #[test]
    fn test_cityjsonseq_extract_types() {
        let temp_file = create_test_cityjsonseq();
        let reader = CityJSONSeqReader::new(temp_file.path()).unwrap();
        let types = reader.city_object_types().unwrap();
        assert_eq!(types, vec!["Building"]);
    }

    #[test]
    fn test_cityjsonseq_extract_lods() {
        let temp_file = create_test_cityjsonseq();
        let reader = CityJSONSeqReader::new(temp_file.path()).unwrap();
        let lods = reader.lods().unwrap();
        assert!(lods.contains(&"2".to_string()));
        assert!(lods.contains(&"2.2".to_string()));
    }

    #[test]
    fn test_cityjsonseq_extract_bbox() {
        let temp_file = create_test_cityjsonseq();
        let reader = CityJSONSeqReader::new(temp_file.path()).unwrap();
        let bbox = reader.bbox().unwrap();
        assert_eq!(bbox.xmin, 1.0);
        assert_eq!(bbox.ymin, 2.0);
        assert_eq!(bbox.xmax, 10.0);
        assert_eq!(bbox.ymax, 20.0);
    }

    #[test]
    fn test_cityjsonseq_extract_crs() {
        let temp_file = create_test_cityjsonseq();
        let reader = CityJSONSeqReader::new(temp_file.path()).unwrap();
        let crs = reader.crs().unwrap();
        assert_eq!(crs.epsg, Some(7415));
    }

    #[test]
    fn test_cityjsonseq_extract_attributes() {
        let temp_file = create_test_cityjsonseq();
        let reader = CityJSONSeqReader::new(temp_file.path()).unwrap();
        let attrs = reader.attributes().unwrap();

        let attr_names: Vec<&str> = attrs.iter().map(|a| a.name.as_str()).collect();
        assert!(attr_names.contains(&"yearOfConstruction"));
        assert!(attr_names.contains(&"function"));
    }

    #[test]
    fn test_cityjsonseq_encoding() {
        let temp_file = create_test_cityjsonseq();
        let reader = CityJSONSeqReader::new(temp_file.path()).unwrap();
        assert_eq!(reader.encoding(), "CityJSONSeq");
    }

    #[test]
    fn test_cityjsonseq_transform() {
        let temp_file = create_test_cityjsonseq();
        let reader = CityJSONSeqReader::new(temp_file.path()).unwrap();
        let transform = reader.transform().unwrap();
        assert!(transform.is_some());

        let t = transform.unwrap();
        assert_eq!(t.scale, [0.01, 0.01, 0.01]);
        assert_eq!(t.translate, [100000.0, 200000.0, 0.0]);
    }

    #[test]
    fn test_cityjsonseq_metadata() {
        let temp_file = create_test_cityjsonseq();
        let reader = CityJSONSeqReader::new(temp_file.path()).unwrap();
        let metadata = reader.metadata().unwrap();
        assert!(metadata.is_some());
    }

    #[test]
    fn test_cityjsonseq_semantic_surfaces() {
        let mut temp_file = NamedTempFile::new().unwrap();
        let header = r#"{"type":"CityJSON","version":"2.0","transform":{"scale":[1.0,1.0,1.0],"translate":[0,0,0]},"CityObjects":{},"vertices":[],"metadata":{"geographicalExtent":[0,0,0,1,1,1]}}"#;
        let feature = r#"{"type":"CityJSONFeature","id":"b1","CityObjects":{"b1":{"type":"Building","geometry":[{"type":"Solid","lod":"2","boundaries":[[[[0,0,0]]]],"semantics":{"surfaces":[{"type":"WallSurface"}],"values":[[[0]]]}}]}},"vertices":[[0,0,0]]}"#;

        writeln!(temp_file, "{}", header).unwrap();
        writeln!(temp_file, "{}", feature).unwrap();
        temp_file.flush().unwrap();

        let reader = CityJSONSeqReader::new(temp_file.path()).unwrap();
        assert!(reader.semantic_surfaces().unwrap());
    }

    #[test]
    fn test_cityjsonseq_no_semantic_surfaces() {
        let temp_file = create_test_cityjsonseq();
        let reader = CityJSONSeqReader::new(temp_file.path()).unwrap();
        assert!(!reader.semantic_surfaces().unwrap());
    }

    #[test]
    fn test_cityjsonseq_from_content() {
        let content = [
            r#"{"type":"CityJSON","version":"2.0","transform":{"scale":[0.01,0.01,0.01],"translate":[100000,200000,0]},"CityObjects":{},"vertices":[],"metadata":{"geographicalExtent":[1.0,2.0,0.0,10.0,20.0,30.0],"referenceSystem":"https://www.opengis.net/def/crs/EPSG/0/7415"}}"#,
            r#"{"type":"CityJSONFeature","id":"building1","CityObjects":{"building1":{"type":"Building","geometry":[{"type":"Solid","lod":"2","boundaries":[[[[0,0,0]]]]}],"attributes":{"yearOfConstruction":2020}}},"vertices":[[1000,2000,3000]]}"#,
            r#"{"type":"CityJSONFeature","id":"building2","CityObjects":{"building2":{"type":"Building","geometry":[{"type":"Solid","lod":"2.2","boundaries":[[[[0,0,0]]]]}]}},"vertices":[[2000,3000,4000]]}"#,
        ].join("\n");

        let reader =
            CityJSONSeqReader::from_content(&content, PathBuf::from("remote.city.jsonl")).unwrap();

        assert_eq!(reader.version().unwrap(), "2.0");
        assert_eq!(reader.city_object_count().unwrap(), 2);
        assert_eq!(reader.city_object_types().unwrap(), vec!["Building"]);
        assert_eq!(reader.crs().unwrap().epsg, Some(7415));
        assert_eq!(reader.file_path(), Path::new("remote.city.jsonl"));
        assert_eq!(reader.encoding(), "CityJSONSeq");

        let lods = reader.lods().unwrap();
        assert!(lods.contains(&"2".to_string()));
        assert!(lods.contains(&"2.2".to_string()));
    }

    #[test]
    fn test_cityjsonseq_from_content_empty() {
        let result = CityJSONSeqReader::from_content("", PathBuf::from("empty.jsonl"));
        assert!(result.is_err());
    }

    #[test]
    fn test_cityjsonseq_from_content_invalid_header() {
        let result = CityJSONSeqReader::from_content("not valid json", PathBuf::from("bad.jsonl"));
        assert!(result.is_err());
    }

    #[test]
    fn test_cityjsonseq_streamable() {
        let temp_file = create_test_cityjsonseq();
        let reader = CityJSONSeqReader::new(temp_file.path()).unwrap();
        assert!(reader.streamable());
    }

    #[test]
    fn test_cityjsonseq_from_content_streamable() {
        let content = [
            r#"{"type":"CityJSON","version":"2.0","transform":{"scale":[1.0,1.0,1.0],"translate":[0,0,0]},"CityObjects":{},"vertices":[],"metadata":{"geographicalExtent":[0,0,0,1,1,1]}}"#,
            r#"{"type":"CityJSONFeature","id":"b1","CityObjects":{"b1":{"type":"Building","geometry":[]}},"vertices":[]}"#,
        ].join("\n");

        let reader =
            CityJSONSeqReader::from_content(&content, PathBuf::from("test.jsonl")).unwrap();
        assert!(reader.streamable());
    }
}