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
//! FlatCityBuf format reader (.fcb)
//!
//! Reads the binary FlatCityBuf format which is an efficient serialization
//! of CityJSON using FlatBuffers.

use crate::error::{CityJsonStacError, Result};
use crate::metadata::{AttributeDefinition, AttributeType, BBox3D, Transform, CRS};
use crate::reader::CityModelMetadataReader;
use fcb_core::fb::feature_generated::CityObjectType;
use fcb_core::fb::header_generated::ColumnType;
use fcb_core::FcbReader;
use std::collections::BTreeSet;
use std::fs::File;
use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::sync::RwLock;

/// Reader for FlatCityBuf format files (.fcb)
///
/// Uses `RwLock` for interior mutability to enable lazy loading
/// while maintaining thread-safety (`Send + Sync` bounds).
///
/// The FlatCityBuf format stores CityJSON data in a binary format using FlatBuffers,
/// which allows for efficient random access and streaming reads.
pub struct FlatCityBufReader {
    file_path: PathBuf,
    /// Cached FcbReader (lazy loaded via interior mutability)
    /// We cache the header data rather than the reader itself because
    /// FcbReader holds a reference to the underlying file
    cached_header_data: RwLock<Option<CachedHeaderData>>,
    /// Cached data from streaming through features (lazy loaded separately)
    cached_streamed_data: RwLock<Option<CachedStreamedData>>,
}

/// Cached data extracted by streaming through FCB features
/// This is loaded separately from header data since it requires full file traversal
struct CachedStreamedData {
    lods: Vec<String>,
    city_object_types: Vec<String>,
    /// Total count of CityObjects across all features
    /// Note: A single FCB feature can contain multiple CityObjects
    city_object_count: usize,
}

/// Cached data extracted from the FCB header
/// This avoids keeping a reference to the file's lifetime
struct CachedHeaderData {
    version: String,
    geographical_extent: Option<(f64, f64, f64, f64, f64, f64)>,
    reference_system_code: Option<i32>,
    transform: Option<(f64, f64, f64, f64, f64, f64)>, // scale_x, scale_y, scale_z, translate_x, translate_y, translate_z
    columns: Vec<FcbColumn>,
    metadata_json: Option<serde_json::Value>,
    extensions: Vec<String>,
}

/// Simplified column representation extracted from FCB header
struct FcbColumn {
    name: String,
    column_type: u8,
    nullable: bool,
    description: Option<String>,
}

impl FlatCityBufReader {
    /// Create a new FlatCityBuf reader
    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()),
            )));
        }

        Ok(Self {
            file_path: file_path.to_path_buf(),
            cached_header_data: RwLock::new(None),
            cached_streamed_data: RwLock::new(None),
        })
    }

    /// Lazy load and cache header data using interior mutability
    fn ensure_loaded(&self) -> Result<()> {
        // First check if already loaded with a read lock (cheaper)
        {
            let data = self
                .cached_header_data
                .read()
                .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
            if data.is_some() {
                return Ok(());
            }
        }

        // Not loaded, acquire write lock and load
        let mut data = self
            .cached_header_data
            .write()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire write lock".to_string()))?;

        // Double-check after acquiring write lock
        if data.is_none() {
            let file = File::open(&self.file_path)?;
            let reader = BufReader::new(file);

            let fcb_reader = FcbReader::open(reader)
                .map_err(|e| CityJsonStacError::Other(format!("Failed to open FCB file: {e}")))?;

            let header = fcb_reader.header();

            // Extract geographical extent
            let geographical_extent = header.geographical_extent().map(|ge| {
                let min = ge.min();
                let max = ge.max();
                (min.x(), min.y(), min.z(), max.x(), max.y(), max.z())
            });

            // Extract reference system code
            let reference_system_code = header.reference_system().map(|rs| rs.code());

            // Extract transform
            let transform = header.transform().map(|t| {
                let scale = t.scale();
                let translate = t.translate();
                (
                    scale.x(),
                    scale.y(),
                    scale.z(),
                    translate.x(),
                    translate.y(),
                    translate.z(),
                )
            });

            // Extract columns for attribute definitions
            let columns: Vec<FcbColumn> = header
                .columns()
                .map(|cols| {
                    cols.iter()
                        .map(|col| FcbColumn {
                            name: col.name().to_string(),
                            column_type: col.type_().0,
                            nullable: col.nullable(),
                            description: col.description().map(|d| d.to_string()),
                        })
                        .collect()
                })
                .unwrap_or_default();

            // Extract metadata using the deserializer
            // Note: fcb_core::deserializer::to_cj_metadata might not be public,
            // so we construct metadata from available header fields
            let metadata_json = Self::extract_metadata_from_header(&header);

            // Get version
            let version = header.version().to_string();

            // Extract extensions
            let mut extensions = Vec::new();
            if let Some(extensions_vec) = header.extensions() {
                for extension in extensions_vec.iter() {
                    if let Some(url) = extension.url() {
                        extensions.push(url.to_string());
                    }
                }
            }
            extensions.sort();

            *data = Some(CachedHeaderData {
                version,
                geographical_extent,
                reference_system_code,
                transform,
                columns,
                metadata_json,
                extensions,
            });
        }
        Ok(())
    }

    /// Extract metadata as JSON from the header
    fn extract_metadata_from_header(header: &fcb_core::fb::Header) -> Option<serde_json::Value> {
        let mut metadata = serde_json::Map::new();

        // Add identifier if present
        if let Some(identifier) = header.identifier() {
            metadata.insert(
                "identifier".to_string(),
                serde_json::Value::String(identifier.to_string()),
            );
        }

        // Add title if present
        if let Some(title) = header.title() {
            metadata.insert(
                "title".to_string(),
                serde_json::Value::String(title.to_string()),
            );
        }

        // Add reference date if present
        if let Some(ref_date) = header.reference_date() {
            metadata.insert(
                "referenceDate".to_string(),
                serde_json::Value::String(ref_date.to_string()),
            );
        }

        // Add point of contact if present
        if let Some(poc_name) = header.poc_contact_name() {
            let mut poc = serde_json::Map::new();
            poc.insert(
                "contactName".to_string(),
                serde_json::Value::String(poc_name.to_string()),
            );

            if let Some(contact_type) = header.poc_contact_type() {
                poc.insert(
                    "contactType".to_string(),
                    serde_json::Value::String(contact_type.to_string()),
                );
            }
            if let Some(role) = header.poc_role() {
                poc.insert(
                    "role".to_string(),
                    serde_json::Value::String(role.to_string()),
                );
            }
            if let Some(email) = header.poc_email() {
                poc.insert(
                    "email".to_string(),
                    serde_json::Value::String(email.to_string()),
                );
            }
            if let Some(phone) = header.poc_phone() {
                poc.insert(
                    "phone".to_string(),
                    serde_json::Value::String(phone.to_string()),
                );
            }
            if let Some(website) = header.poc_website() {
                poc.insert(
                    "website".to_string(),
                    serde_json::Value::String(website.to_string()),
                );
            }

            metadata.insert("pointOfContact".to_string(), serde_json::Value::Object(poc));
        }

        // Add geographical extent
        if let Some(ge) = header.geographical_extent() {
            let min = ge.min();
            let max = ge.max();
            metadata.insert(
                "geographicalExtent".to_string(),
                serde_json::json!([min.x(), min.y(), min.z(), max.x(), max.y(), max.z()]),
            );
        }

        // Add reference system
        if let Some(rs) = header.reference_system() {
            let epsg_code = rs.code();
            // Construct the CityJSON-style reference system URL
            let ref_system_url = format!("https://www.opengis.net/def/crs/EPSG/0/{epsg_code}");
            metadata.insert(
                "referenceSystem".to_string(),
                serde_json::Value::String(ref_system_url),
            );
        }

        if metadata.is_empty() {
            None
        } else {
            Some(serde_json::Value::Object(metadata))
        }
    }

    /// Execute a closure with access to the loaded header data
    fn with_header_data<T, F>(&self, f: F) -> Result<T>
    where
        F: FnOnce(&CachedHeaderData) -> Result<T>,
    {
        self.ensure_loaded()?;
        let data = self
            .cached_header_data
            .read()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
        let header_data = data
            .as_ref()
            .expect("data should be loaded after ensure_loaded");
        f(header_data)
    }

    /// Ensure streamed data is loaded and cached
    fn ensure_streamed_data_loaded(&self) -> Result<()> {
        // First check if already loaded with a read lock (cheaper)
        {
            let data = self
                .cached_streamed_data
                .read()
                .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
            if data.is_some() {
                return Ok(());
            }
        }

        // Not loaded, acquire write lock and load
        let mut data = self
            .cached_streamed_data
            .write()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire write lock".to_string()))?;

        // Double-check after acquiring write lock
        if data.is_none() {
            let (lods, city_object_types, city_object_count) =
                self.stream_extract_lods_and_types_inner()?;
            *data = Some(CachedStreamedData {
                lods,
                city_object_types,
                city_object_count,
            });
        }

        Ok(())
    }

    /// Stream through features to extract LODs, city object types, and count
    ///
    /// This method iterates through all features in the FCB file in a streaming
    /// fashion, extracting unique LODs, city object types, and counting all
    /// CityObjects without loading all features into memory at once.
    fn stream_extract_lods_and_types_inner(&self) -> Result<(Vec<String>, Vec<String>, usize)> {
        let file = File::open(&self.file_path)?;
        let reader = BufReader::new(file);

        let fcb_reader = FcbReader::open(reader)
            .map_err(|e| CityJsonStacError::Other(format!("Failed to open FCB file: {e}")))?;

        // Use BTreeSet for automatic deduplication and sorting
        let mut lods: BTreeSet<String> = BTreeSet::new();
        let mut types: BTreeSet<String> = BTreeSet::new();
        // Count all CityObjects (note: a single feature can have multiple objects)
        let mut city_object_count: usize = 0;

        // Use the sequential (streaming) iterator
        let mut feature_iter = fcb_reader
            .select_all_seq()
            .map_err(|e| CityJsonStacError::Other(format!("Failed to select features: {e}")))?;

        // Stream through features one at a time
        while let Some(iter) = feature_iter
            .next()
            .map_err(|e| CityJsonStacError::Other(format!("Failed to read feature: {e}")))?
        {
            let feature = iter.cur_feature();

            // Extract from city objects within this feature
            if let Some(objects) = feature.objects() {
                for obj in objects.iter() {
                    // Count this CityObject
                    city_object_count += 1;

                    // Extract city object type
                    let obj_type = obj.type_();
                    // Handle extension types specially
                    if obj_type == CityObjectType::ExtensionObject {
                        if let Some(ext_type) = obj.extension_type() {
                            types.insert(ext_type.to_string());
                        } else {
                            types.insert("ExtensionObject".to_string());
                        }
                    } else if let Some(name) = obj_type.variant_name() {
                        types.insert(name.to_string());
                    }

                    // Extract LODs from geometry
                    if let Some(geometries) = obj.geometry() {
                        for geom in geometries.iter() {
                            if let Some(lod) = geom.lod() {
                                lods.insert(lod.to_string());
                            }
                        }
                    }
                }
            }
        }

        // Convert to Vec (BTreeSet is already sorted)
        Ok((
            lods.into_iter().collect(),
            types.into_iter().collect(),
            city_object_count,
        ))
    }

    /// Execute a closure with access to the loaded streamed data
    fn with_streamed_data<T, F>(&self, f: F) -> Result<T>
    where
        F: FnOnce(&CachedStreamedData) -> Result<T>,
    {
        self.ensure_streamed_data_loaded()?;
        let data = self
            .cached_streamed_data
            .read()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
        let streamed_data = data
            .as_ref()
            .expect("data should be loaded after ensure_streamed_data_loaded");
        f(streamed_data)
    }
}

/// Map FCB ColumnType to AttributeType
fn map_column_type(column_type: u8) -> AttributeType {
    match column_type {
        t if t == ColumnType::Byte.0 => AttributeType::Number,
        t if t == ColumnType::UByte.0 => AttributeType::Number,
        t if t == ColumnType::Bool.0 => AttributeType::Boolean,
        t if t == ColumnType::Short.0 => AttributeType::Number,
        t if t == ColumnType::UShort.0 => AttributeType::Number,
        t if t == ColumnType::Int.0 => AttributeType::Number,
        t if t == ColumnType::UInt.0 => AttributeType::Number,
        t if t == ColumnType::Long.0 => AttributeType::Number,
        t if t == ColumnType::ULong.0 => AttributeType::Number,
        t if t == ColumnType::Float.0 => AttributeType::Number,
        t if t == ColumnType::Double.0 => AttributeType::Number,
        t if t == ColumnType::String.0 => AttributeType::String,
        t if t == ColumnType::Json.0 => AttributeType::Object,
        t if t == ColumnType::DateTime.0 => AttributeType::Date,
        _ => AttributeType::String, // Default fallback
    }
}

impl CityModelMetadataReader for FlatCityBufReader {
    fn bbox(&self) -> Result<BBox3D> {
        self.with_header_data(|data| {
            data.geographical_extent
                .map(|(xmin, ymin, zmin, xmax, ymax, zmax)| {
                    BBox3D::new(xmin, ymin, zmin, xmax, ymax, zmax)
                })
                .ok_or_else(|| {
                    CityJsonStacError::MetadataError(
                        "No geographical extent found in FCB header".to_string(),
                    )
                })
        })
    }

    fn crs(&self) -> Result<CRS> {
        self.with_header_data(|data| {
            if let Some(epsg_code) = data.reference_system_code {
                if epsg_code > 0 {
                    return Ok(CRS::from_epsg(epsg_code as u32));
                }
            }
            // Default to WGS84 if no CRS found
            Ok(CRS::default())
        })
    }

    fn lods(&self) -> Result<Vec<String>> {
        // Use cached streamed data
        self.with_streamed_data(|data| Ok(data.lods.clone()))
    }

    fn city_object_types(&self) -> Result<Vec<String>> {
        // Use cached streamed data
        self.with_streamed_data(|data| Ok(data.city_object_types.clone()))
    }

    fn city_object_count(&self) -> Result<usize> {
        // Use cached streamed data to get the actual number of CityObjects,
        // which may be higher than the number of FCB features (rows).
        // A single FCB feature can contain multiple CityObjects.
        self.with_streamed_data(|data| Ok(data.city_object_count))
    }

    fn attributes(&self) -> Result<Vec<AttributeDefinition>> {
        self.with_header_data(|data| {
            let mut attributes: Vec<AttributeDefinition> = data
                .columns
                .iter()
                .map(|col| {
                    let attr_type = map_column_type(col.column_type);
                    let mut attr_def = AttributeDefinition::new(&col.name, attr_type);

                    // nullable = true means required = false
                    attr_def = attr_def.with_required(!col.nullable);

                    if let Some(ref desc) = col.description {
                        attr_def = attr_def.with_description(desc);
                    }

                    attr_def
                })
                .collect();

            attributes.sort_by(|a, b| a.name.cmp(&b.name));
            Ok(attributes)
        })
    }

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

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

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

    fn transform(&self) -> Result<Option<Transform>> {
        self.with_header_data(|data| {
            Ok(data.transform.map(
                |(scale_x, scale_y, scale_z, translate_x, translate_y, translate_z)| {
                    Transform::new(
                        [scale_x, scale_y, scale_z],
                        [translate_x, translate_y, translate_z],
                    )
                },
            ))
        })
    }

    fn metadata(&self) -> Result<Option<serde_json::Value>> {
        self.with_header_data(|data| Ok(data.metadata_json.clone()))
    }

    fn extensions(&self) -> Result<Vec<String>> {
        self.with_header_data(|data| Ok(data.extensions.clone()))
    }

    fn semantic_surfaces(&self) -> Result<bool> {
        // FlatCityBuf stores semantic surface information in geometry
        // We need to stream through features to check for this
        let file = File::open(&self.file_path)?;
        let reader = BufReader::new(file);

        let fcb_reader = FcbReader::open(reader)
            .map_err(|e| CityJsonStacError::Other(format!("Failed to open FCB file: {e}")))?;

        let mut feature_iter = fcb_reader
            .select_all_seq()
            .map_err(|e| CityJsonStacError::Other(format!("Failed to select features: {e}")))?;

        while let Some(iter) = feature_iter
            .next()
            .map_err(|e| CityJsonStacError::Other(format!("Failed to read feature: {e}")))?
        {
            let feature = iter.cur_feature();

            if let Some(objects) = feature.objects() {
                for obj in objects.iter() {
                    if let Some(geometries) = obj.geometry() {
                        for geom in geometries.iter() {
                            // Check if geometry has semantic surfaces
                            // In FCB, this would be indicated by presence of semantic information
                            if geom.semantics().is_some() {
                                return Ok(true);
                            }
                        }
                    }
                }
            }
        }

        Ok(false)
    }

    fn textures(&self) -> Result<bool> {
        // Check if FCB header indicates presence of textures
        self.with_header_data(|data| {
            // FCB doesn't directly store textures flag, we check metadata
            if let Some(ref metadata) = data.metadata_json {
                if let Some(obj) = metadata.as_object() {
                    if obj.get("appearance").is_some() {
                        // Check if appearance contains texture data
                        if let Some(appearance) = obj.get("appearance") {
                            if let Some(app_obj) = appearance.as_object() {
                                if app_obj.get("textures").is_some() {
                                    return Ok(true);
                                }
                            }
                        }
                    }
                }
            }
            Ok(false)
        })
    }

    fn materials(&self) -> Result<bool> {
        // Check if FCB header indicates presence of materials
        self.with_header_data(|data| {
            if let Some(ref metadata) = data.metadata_json {
                if let Some(obj) = metadata.as_object() {
                    if let Some(appearance) = obj.get("appearance") {
                        if let Some(app_obj) = appearance.as_object() {
                            if app_obj.get("materials").is_some() {
                                return Ok(true);
                            }
                        }
                    }
                }
            }
            Ok(false)
        })
    }
}

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

    #[test]
    fn test_map_column_type() {
        assert_eq!(map_column_type(ColumnType::String.0), AttributeType::String);
        assert_eq!(map_column_type(ColumnType::Int.0), AttributeType::Number);
        assert_eq!(map_column_type(ColumnType::Bool.0), AttributeType::Boolean);
        assert_eq!(map_column_type(ColumnType::Json.0), AttributeType::Object);
    }

    #[test]
    fn test_reader_file_not_found() {
        let result = FlatCityBufReader::new(Path::new("nonexistent.fcb"));
        assert!(result.is_err());
    }
}