Skip to content

Commit

Permalink
encode/decode attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
HideBa committed Jan 11, 2025
1 parent be8a8e9 commit aa8556c
Show file tree
Hide file tree
Showing 17 changed files with 944 additions and 312 deletions.
2 changes: 2 additions & 0 deletions src/fbs/header.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ enum ColumnType: ubyte {
Json, // General JSON type intended to be application specific
DateTime, // ISO 8601 date time
Binary // General binary type intended to be application specific
// Array // Array of values
}

table Column {
index: ushort; // Column index (0 = first column)
name: string (required); // Column name
type: ColumnType; // Column type
title: string; // Column title
Expand Down
30 changes: 15 additions & 15 deletions src/rust/benches/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn read_fcb(path: &str) -> Result<(u64, u64, u64)> {
let mut multi_surface_count = 0;
let mut other_count = 0;
let mut feat_num = 0;
while let Ok(Some(feat_buf)) = reader.next() {
while let Some(feat_buf) = reader.next()? {
let feature = feat_buf.cur_feature();
feature
.objects()
Expand Down Expand Up @@ -97,13 +97,13 @@ mod tests {
}

const DATASETS: &[(&str, (&str, &str))] = &[
(
"3DBAG",
(
"benchmark_data/3DBAG.fcb",
"benchmark_data/3DBAG.city.jsonl",
),
),
// (
// "3DBAG",
// (
// "benchmark_data/3DBAG.fcb",
// "benchmark_data/3DBAG.city.jsonl",
// ),
// ),
// (
// "3DBV",
// ("benchmark_data/3DBV.fcb", "benchmark_data/3DBV.city.jsonl"),
Expand All @@ -122,13 +122,13 @@ const DATASETS: &[(&str, (&str, &str))] = &[
// "benchmark_data/Ingolstadt.city.jsonl",
// ),
// ),
// (
// "Montreal",
// (
// "benchmark_data/Montreal.fcb",
// "benchmark_data/Montreal.city.jsonl",
// ),
// ),
(
"Montreal",
(
"benchmark_data/Montreal.fcb",
"benchmark_data/Montreal.city.jsonl",
),
),
// (
// "NYC",
// ("benchmark_data/NYC.fcb", "benchmark_data/NYC.city.jsonl"),
Expand Down
3 changes: 2 additions & 1 deletion src/rust/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pre-commit:

.PHONY: encode
encode:
cargo run --bin flatcitybuf_cli serialize -i tests/data/delft.city.jsonl -o temp/delft.fcb
cargo run --bin flatcitybuf_cli serialize -i tests/data/delft.city.jsonl -o temp/delft_attr.fcb
# cargo run --bin flatcitybuf_cli serialize -i tests/data/delft.city.jsonl -o temp/delft.fcb

.PHONY: decode
decode:
Expand Down
9 changes: 4 additions & 5 deletions src/rust/src/bin/read.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use flatcitybuf::fcb_deserializer::{to_cj_feature, to_cj_metadata};
use flatcitybuf::fcb_deserializer::to_cj_metadata;
use flatcitybuf::FcbReader;
use std::error::Error;
use std::fs::File;
Expand All @@ -20,16 +20,15 @@ fn read_file() -> Result<(), Box<dyn Error>> {
let mut reader = FcbReader::open(inputreader)?.select_all()?;
let header = reader.header();
let cj = to_cj_metadata(&header)?;

let mut features = Vec::new();
let feat_count = header.features_count();
let mut feat_num = 0;
while let Ok(Some(feat_buf)) = reader.next() {
let feature = feat_buf.cur_feature();
while let Some(feat_buf) = reader.next()? {
let feature = feat_buf.cur_cj_feature()?;
if feat_num == 0 {
println!("feature: {:?}", feature);
}
features.push(to_cj_feature(feature)?);
features.push(feature);
feat_num += 1;
if feat_num >= feat_count {
break;
Expand Down
11 changes: 10 additions & 1 deletion src/rust/src/bin/write.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use flatcitybuf::attribute::{AttributeSchema, AttributeSchemaMethods};
use flatcitybuf::header_writer::{HeaderMetadata, HeaderWriterOptions};
use flatcitybuf::{read_cityjson_from_reader, CJType, CJTypeKind, CityJSONSeq, FcbWriter};
use std::error::Error;
Expand Down Expand Up @@ -28,7 +29,15 @@ fn write_file() -> Result<(), Box<dyn Error>> {
write_index: false,
header_metadata,
});
let mut fcb = FcbWriter::new(cj, header_options, features.first())?;
let mut attr_schema = AttributeSchema::new();
for feature in features.iter() {
for (_, co) in feature.city_objects.iter() {
if let Some(attributes) = &co.attributes {
attr_schema.add_attributes(attributes);
}
}
}
let mut fcb = FcbWriter::new(cj, header_options, features.first(), Some(&attr_schema))?;
fcb.write_feature()?;
for feature in features.iter().skip(1) {
fcb.add_feature(feature)?;
Expand Down
116 changes: 58 additions & 58 deletions src/rust/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
use flatbuffers::InvalidFlatbuffer;
use std::fmt::{Display, Formatter};
// use flatbuffers::InvalidFlatbuffer;
// use std::fmt::{Display, Formatter};

#[derive(Debug)]
pub enum Error {
MissingMagicBytes,
NoIndex,
// #[cfg(feature = "http")]
// HttpClient(http_range_client::HttpError),
IllegalHeaderSize(usize),
InvalidFlatbuffer(InvalidFlatbuffer),
IO(std::io::Error),
}
pub type Result<T> = std::result::Result<T, Error>;

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Error::MissingMagicBytes => "Missing magic bytes. Is this an fgb file?".fmt(f),
Error::NoIndex => "Index missing".fmt(f),
// #[cfg(feature = "http")]
// Error::HttpClient(http_client) => http_client.fmt(f),
Error::IllegalHeaderSize(size) => write!(f, "Illegal header size: {size}"),
Error::InvalidFlatbuffer(invalid_flatbuffer) => invalid_flatbuffer.fmt(f),
Error::IO(io) => io.fmt(f),
}
}
}
// #[derive(Debug)]
// pub enum Error {
// MissingMagicBytes,
// NoIndex,
// // #[cfg(feature = "http")]
// // HttpClient(http_range_client::HttpError),
// IllegalHeaderSize(usize),
// InvalidFlatbuffer(InvalidFlatbuffer),
// IO(std::io::Error),
// }
// pub type Result<T> = std::result::Result<T, Error>;

impl std::error::Error for Error {}
// impl Display for Error {
// fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
// match self {
// Error::MissingMagicBytes => "Missing magic bytes. Is this an fgb file?".fmt(f),
// Error::NoIndex => "Index missing".fmt(f),
// // #[cfg(feature = "http")]
// // Error::HttpClient(http_client) => http_client.fmt(f),
// Error::IllegalHeaderSize(size) => write!(f, "Illegal header size: {size}"),
// Error::InvalidFlatbuffer(invalid_flatbuffer) => invalid_flatbuffer.fmt(f),
// Error::IO(io) => io.fmt(f),
// }
// }
// }

impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Self::IO(value)
}
}
// impl std::error::Error for Error {}

impl From<InvalidFlatbuffer> for Error {
fn from(value: InvalidFlatbuffer) -> Self {
Error::InvalidFlatbuffer(value)
}
}
// impl From<std::io::Error> for Error {
// fn from(value: std::io::Error) -> Self {
// Self::IO(value)
// }
// }

// #[cfg(feature = "http")]
// impl From<http_range_client::HttpError> for Error {
// fn from(value: http_range_client::HttpError) -> Self {
// Error::HttpClient(value)
// impl From<InvalidFlatbuffer> for Error {
// fn from(value: InvalidFlatbuffer) -> Self {
// Error::InvalidFlatbuffer(value)
// }
// }

#[derive(Debug)]
pub enum CityJSONError {
MissingField(&'static str),
// ParseError(String),
// InvalidData(&'static str),
}
impl std::error::Error for CityJSONError {}
// // #[cfg(feature = "http")]
// // impl From<http_range_client::HttpError> for Error {
// // fn from(value: http_range_client::HttpError) -> Self {
// // Error::HttpClient(value)
// // }
// // }

// #[derive(Debug)]
// pub enum CityJSONError {
// MissingField(&'static str),
// // ParseError(String),
// // InvalidData(&'static str),
// }
// impl std::error::Error for CityJSONError {}

impl std::fmt::Display for CityJSONError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CityJSONError::MissingField(field) => write!(f, "Missing field: {}", field),
// CityJSONError::ParseError(err) => write!(f, "Parse error: {}", err),
// CityJSONError::InvalidData(msg) => write!(f, "Invalid data: {}", msg),
}
}
}
// impl std::fmt::Display for CityJSONError {
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// match self {
// CityJSONError::MissingField(field) => write!(f, "Missing field: {}", field),
// // CityJSONError::ParseError(err) => write!(f, "Parse error: {}", err),
// // CityJSONError::InvalidData(msg) => write!(f, "Invalid data: {}", msg),
// }
// }
// }
Loading

0 comments on commit aa8556c

Please sign in to comment.