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
//! Unit tests for the reader module

use city3d_stac::reader::{get_reader, CityJSONReader, CityJSONSeqReader, CityModelMetadataReader};
use std::path::Path;

/// Test data directory path
fn test_data_path(filename: &str) -> std::path::PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("data")
        .join(filename)
}

mod cityjson_reader_tests {
    use super::*;

    #[test]
    fn test_read_delft_cityjson() {
        let path = test_data_path("delft.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

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

    #[test]
    fn test_delft_version() {
        let path = test_data_path("delft.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let version = reader.version().expect("Failed to get version");
        assert_eq!(version, "2.0");
    }

    #[test]
    fn test_delft_bbox() {
        let path = test_data_path("delft.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let bbox = reader.bbox().expect("Failed to get bbox");

        // Check the geographicalExtent from delft.city.json metadata
        assert!((bbox.xmin - 84927.558).abs() < 0.001);
        assert!((bbox.ymin - 446572.456).abs() < 0.001);
        assert!((bbox.zmin - (-3.704)).abs() < 0.001);
        assert!((bbox.xmax - 85527.591).abs() < 0.001);
        assert!((bbox.ymax - 447122.446).abs() < 0.001);
        assert!((bbox.zmax - 52.147).abs() < 0.001);
    }

    #[test]
    fn test_delft_crs() {
        let path = test_data_path("delft.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let crs = reader.crs().expect("Failed to get CRS");
        assert_eq!(crs.epsg, Some(7415));
    }

    #[test]
    fn test_delft_city_object_count() {
        let path = test_data_path("delft.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let count = reader
            .city_object_count()
            .expect("Failed to get city object count");
        // delft.city.json has 319 CityObjects
        assert_eq!(count, 319);
    }

    #[test]
    fn test_delft_transform() {
        let path = test_data_path("delft.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let transform = reader.transform().expect("Failed to get transform");
        assert!(transform.is_some());

        let t = transform.unwrap();
        assert_eq!(t.scale, [0.001, 0.001, 0.001]);
    }

    #[test]
    fn test_delft_metadata() {
        let path = test_data_path("delft.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let metadata = reader.metadata().expect("Failed to get metadata");
        assert!(metadata.is_some());

        let m = metadata.unwrap();
        assert_eq!(m["title"], "3DBAG");
    }

    #[test]
    fn test_delft_lods_empty() {
        let path = test_data_path("delft.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let lods = reader.lods().expect("Failed to get LODs");
        // delft.city.json has LODs: 0, 1.2, 1.3, 2.2
        assert!(!lods.is_empty());
        assert!(lods.contains(&"0".to_string()));
        assert!(lods.contains(&"1.2".to_string()));
        assert!(lods.contains(&"1.3".to_string()));
        assert!(lods.contains(&"2.2".to_string()));
    }

    #[test]
    fn test_delft_city_object_types_empty() {
        let path = test_data_path("delft.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let types = reader.city_object_types().expect("Failed to get types");
        // delft.city.json has Building and BuildingPart types
        assert!(!types.is_empty());
        assert!(types.contains(&"Building".to_string()));
        assert!(types.contains(&"BuildingPart".to_string()));
    }

    #[test]
    fn test_delft_attributes_empty() {
        let path = test_data_path("delft.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let attrs = reader.attributes().expect("Failed to get attributes");
        // delft.city.json has many attributes
        assert!(!attrs.is_empty());
    }

    #[test]
    fn test_file_path() {
        let path = test_data_path("delft.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        assert_eq!(reader.file_path(), path);
    }
}

mod railway_tests {
    use super::*;

    #[test]
    fn test_read_railway_cityjson() {
        let path = test_data_path("railway.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

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

    #[test]
    fn test_railway_version() {
        let path = test_data_path("railway.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let version = reader.version().expect("Failed to get version");
        assert_eq!(version, "2.0");
    }

    #[test]
    fn test_railway_has_city_objects() {
        let path = test_data_path("railway.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let count = reader.city_object_count().expect("Failed to get count");
        assert!(count > 0, "Railway should have city objects");
    }

    #[test]
    fn test_railway_has_city_object_types() {
        let path = test_data_path("railway.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let types = reader.city_object_types().expect("Failed to get types");
        assert!(!types.is_empty(), "Railway should have object types");
    }

    #[test]
    fn test_railway_bbox() {
        let path = test_data_path("railway.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let bbox = reader.bbox().expect("Failed to get bbox");
        assert!(bbox.is_valid());
    }

    #[test]
    fn test_railway_has_lods() {
        let path = test_data_path("railway.city.json");
        let reader = CityJSONReader::new(&path).expect("Failed to create reader");

        let lods = reader.lods().expect("Failed to get LODs");
        assert!(!lods.is_empty(), "Railway should have LODs");
    }
}

mod get_reader_tests {
    use super::*;

    #[test]
    fn test_get_reader_for_cityjson() {
        let path = test_data_path("delft.city.json");
        let reader = get_reader(&path).expect("Failed to get reader");

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

    #[test]
    fn test_get_reader_unsupported_extension() {
        let path = Path::new("test.txt");
        let result = get_reader(path);

        assert!(result.is_err());
    }

    #[test]
    fn test_get_reader_nonexistent_file() {
        let path = Path::new("nonexistent.json");
        let result = get_reader(path);

        assert!(result.is_err());
    }

    #[test]
    fn test_get_reader_fcb_supported() {
        // FlatCityBuf is now supported
        let path = test_data_path("all.fcb");
        let result = get_reader(&path);

        assert!(result.is_ok(), "FlatCityBuf should be supported now");
        let reader = result.unwrap();
        assert_eq!(reader.encoding(), "FlatCityBuf");
    }
}

mod reader_thread_safety_tests {
    use super::*;
    use std::sync::Arc;
    use std::thread;

    #[test]
    fn test_reader_is_send_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<CityJSONReader>();
    }

    #[test]
    fn test_concurrent_reader_access() {
        let path = test_data_path("delft.city.json");
        let reader = Arc::new(CityJSONReader::new(&path).expect("Failed to create reader"));

        let handles: Vec<_> = (0..4)
            .map(|_| {
                let r = Arc::clone(&reader);
                thread::spawn(move || {
                    let version = r.version().expect("Failed to get version");
                    let bbox = r.bbox().expect("Failed to get bbox");
                    (version, bbox)
                })
            })
            .collect();

        for handle in handles {
            let (version, bbox) = handle.join().expect("Thread panicked");
            assert_eq!(version, "2.0");
            assert!(bbox.is_valid());
        }
    }
}

mod cjseq_integration_tests {
    use super::*;

    #[test]
    fn test_read_delft_cjseq() {
        let path = test_data_path("delft.city.jsonl");
        let reader = get_reader(&path).expect("Failed to create reader");

        assert_eq!(reader.encoding(), "CityJSONSeq");

        let version = reader.version().expect("Failed to get version");
        assert_eq!(version, "2.0");

        let bbox = reader.bbox().expect("Failed to get bbox");
        assert!(bbox.is_valid());
    }

    #[test]
    fn test_read_railway_cjseq() {
        let path = test_data_path("railway.city.jsonl");
        let reader = get_reader(&path).expect("Failed to create reader");

        assert_eq!(reader.encoding(), "CityJSONSeq");

        let count = reader.city_object_count().expect("Failed to get count");
        assert!(count > 0, "Railway should have city objects");

        let types = reader.city_object_types().expect("Failed to get types");
        assert!(!types.is_empty());
    }

    #[test]
    fn test_cjseq_reader_is_send_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<CityJSONSeqReader>();
    }

    #[test]
    fn test_delft_cjseq_crs() {
        let path = test_data_path("delft.city.jsonl");
        let reader = get_reader(&path).expect("Failed to create reader");

        let crs = reader.crs().expect("Failed to get CRS");
        assert_eq!(crs.epsg, Some(7415));
    }

    #[test]
    fn test_delft_cjseq_transform() {
        let path = test_data_path("delft.city.jsonl");
        let reader = get_reader(&path).expect("Failed to create reader");

        let transform = reader.transform().expect("Failed to get transform");
        assert!(transform.is_some());
    }
}

mod fcb_integration_tests {
    use super::*;
    use city3d_stac::reader::FlatCityBufReader;

    #[test]
    fn test_read_fcb_file() {
        let path = test_data_path("all.fcb");
        let reader = FlatCityBufReader::new(&path).expect("Failed to create reader");

        assert_eq!(reader.encoding(), "FlatCityBuf");
    }

    #[test]
    fn test_fcb_version() {
        let path = test_data_path("all.fcb");
        let reader = FlatCityBufReader::new(&path).expect("Failed to create reader");

        let version = reader.version().expect("Failed to get version");
        // FCB files have a CityJSON version
        assert!(!version.is_empty(), "Version should not be empty");
    }

    #[test]
    fn test_fcb_city_object_count() {
        let path = test_data_path("all.fcb");
        let reader = FlatCityBufReader::new(&path).expect("Failed to create reader");

        let count = reader.city_object_count().expect("Failed to get count");
        // FCB files have features
        assert!(count > 0, "FCB file should have city objects");
    }

    #[test]
    fn test_fcb_bbox() {
        let path = test_data_path("all.fcb");
        let reader = FlatCityBufReader::new(&path).expect("Failed to create reader");

        let result = reader.bbox();
        // FCB files may or may not have bbox
        if let Ok(bbox) = result {
            assert!(bbox.is_valid(), "Bbox should be valid if present");
        }
    }

    #[test]
    fn test_fcb_crs() {
        let path = test_data_path("all.fcb");
        let reader = FlatCityBufReader::new(&path).expect("Failed to create reader");

        let crs = reader.crs().expect("Failed to get CRS");
        // CRS should always be available (may be default WGS84)
        assert!(crs.epsg.is_some(), "CRS should have EPSG code");
    }

    #[test]
    fn test_fcb_attributes() {
        let path = test_data_path("all.fcb");
        let reader = FlatCityBufReader::new(&path).expect("Failed to create reader");

        let attrs = reader.attributes().expect("Failed to get attributes");
        // FCB files define columns which map to attributes
        // Verify attributes are sorted by name (the reader should sort them)
        if !attrs.is_empty() {
            let names: Vec<_> = attrs.iter().map(|a| &a.name).collect();
            let mut sorted_names = names.clone();
            sorted_names.sort();
            assert_eq!(names, sorted_names, "Attributes should be sorted by name");
        }
    }

    #[test]
    fn test_fcb_transform() {
        let path = test_data_path("all.fcb");
        let reader = FlatCityBufReader::new(&path).expect("Failed to create reader");

        let transform = reader.transform().expect("Failed to get transform");
        // Transform may or may not be present
        if let Some(t) = transform {
            assert_eq!(t.scale.len(), 3, "Scale should have 3 components");
            assert_eq!(t.translate.len(), 3, "Translate should have 3 components");
        }
    }

    #[test]
    fn test_fcb_metadata() {
        let path = test_data_path("all.fcb");
        let reader = FlatCityBufReader::new(&path).expect("Failed to create reader");

        let metadata = reader.metadata().expect("Failed to get metadata");
        // Metadata may or may not be present
        if let Some(m) = metadata {
            assert!(m.is_object(), "Metadata should be an object");
        }
    }

    #[test]
    fn test_fcb_file_path() {
        let path = test_data_path("all.fcb");
        let reader = FlatCityBufReader::new(&path).expect("Failed to create reader");

        assert_eq!(reader.file_path(), path);
    }

    #[test]
    fn test_fcb_thread_safety() {
        use std::sync::Arc;
        use std::thread;

        let path = test_data_path("all.fcb");
        let reader = Arc::new(FlatCityBufReader::new(&path).expect("Failed to create reader"));

        let handles: Vec<_> = (0..4)
            .map(|_| {
                let r = Arc::clone(&reader);
                thread::spawn(move || {
                    let encoding = r.encoding();
                    let version = r.version().expect("Failed to get version");
                    (encoding, version)
                })
            })
            .collect();

        for handle in handles {
            let (encoding, version) = handle.join().expect("Thread panicked");
            assert_eq!(encoding, "FlatCityBuf");
            assert!(!version.is_empty());
        }
    }

    #[test]
    fn test_fcb_lods() {
        let path = test_data_path("all.fcb");
        let reader = FlatCityBufReader::new(&path).expect("Failed to create reader");

        let lods = reader.lods().expect("Failed to get LODs");
        // The all.fcb file contains geometry with LODs
        assert!(
            !lods.is_empty(),
            "FCB file should have at least one LOD from geometry"
        );

        // Verify LODs are sorted alphabetically
        let mut sorted = lods.clone();
        sorted.sort();
        assert_eq!(lods, sorted, "LODs should be sorted");
    }

    #[test]
    fn test_fcb_city_object_types() {
        let path = test_data_path("all.fcb");
        let reader = FlatCityBufReader::new(&path).expect("Failed to create reader");

        let types = reader
            .city_object_types()
            .expect("Failed to get city object types");
        // FCB file should have at least one city object type
        assert!(!types.is_empty(), "FCB file should have city object types");

        // The test file is called "all.fcb" so it likely contains various city object types
        // Types should be sorted alphabetically
        let mut sorted = types.clone();
        sorted.sort();
        assert_eq!(types, sorted, "City object types should be sorted");
    }

    #[test]
    fn test_fcb_lods_and_types_caching() {
        let path = test_data_path("all.fcb");
        let reader = FlatCityBufReader::new(&path).expect("Failed to create reader");

        // Call lods() twice - second call should use cache
        let lods1 = reader.lods().expect("Failed to get LODs first time");
        let lods2 = reader.lods().expect("Failed to get LODs second time");
        assert_eq!(lods1, lods2, "LODs should be consistent between calls");

        // city_object_types() should use the same cached data
        let types = reader
            .city_object_types()
            .expect("Failed to get city object types");
        assert!(!types.is_empty(), "Should get types from cached data");
    }

    #[test]
    fn test_fcb_streaming_thread_safety() {
        use std::sync::Arc;
        use std::thread;

        let path = test_data_path("all.fcb");
        let reader = Arc::new(FlatCityBufReader::new(&path).expect("Failed to create reader"));

        let handles: Vec<_> = (0..4)
            .map(|i| {
                let r = Arc::clone(&reader);
                thread::spawn(move || {
                    if i % 2 == 0 {
                        r.lods().expect("Failed to get LODs")
                    } else {
                        r.city_object_types().expect("Failed to get types")
                    }
                })
            })
            .collect();

        // Get baseline results for comparison
        let baseline_lods = reader.lods().expect("Failed to get baseline LODs");
        let baseline_types = reader
            .city_object_types()
            .expect("Failed to get baseline types");

        for (i, handle) in handles.into_iter().enumerate() {
            let result = handle.join().expect("Thread panicked");
            // Verify each thread got the same result as sequential baseline
            if i % 2 == 0 {
                assert_eq!(result, baseline_lods, "LODs should match baseline");
            } else {
                assert_eq!(result, baseline_types, "Types should match baseline");
            }
        }
    }
}