fcb_core 0.7.6

FlatCityBuf is a library for reading and writing CityJSON with FlatBuffers. Contains code derived from FlatGeobuf (BSD-2-Clause) for spatial indexing.
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
use crate::fb::ColumnType;
use byteorder::{ByteOrder, LittleEndian};
use chrono::{DateTime, Utc};
use cjseq::CityJSONFeature;
use serde_json::Value;
use std::collections::HashMap;

// Schema for attributes. The key is the attribute name, the value is a tuple of the column index and the column type.
pub type AttributeSchema = HashMap<String, (u16, ColumnType)>;

pub trait AttributeSchemaMethods {
    fn add_attributes(&mut self, attrs: &Value);
}

impl AttributeSchemaMethods for AttributeSchema {
    fn add_attributes(&mut self, attrs: &Value) {
        if !attrs.is_object() {
            self.insert("json".to_string(), (self.len() as u16, ColumnType::Json));
            return;
        }

        let map = attrs.as_object().unwrap();
        for (key, val) in map.iter() {
            if !self.contains_key(key) && !val.is_null() {
                if let Some(coltype) = guess_type(val) {
                    self.insert(key.clone(), (self.len() as u16, coltype));
                }
            }
        }
    }
}

/// Naive type-guessing. You could use your schema or logic as in your Python code.
fn guess_type(value: &Value) -> Option<ColumnType> {
    match value {
        Value::Bool(_) => Some(ColumnType::Bool),
        Value::Number(n) => {
            if n.is_f64() {
                Some(ColumnType::Double)
            } else if n.is_u64() {
                Some(ColumnType::ULong)
            } else if n.is_i64() {
                Some(ColumnType::Long)
            } else {
                Some(ColumnType::ULong) // Fallback for unknown number type.
            }
        }
        Value::String(s) => {
            // Attempt to parse the string as an RFC3339 date.
            if chrono::DateTime::parse_from_rfc3339(s).is_ok() {
                Some(ColumnType::DateTime)
            } else {
                Some(ColumnType::String)
            }
        }
        Value::Array(_) => Some(ColumnType::Json),
        Value::Object(_) => Some(ColumnType::Json),
        _ => None,
    }
}

pub(crate) fn attr_size(coltype: &ColumnType, colval: &Value) -> usize {
    match *coltype {
        ColumnType::Byte => size_of::<i8>(),
        ColumnType::UByte => size_of::<u8>(),
        ColumnType::Bool => size_of::<u8>(),
        ColumnType::Short => size_of::<i16>(),
        ColumnType::UShort => size_of::<u16>(),
        ColumnType::Int => size_of::<i32>(),
        ColumnType::UInt => size_of::<u32>(),
        ColumnType::Long => size_of::<i64>(),
        ColumnType::ULong => size_of::<u64>(),
        ColumnType::Float => size_of::<f32>(),
        ColumnType::Double => size_of::<f64>(),
        ColumnType::String | ColumnType::DateTime => {
            size_of::<u32>() + colval.as_str().unwrap().len()
        }
        ColumnType::Json => {
            let json = serde_json::to_string(colval).unwrap_or_default();
            size_of::<u32>() + json.len()
        }
        ColumnType::Binary => size_of::<u32>() + colval.as_str().unwrap().len(), //TODO: check if this is correct
        _ => unreachable!(),
    }
}

pub(crate) fn encode_attributes_with_schema(attr: &Value, schema: &AttributeSchema) -> Vec<u8> {
    if !attr.is_object() || attr.as_object().unwrap().is_empty() || attr.is_null() {
        return Vec::new();
    }

    let mut out = Vec::new();
    let mut sorted_schema: Vec<_> = schema.iter().collect();
    sorted_schema.sort_by_key(|(_, (index, _))| *index);

    for (name, (index, coltype)) in sorted_schema {
        let (_, val) = {
            let attr_obj = attr.as_object();
            if let Some(attr_obj) = attr_obj {
                let value = attr_obj.iter().find(|(k, _)| *k == name);
                if let Some(value) = value {
                    (value.0, value.1)
                } else {
                    continue;
                }
            } else {
                return Vec::new();
            }
        };

        if val.is_null() {
            continue;
        }

        let mut offset = out.len();
        let attr_size = attr_size(coltype, val);

        // Reserve space for index and value
        out.resize(offset + size_of::<u16>() + attr_size, 0);

        // Write index
        LittleEndian::write_u16(&mut out[offset..], *index);
        offset += size_of::<u16>();

        match *coltype {
            ColumnType::Bool => {
                let b = val.as_bool().unwrap_or(false);
                out[offset] = b as u8;
            }
            ColumnType::Int => {
                let i = val.as_i64().unwrap_or(0);
                LittleEndian::write_i32(&mut out[offset..], i as i32);
            }
            ColumnType::UInt => {
                let i = val.as_u64().unwrap_or(0);
                LittleEndian::write_u32(&mut out[offset..], i as u32);
            }
            ColumnType::Byte => {
                let b = val.as_i64().unwrap_or(0);
                out[offset] = b as u8;
            }
            ColumnType::UByte => {
                let b = val.as_u64().unwrap_or(0);
                out[offset] = b as u8;
            }

            ColumnType::Short => {
                let i = val.as_i64().unwrap_or(0);
                LittleEndian::write_i16(&mut out[offset..], i as i16);
            }
            ColumnType::UShort => {
                let i = val.as_u64().unwrap_or(0);
                LittleEndian::write_u16(&mut out[offset..], i as u16);
            }

            ColumnType::Long => {
                let i = val.as_i64().unwrap_or(0);
                LittleEndian::write_i64(&mut out[offset..], i);
            }
            ColumnType::ULong => {
                let i = val.as_u64().unwrap_or(0);
                LittleEndian::write_u64(&mut out[offset..], i);
            }
            ColumnType::Float => {
                let f = val.as_f64().unwrap_or(0.0);
                LittleEndian::write_f32(&mut out[offset..], f as f32);
            }
            ColumnType::Double => {
                let f = val.as_f64().unwrap_or(0.0);
                LittleEndian::write_f64(&mut out[offset..], f);
            }
            ColumnType::String | ColumnType::DateTime => {
                let s = val.as_str().unwrap_or("");
                LittleEndian::write_u32(&mut out[offset..], s.len() as u32);
                out[offset + size_of::<u32>()..offset + size_of::<u32>() + s.len()]
                    .copy_from_slice(s.as_bytes());
            }
            ColumnType::Json => {
                let json = serde_json::to_string(val).unwrap_or_default();
                LittleEndian::write_u32(&mut out[offset..], json.len() as u32);
                out[offset + size_of::<u32>()..offset + size_of::<u32>() + json.len()]
                    .copy_from_slice(json.as_bytes());
            }
            ColumnType::Binary => {
                let s = val.as_str().unwrap_or("");
                LittleEndian::write_u32(&mut out[offset..], s.len() as u32);
                out[offset + size_of::<u32>()..offset + size_of::<u32>() + s.len()]
                    .copy_from_slice(s.as_bytes());
            }
            _ => unreachable!(),
        }
    }
    out
}

#[derive(Clone, PartialEq, Debug)]
pub enum AttributeIndexEntry {
    Bool { index: u16, val: bool },
    Int { index: u16, val: i32 },
    UInt { index: u16, val: u32 },
    Long { index: u16, val: i64 },
    ULong { index: u16, val: u64 },
    Float { index: u16, val: f32 },
    Double { index: u16, val: f64 },
    String { index: u16, val: String },
    DateTime { index: u16, val: DateTime<Utc> },
    Short { index: u16, val: i16 },
    UShort { index: u16, val: u16 },
    Byte { index: u16, val: u8 },
    UByte { index: u16, val: u8 },
    Json { index: u16, val: String },
    Binary { index: u16, val: String },
}

pub fn cityfeature_to_index_entries(
    cityfeature: &CityJSONFeature,
    schema: &AttributeSchema,
    indexing_attr: &[String],
) -> Vec<AttributeIndexEntry> {
    let mut index_entries = Vec::new();
    for object in cityfeature.city_objects.values() {
        if let Some(attr) = &object.attributes {
            let attr_index_entries = attribute_to_index_entries(attr, schema, indexing_attr);
            index_entries.extend(attr_index_entries);
        }
    }

    index_entries
}

// this attr should be a json object with attribute name as key and attribute value as value
pub fn attribute_to_index_entries(
    attr: &Value,
    schema: &AttributeSchema,
    indexing_attr: &[String],
) -> Vec<AttributeIndexEntry> {
    if !attr.is_object() || attr.is_null() || attr.as_object().unwrap().is_empty() {
        return Vec::new();
    }

    let mut index_entries = Vec::new();

    let map = attr.as_object().unwrap();
    for attr in indexing_attr {
        let val: &Value = match map.get(attr) {
            Some(val) => val,
            None => {
                println!("Attribute {attr} not found in schema");
                continue;
            }
        };

        let index_coltype = schema.get(attr);
        if let Some((index, coltype)) = index_coltype {
            match *coltype {
                ColumnType::Bool => {
                    let b = val.as_bool().unwrap_or(false);
                    index_entries.push(AttributeIndexEntry::Bool {
                        index: *index,
                        val: b,
                    });
                }
                ColumnType::Int => {
                    let i = val.as_i64().unwrap_or(0);
                    index_entries.push(AttributeIndexEntry::Int {
                        index: *index,
                        val: i as i32,
                    });
                }
                ColumnType::UInt => {
                    let i = val.as_u64().unwrap_or(0);
                    index_entries.push(AttributeIndexEntry::UInt {
                        index: *index,
                        val: i as u32,
                    });
                }
                ColumnType::Long => {
                    let i = val.as_i64().unwrap_or(0);
                    index_entries.push(AttributeIndexEntry::Long {
                        index: *index,
                        val: i as i64,
                    });
                }
                ColumnType::ULong => {
                    let i = val.as_u64().unwrap_or(0);
                    index_entries.push(AttributeIndexEntry::ULong {
                        index: *index,
                        val: i as u64,
                    });
                }
                ColumnType::Float => {
                    let f = val.as_f64().unwrap_or(0.0);
                    index_entries.push(AttributeIndexEntry::Float {
                        index: *index,
                        val: f as f32,
                    });
                }
                ColumnType::Double => {
                    let f = val.as_f64().unwrap_or(0.0);
                    index_entries.push(AttributeIndexEntry::Double {
                        index: *index,
                        val: f,
                    });
                }
                ColumnType::String => {
                    index_entries.push(AttributeIndexEntry::String {
                        index: *index,
                        val: val.as_str().unwrap_or("").to_string(),
                    });
                }
                ColumnType::DateTime => {
                    let dt = match chrono::DateTime::parse_from_rfc3339(val.as_str().unwrap_or(""))
                    {
                        Ok(dt) => dt.to_utc(),
                        Err(e) => {
                            eprintln!("Failed to parse DateTime: {e}");
                            // Choose whether to skip, default, or handle differently
                            // For example, default to 1970-01-01:
                            DateTime::<Utc>::from_timestamp(0, 0).unwrap()
                        }
                    };
                    index_entries.push(AttributeIndexEntry::DateTime {
                        index: *index,
                        val: dt,
                    });
                }
                _ => {
                    //Byte, Ubyte,
                    println!("Attribute {attr} is not supported for indexing");
                }
            }
        }
    }

    index_entries
}

#[cfg(test)]
mod tests {
    use crate::{
        deserializer::decode_attributes,
        root_as_city_feature, root_as_header,
        serializer::{to_columns, to_fcb_attribute},
        CityFeature, CityFeatureArgs, CityObject, CityObjectArgs, Header, HeaderArgs,
    };

    use super::*;

    use anyhow::Result;
    use flatbuffers::FlatBufferBuilder;
    use pretty_assertions::assert_eq;
    use serde_json::json;

    #[test]
    fn test_add_attributes() -> Result<()> {
        let json_data = json!({
            "attributes": {
                "int": -10,
                "uint": 5,
                "bool": true,
                "float": 1.0,
                "string": "hoge",
                "array": [1, 2, 3],
                "json": {
                    "hoge": "fuga"
                },
                "null": null
            }
        });

        let mut attr_schema: AttributeSchema = AttributeSchema::new();

        attr_schema.add_attributes(&json_data["attributes"]);

        // Check if the schema contains the expected keys and types
        assert_eq!(attr_schema.get("int").unwrap().1, ColumnType::Long);
        assert_eq!(attr_schema.get("uint").unwrap().1, ColumnType::ULong);
        assert_eq!(attr_schema.get("bool").unwrap().1, ColumnType::Bool);
        assert_eq!(attr_schema.get("float").unwrap().1, ColumnType::Double);
        assert_eq!(attr_schema.get("string").unwrap().1, ColumnType::String);
        assert_eq!(attr_schema.get("array").unwrap().1, ColumnType::Json); //TODO: check if this is correct
        assert_eq!(attr_schema.get("json").unwrap().1, ColumnType::Json);

        Ok(())
    }

    #[test]
    fn test_attribute_serialization() -> Result<()> {
        let test_cases = vec![
            // Case 1: Same schema
            (
                json!({
                        "int": -10,
                        "uint": 5,
                        "bool": true,
                        "float": 1.0,
                        "string": "hoge",
                        "array": [1, 2, 3],
                        "json": {
                            "hoge": "fuga"
                        }
                }),
                json!({
                        "int": -10,
                        "uint": 5,
                            "bool": true,
                        "float": 1.0,
                        "string": "hoge",
                        "array": [1, 2, 3],
                        "json": {
                            "hoge": "fuga"
                    },
                }),
                json!({
                    "attributes": {
                        "int": -10,
                        "uint": 5,
                        "bool": true,
                        "float": 1.0,
                        "string": "hoge",
                        "array": [1, 2, 3],
                        "json": {
                            "hoge": "fuga"
                        }
                    }
                }),
                "same schema",
            ),
            // Case 2: JSON with null value
            (
                json!({
                            "int": -10,
                        "uint": 5,
                        "bool": true,
                        "float": 1.0,
                        "string": "hoge",
                        "array": [1, 2, 3],
                        "json": {
                            "hoge": "fuga"
                        },
                        "exception": null
                }),
                json!({
                            "int": -10,
                        "uint": 5,
                        "bool": true,
                        "float": 1.0,
                        "string": "hoge",
                        "array": [1, 2, 3],
                        "json": {
                            "hoge": "fuga"
                        },
                }),
                json!({
                    "attributes": {
                        "int": -10,
                        "uint": 5,
                        "bool": true,
                        "float": 1.0,
                        "string": "hoge",
                        "array": [1, 2, 3],
                        "json": {
                            "hoge": "fuga"
                        },
                        "exception": 1000
                    }
                }),
                "JSON with null value",
            ),
            // Case 3: JSON is empty
            (
                json!({}),
                json!({}),
                json!({
                    "attributes": {
                        "int": -10,
                        "uint": 5,
                        "bool": true,
                        "float": 1.0,
                        "string": "hoge",
                        "array": [1, 2, 3],
                        "json": {
                            "hoge": "fuga"
                        },
                        "exception": 1000
                    }
                }),
                "JSON is empty",
            ),
        ];

        for (input, expected, schema, test_name) in test_cases {
            println!("Testing case: {test_name}");

            let attrs = &input;
            let attr_schema = &schema["attributes"];

            // Create and encode with schema
            let mut fbb = FlatBufferBuilder::new();
            let mut common_schema = AttributeSchema::new();
            common_schema.add_attributes(attr_schema);

            let columns = to_columns(&mut fbb, &common_schema);
            let header = {
                let version = fbb.create_string("1.0.0");
                Header::create(
                    &mut fbb,
                    &HeaderArgs {
                        version: Some(version),
                        columns: Some(columns),
                        ..Default::default()
                    },
                )
            };
            fbb.finish(header, None);

            // Decode and verify
            let finished_data = fbb.finished_data();
            let header_buf = root_as_header(finished_data).unwrap();

            let mut fbb = FlatBufferBuilder::new();
            let feature = {
                let (attr_buf, _) = to_fcb_attribute(&mut fbb, attrs, &common_schema);
                let city_object = {
                    let id = fbb.create_string("test");
                    CityObject::create(
                        &mut fbb,
                        &CityObjectArgs {
                            id: Some(id),
                            attributes: Some(attr_buf),
                            ..Default::default()
                        },
                    )
                };
                let objects = fbb.create_vector(&[city_object]);
                let cf_id = fbb.create_string("test_feature");
                CityFeature::create(
                    &mut fbb,
                    &CityFeatureArgs {
                        id: Some(cf_id),
                        objects: Some(objects),
                        ..Default::default()
                    },
                )
            };

            fbb.finish(feature, None);

            let finished_data = fbb.finished_data();
            let feature_buf = root_as_city_feature(finished_data).unwrap();
            let attributes = feature_buf.objects().unwrap().get(0).attributes().unwrap();

            let decoded = decode_attributes(&header_buf.columns().unwrap(), attributes);

            assert_eq!(
                expected, decoded,
                "decoded data should match original for {}",
                test_name
            );
        }

        Ok(())
    }
}