FlatCityBuf Core Library
A high-performance Rust library for encoding and decoding CityJSON data to the FlatCityBuf (FCB) binary format. FCB uses FlatBuffers for efficient serialization with support for spatial and attribute indexing.
Features
- Binary Format: Efficient storage using FlatBuffers
- Spatial Indexing: Fast spatial queries with R-tree indexing
- Attribute Indexing: Query features by attribute values
- HTTP Support: Stream features over HTTP with range requests
- Memory Efficient: Streaming readers for large datasets
- CityJSON Compatibility: Full support for CityJSON 2.0 specification
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
# For HTTP support
= { = "0.1.0", = ["http"] }
Quick Start
Writing FCB Files
use ;
use File;
use ;
// read cityjson data
let input_file = open?;
let input_reader = new;
let cj_seq = read_cityjson_from_reader?;
if let Seq = cj_seq
you can also use the fcb_cli to serialize CityJSON to FCB. Check the CLI README for more details.
Reading FCB Files
Read All Features
use ;
use File;
use BufReader;
let input_file = open?;
let input_reader = new;
let mut reader = open?.select_all?;
let header = reader.header;
let cj_metadata = to_cj_metadata?;
println!;
while let Some = reader.next?
Spatial Queries (Bounding Box)
use ;
let mut reader = open?
.select_query?;
while let Some = reader.next?
Attribute Queries
use ;
let query = vec!;
let mut reader = open?.select_attr_query?;
while let Some = reader.next?
HTTP Streaming
use HttpFcbReader;
async
Attribution
Portions of this software are derived from FlatGeobuf (BSD 2-Clause License). See ATTRIBUTION.md for detailed attribution information.
API Reference
Core Types
FcbWriter<'a>
Main writer for serializing CityJSON to FCB format.
Methods:
new(cj, header_options, attr_schema, semantic_attr_schema) -> Result<Self>add_feature(&mut self, feature) -> Result<()>write(self, output) -> Result<()>
FcbReader<R>
Reader for deserializing FCB files.
Methods:
open(reader) -> Result<Self>select_all(self) -> Result<FeatureIter<R, Seekable>>select_query(self, query) -> Result<FeatureIter<R, Seekable>>select_attr_query(self, query) -> Result<FeatureIter<R, Seekable>>select_all_seq(self) -> Result<FeatureIter<R, NotSeekable>>select_query_seq(self, query) -> Result<FeatureIter<R, NotSeekable>>select_attr_query_seq(self, query) -> Result<FeatureIter<R, NotSeekable>>
HttpFcbReader<T>
HTTP-based streaming reader.
Methods:
open(url) -> Result<Self>select_all(self) -> Result<AsyncFeatureIter<T>>select_query(self, query) -> Result<AsyncFeatureIter<T>>select_attr_query(self, query) -> Result<AsyncFeatureIter<T>>
Configuration
HeaderWriterOptions
Configuration for FCB header writing.
AttributeSchema
Schema for managing attribute types and indexing.
Methods:
new() -> Selfadd_attributes(&mut self, attributes)get(&self, name) -> Option<(u16, ColumnType)>
Query Types
Spatial Queries
use Query;
// bounding box query
let bbox_query = BBox;
Attribute Queries
use ;
type AttrQuery = ;
// numeric comparison
let height_query = ;
// string exact match
let type_query = ;
// datetime comparison
let date_query = ;
Supported Operators
Operator::Eq- equalsOperator::Gt- greater thanOperator::Lt- less thanOperator::Gte- greater than or equalOperator::Lte- less than or equal
Supported Key Types
KeyType::Float64(Float)- 64-bit floating pointKeyType::StringKey50(FixedStringKey)- fixed-length strings up to 50 charsKeyType::DateTime(chrono::DateTime<Utc>)- datetime valuesKeyType::UByte(u8)- unsigned 8-bit integer
Error Handling
The library uses thiserror for structured error handling:
use ;
match fcb_reader.select_all
Features
Enable optional features in Cargo.toml:
[]
= { = "0.1.0", = ["http"] }
http- enables HTTP streaming capabilities
Examples
See the tests/ directory for comprehensive examples:
tests/attr_index.rs- attribute indexing and queryingtests/http.rs- HTTP streaming examplestests/e2e.rs- end-to-end serialization/deserializationtests/read.rs- various reading patterns
Related
- FlatCityBuf CLI - command-line tools
- FlatCityBuf WASM - web assembly bindings
- CityJSON Specification
- FlatBuffers
License
MIT License - see LICENSE file for details.