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
//! ZIP archive reader for CityJSON/CityGML files
//!
//! Extracts ZIP archives and aggregates metadata from all supported files inside.

use crate::error::{CityJsonStacError, Result};
use crate::memory::{log_memory, memory_log_interval, memory_logging_enabled};
use crate::metadata::AttributeDefinition;
use crate::metadata::BBox3D;
use crate::metadata::CRS;
use crate::reader::{get_reader, CityModelMetadataReader};
use std::collections::{BTreeSet, HashMap};
use std::path::{Path, PathBuf};
use std::sync::RwLock;
use tempfile::{TempDir, TempPath};

/// Reader for ZIP archives containing CityJSON/CityGML files
pub struct ZipReader {
    /// Virtual path for display (e.g., original filename from URL)
    file_path: PathBuf,
    temp_dir: TempDir,
    /// Keep temp file alive for remote ZIPs (downloaded files)
    _temp_file: Option<TempPath>,
    inner_paths: Vec<PathBuf>,
    metadata: RwLock<Option<ZipMetadata>>,
}

/// Aggregated metadata from all files in the ZIP
#[derive(Debug)]
struct ZipMetadata {
    bbox: Option<BBox3D>,
    city_object_count: usize,
    city_object_types: BTreeSet<String>,
    lods: BTreeSet<String>,
    attributes: Vec<AttributeDefinition>,
    primary_encoding: &'static str,
    version: String,
    crs: Option<CRS>,
    has_textures: bool,
    has_materials: bool,
    has_semantic_surfaces: bool,
}

impl ZipReader {
    /// Create a new ZIP reader from a local file
    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()),
            )));
        }

        // Create temporary directory for extraction
        let temp_dir = TempDir::new()?;

        // Extract ZIP to temp directory
        Self::extract_zip(file_path, temp_dir.path())?;
        log_memory(format!("zip-extracted path={}", file_path.display()));

        let mut reader = Self {
            file_path: file_path.to_path_buf(),
            temp_dir,
            _temp_file: None,
            inner_paths: Vec::new(),
            metadata: RwLock::new(None),
        };

        // Discover supported files inside the extracted ZIP
        reader.inner_paths = reader.discover_inner_paths()?;
        log_memory(format!(
            "zip-discovered path={} inner_files={}",
            reader.file_path.display(),
            reader.inner_paths.len()
        ));

        if reader.inner_paths.is_empty() {
            return Err(CityJsonStacError::InvalidCityJson(
                "No CityJSON/CityGML files found in ZIP".to_string(),
            ));
        }

        Ok(reader)
    }

    /// Create a ZIP reader from a temporary file (for remote downloads)
    ///
    /// This constructor takes ownership of a TempPath to keep the downloaded
    /// ZIP file alive for the lifetime of the reader.
    ///
    /// # Arguments
    /// * `virtual_path` - The display path (e.g., original filename from URL)
    /// * `real_path` - The actual file path to read from (temp file)
    /// * `temp_path` - The TempPath to keep alive for the lifetime of the reader
    pub fn from_temp_file(
        virtual_path: &Path,
        real_path: &Path,
        temp_path: TempPath,
    ) -> Result<Self> {
        // Create temporary directory for extraction
        let temp_dir = TempDir::new()?;

        // Extract ZIP to temp directory
        Self::extract_zip(real_path, temp_dir.path())?;
        log_memory(format!("zip-extracted path={}", virtual_path.display()));

        let mut reader = Self {
            file_path: virtual_path.to_path_buf(),
            temp_dir,
            _temp_file: Some(temp_path),
            inner_paths: Vec::new(),
            metadata: RwLock::new(None),
        };

        // Discover supported files inside the extracted ZIP
        reader.inner_paths = reader.discover_inner_paths()?;
        log_memory(format!(
            "zip-discovered path={} inner_files={}",
            reader.file_path.display(),
            reader.inner_paths.len()
        ));

        if reader.inner_paths.is_empty() {
            return Err(CityJsonStacError::InvalidCityJson(
                "No CityJSON/CityGML files found in ZIP".to_string(),
            ));
        }

        Ok(reader)
    }

    /// Extract ZIP file to directory
    fn extract_zip(zip_path: &Path, dest_dir: &Path) -> Result<()> {
        let file = std::fs::File::open(zip_path)?;
        let mut archive = zip::ZipArchive::new(file)?;

        for i in 0..archive.len() {
            let mut file = archive.by_index(i)?;
            let outpath = dest_dir.join(file.name());

            // Validate path doesn't escape dest_dir (ZIP Slip prevention)
            if !outpath.starts_with(dest_dir) {
                return Err(CityJsonStacError::InvalidCityJson(format!(
                    "ZIP file contains unsafe path: {}",
                    file.name()
                )));
            }

            if file.name().ends_with('/') {
                std::fs::create_dir_all(&outpath)?;
            } else {
                if let Some(parent) = outpath.parent() {
                    std::fs::create_dir_all(parent)?;
                }
                let mut outfile = std::fs::File::create(&outpath)?;
                std::io::copy(&mut file, &mut outfile)?;
            }
        }

        Ok(())
    }

    /// Discover all supported files in the extracted directory.
    ///
    /// Keep only paths here so we do not retain one reader per inner file.
    fn discover_inner_paths(&self) -> Result<Vec<PathBuf>> {
        let mut paths = Vec::new();

        // Walk the extracted directory
        fn walk_dir(dir: &Path, paths: &mut Vec<PathBuf>) -> Result<()> {
            for entry in std::fs::read_dir(dir)? {
                let entry = entry?;
                let path = entry.path();

                if path.is_dir() {
                    walk_dir(&path, paths)?;
                } else {
                    // Only keep paths for supported files; readers are opened on demand later.
                    if get_reader(&path).is_ok() {
                        log::debug!("Found supported file in ZIP: {:?}", path);
                        paths.push(path);
                    }
                }
            }
            Ok(())
        }

        walk_dir(self.temp_dir.path(), &mut paths)?;
        Ok(paths)
    }

    /// Aggregate metadata from all supported inner files.
    fn aggregate_metadata(&self) -> Result<ZipMetadata> {
        let mut city_object_count = 0;
        let mut city_object_types = BTreeSet::new();
        let mut lods = BTreeSet::new();
        let mut attributes_map: HashMap<String, AttributeDefinition> = HashMap::new();
        let mut has_textures = false;
        let mut has_materials = false;
        let mut has_semantic_surfaces = false;

        // For bbox, we need to merge extents
        let mut min_x = f64::MAX;
        let mut min_y = f64::MAX;
        let mut min_z = f64::MAX;
        let mut max_x = f64::MIN;
        let mut max_y = f64::MIN;
        let mut max_z = f64::MIN;
        let mut has_bbox = false;

        let mut primary_encoding = "CityJSON";

        let mut version = String::new();
        let mut crs = None;
        let memory_log_every = memory_log_interval(100);

        for (idx, path) in self.inner_paths.iter().enumerate() {
            let reader = match get_reader(path) {
                Ok(reader) => reader,
                Err(_) => continue,
            };

            if idx == 0 {
                primary_encoding = reader.encoding();
            }

            // Count city objects
            if let Ok(count) = reader.city_object_count() {
                city_object_count += count;
            }

            // Collect city object types
            if let Ok(types) = reader.city_object_types() {
                city_object_types.extend(types);
            }

            // Collect LODs
            if let Ok(reader_lods) = reader.lods() {
                lods.extend(reader_lods);
            }

            // Collect attributes (using HashMap for deduplication by name)
            if let Ok(reader_attrs) = reader.attributes() {
                for attr in reader_attrs {
                    attributes_map.insert(attr.name.clone(), attr);
                }
            }

            // Check for textures/materials/semantic surfaces
            if let Ok(true) = reader.textures() {
                has_textures = true;
            }
            if let Ok(true) = reader.materials() {
                has_materials = true;
            }
            if let Ok(true) = reader.semantic_surfaces() {
                has_semantic_surfaces = true;
            }

            // Merge bbox
            if let Ok(bbox) = reader.bbox() {
                has_bbox = true;
                min_x = min_x.min(bbox.xmin);
                min_y = min_y.min(bbox.ymin);
                min_z = min_z.min(bbox.zmin);
                max_x = max_x.max(bbox.xmax);
                max_y = max_y.max(bbox.ymax);
                max_z = max_z.max(bbox.zmax);
            }

            // Get version and CRS from first reader
            if version.is_empty() {
                if let Ok(v) = reader.version() {
                    version = v;
                }
            }
            if crs.is_none() {
                if let Ok(c) = reader.crs() {
                    crs = Some(c);
                }
            }

            if memory_logging_enabled() && (idx + 1) % memory_log_every == 0 {
                log_memory(format!(
                    "zip-aggregate path={} processed_inner={}/{}",
                    self.file_path.display(),
                    idx + 1,
                    self.inner_paths.len()
                ));
            }
        }

        log_memory(format!(
            "zip-aggregate-finished path={} inner_files={}",
            self.file_path.display(),
            self.inner_paths.len()
        ));

        let bbox = if has_bbox {
            Some(BBox3D::new(min_x, min_y, min_z, max_x, max_y, max_z))
        } else {
            None
        };

        let attributes: Vec<_> = attributes_map.into_values().collect();

        Ok(ZipMetadata {
            bbox,
            city_object_count,
            city_object_types,
            lods,
            attributes,
            primary_encoding,
            version,
            crs,
            has_textures,
            has_materials,
            has_semantic_surfaces,
        })
    }

    /// Lazy load metadata
    fn ensure_loaded(&self) -> Result<()> {
        {
            let metadata = self
                .metadata
                .read()
                .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
            if metadata.is_some() {
                return Ok(());
            }
        }

        let mut metadata = self
            .metadata
            .write()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire write lock".to_string()))?;

        if metadata.is_none() {
            *metadata = Some(self.aggregate_metadata()?);
        }

        Ok(())
    }
}

impl CityModelMetadataReader for ZipReader {
    fn bbox(&self) -> Result<BBox3D> {
        self.ensure_loaded()?;
        let metadata = self
            .metadata
            .read()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
        metadata
            .as_ref()
            .and_then(|m| m.bbox.clone())
            .ok_or_else(|| CityJsonStacError::MetadataError("BBox not found".to_string()))
    }

    fn crs(&self) -> Result<CRS> {
        self.ensure_loaded()?;
        let metadata = self
            .metadata
            .read()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
        Ok(metadata.as_ref().unwrap().crs.clone().unwrap_or_default())
    }

    fn lods(&self) -> Result<Vec<String>> {
        self.ensure_loaded()?;
        let metadata = self
            .metadata
            .read()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
        Ok(metadata.as_ref().unwrap().lods.iter().cloned().collect())
    }

    fn city_object_types(&self) -> Result<Vec<String>> {
        self.ensure_loaded()?;
        let metadata = self
            .metadata
            .read()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
        Ok(metadata
            .as_ref()
            .unwrap()
            .city_object_types
            .iter()
            .cloned()
            .collect())
    }

    fn city_object_count(&self) -> Result<usize> {
        self.ensure_loaded()?;
        let metadata = self
            .metadata
            .read()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
        Ok(metadata.as_ref().unwrap().city_object_count)
    }

    fn attributes(&self) -> Result<Vec<AttributeDefinition>> {
        self.ensure_loaded()?;
        let metadata = self
            .metadata
            .read()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
        Ok(metadata.as_ref().unwrap().attributes.clone())
    }

    fn encoding(&self) -> &'static str {
        // Return the internal format from first file found
        if let Ok(metadata) = self.metadata.read() {
            if let Some(ref m) = *metadata {
                return m.primary_encoding;
            }
        }
        "CityJSON" // Fallback
    }

    fn version(&self) -> Result<String> {
        self.ensure_loaded()?;
        let metadata = self
            .metadata
            .read()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
        Ok(metadata.as_ref().unwrap().version.clone())
    }

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

    fn transform(&self) -> Result<Option<crate::metadata::Transform>> {
        Ok(None) // ZIP wrapper doesn't use vertex compression
    }

    fn metadata(&self) -> Result<Option<serde_json::Value>> {
        Ok(None)
    }

    fn extensions(&self) -> Result<Vec<String>> {
        Ok(Vec::new())
    }

    fn semantic_surfaces(&self) -> Result<bool> {
        self.ensure_loaded()?;
        let metadata = self
            .metadata
            .read()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
        Ok(metadata.as_ref().unwrap().has_semantic_surfaces)
    }

    fn textures(&self) -> Result<bool> {
        self.ensure_loaded()?;
        let metadata = self
            .metadata
            .read()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
        Ok(metadata.as_ref().unwrap().has_textures)
    }

    fn materials(&self) -> Result<bool> {
        self.ensure_loaded()?;
        let metadata = self
            .metadata
            .read()
            .map_err(|_| CityJsonStacError::Other("Failed to acquire read lock".to_string()))?;
        Ok(metadata.as_ref().unwrap().has_materials)
    }
}

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

    /// Helper function to create a test ZIP file with CityJSON content
    fn create_test_zip_with_cityjson() -> NamedTempFile {
        let temp_zip = NamedTempFile::new().unwrap();
        let mut zip = zip::ZipWriter::new(temp_zip.as_file());

        let cityjson = r#"{
        "type": "CityJSON",
        "version": "1.1",
        "transform": {
            "scale": [0.01, 0.01, 0.01],
            "translate": [100000, 200000, 0]
        },
        "metadata": {
            "geographicalExtent": [1.0, 2.0, 0.0, 10.0, 20.0, 30.0],
            "referenceSystem": "https://www.opengis.net/def/crs/EPSG/0/7415"
        },
        "CityObjects": {
            "building1": {
                "type": "Building",
                "geometry": [{
                    "type": "Solid",
                    "lod": "2",
                    "boundaries": [[[[0,0,0]]]]
                }]
            }
        },
        "vertices": [[0,0,0]]
    }"#;

        let options = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);

        zip.start_file("data.city.json", options).unwrap();
        zip.write_all(cityjson.as_bytes()).unwrap();
        zip.finish().unwrap();

        temp_zip
    }

    #[test]
    fn test_zip_reader_aggregates_metadata() {
        let temp_zip = create_test_zip_with_cityjson();
        let reader = ZipReader::new(temp_zip.path()).unwrap();

        // Should have bbox from inner file
        let bbox = reader.bbox().unwrap();
        assert_eq!(bbox.xmin, 1.0);
        assert_eq!(bbox.xmax, 10.0);

        // Should have city object count
        let count = reader.city_object_count().unwrap();
        assert_eq!(count, 1);

        // Should have city object types
        let types = reader.city_object_types().unwrap();
        assert!(types.contains(&"Building".to_string()));

        // Should have LODs
        let lods = reader.lods().unwrap();
        assert!(lods.contains(&"2".to_string()));

        // Should have version
        let version = reader.version().unwrap();
        assert_eq!(version, "1.1");

        // Should have CRS
        let crs = reader.crs().unwrap();
        assert_eq!(crs.to_stac_epsg(), Some(7415));

        // Should have CityJSON encoding
        assert_eq!(reader.encoding(), "CityJSON");
    }

    #[test]
    fn test_zip_reader_empty_zip() {
        let temp_zip = NamedTempFile::new().unwrap();
        let zip = zip::ZipWriter::new(temp_zip.as_file());
        zip.finish().unwrap();

        let result = ZipReader::new(temp_zip.path());
        assert!(result.is_err());
        match result {
            Err(CityJsonStacError::InvalidCityJson(msg)) => {
                assert!(msg.contains("No CityJSON/CityGML files found"));
            }
            _ => panic!("Expected InvalidCityJson error"),
        }
    }

    #[test]
    fn test_zip_reader_not_streamable() {
        // Create a minimal valid ZIP file
        let mut temp_zip = NamedTempFile::new().unwrap();
        let zip = zip::ZipWriter::new(temp_zip.as_file_mut());
        zip.finish().unwrap();

        // Note: This will fail with "No CityJSON/CityGML files found"
        // because the ZIP is empty. That's expected - test verifies
        // the struct compiles and method exists.
        let result = ZipReader::new(temp_zip.path());
        assert!(result.is_err());

        // Verify it's the expected error
        match result {
            Err(CityJsonStacError::InvalidCityJson(msg)) => {
                assert!(msg.contains("No CityJSON/CityGML files found"));
            }
            _ => panic!("Expected InvalidCityJson error"),
        }
    }
}