Crate flatgeobuf

Source
Expand description

FlatGeobuf is a performant binary encoding for geographic data based on flatbuffers that can hold a collection of Simple Features including circular interpolations as defined by SQL-MM Part 3.

§Reading a FlatGeobuf file

use flatgeobuf::*;
use geozero::ToJson;

let mut filein = BufReader::new(File::open("countries.fgb")?);
let mut fgb = FgbReader::open(&mut filein)?.select_all()?;
while let Some(feature) = fgb.next()? {
    println!("{}", feature.property::<String>("name").unwrap());
    println!("{}", feature.to_json()?);
}

§Reading FlatGeobuf via HTTP

use flatgeobuf::*;
use geozero::ToWkt;

let mut fgb = HttpFgbReader::open("https://flatgeobuf.org/test/data/countries.fgb")
    .await?
    .select_bbox(8.8, 47.2, 9.5, 55.3)
    .await?;
while let Some(feature) = fgb.next().await? {
    let props = feature.properties()?;
    println!("{}", props["name"]);
    println!("{}", feature.to_wkt()?);
}

§Writing a FlatGeobuf file

use flatgeobuf::*;
use geozero::geojson::GeoJsonReader;
use geozero::GeozeroDatasource;

let mut fgb = FgbWriter::create("countries", GeometryType::MultiPolygon)?;
let mut fin = BufReader::new(File::open("countries.geojson")?);
let mut reader = GeoJsonReader(&mut fin);
reader.process(&mut fgb)?;
let mut fout = BufWriter::new(File::create("countries.fgb")?);
fgb.write(&mut fout)?;

§geo_traits integration

This crate integrates with geo_traits for easy zero-copy vector data interoperability. It can be easier to use than geozero in some cases because you get random access to the underlying coordinates, rather than needing to receive a stream of coordinates. This means that you can write algorithms based on geo_traits and pass in FlatGeobuf objects directly. Because the underlying Flatbuffers support zero-copy access, you could even memory-map a FlatGeobuf file directly.

Use FgbFeature::geometry_trait to access an opaque object that implements geo_traits::GeometryTrait. Then with geo_traits::GeometryTrait::as_type you can match on the geometry type, downcasting to a trait implementation of concrete type.

use flatgeobuf::*;
use geo_traits::{GeometryTrait, GeometryType};

fn assert_multi_polygon(geom: &impl GeometryTrait<T = f64>) {
    assert!(matches!(geom.as_type(), GeometryType::MultiPolygon(_)))
}

let mut filein = BufReader::new(File::open("countries.fgb")?);
let mut fgb = FgbReader::open(&mut filein)?.select_all()?;
while let Some(feature) = fgb.next()? {
    println!("{}", feature.property::<String>("name").unwrap());
    // Assert that each feature is a multi polygon type
    assert_multi_polygon(&feature.geometry_trait().unwrap().unwrap());
}

Re-exports§

pub use geozero;

Modules§

packed_r_tree
Create and read a packed Hilbert R-Tree to enable fast bounding box spatial filtering.

Structs§

AsyncFeatureIter
Column
ColumnArgs
ColumnBuilder
ColumnType
Crs
CrsArgs
CrsBuilder
Feature
FeatureArgs
FeatureBuilder
FeatureIter
FgbCrs
FgbFeature
Access to current feature
FgbReader
FlatGeobuf dataset reader
FgbWriter
FlatGeobuf dataset writer
FgbWriterOptions
Options for FlatGeobuf writer
Geometry
GeometryArgs
GeometryBuilder
GeometryType
Header
HeaderArgs
HeaderBuilder
HttpFgbReader
FlatGeobuf dataset HTTP reader
NotSeekable
Seekable

Enums§

ColumnOffset
CrsOffset
Error
FeatureOffset
GeometryOffset
HeaderOffset

Constants§

ENUM_MAX_COLUMN_TYPEDeprecated
ENUM_MAX_GEOMETRY_TYPEDeprecated
ENUM_MIN_COLUMN_TYPEDeprecated
ENUM_MIN_GEOMETRY_TYPEDeprecated
ENUM_VALUES_COLUMN_TYPEDeprecated
ENUM_VALUES_GEOMETRY_TYPEDeprecated
VERSION

Traits§

FallibleStreamingIterator
A fallible, streaming iterator.
FeatureAccess
Feature processing API
FeatureProperties
Feature properties processing API
GeozeroGeometry
Geometry processing trait.

Functions§

finish_feature_buffer
finish_header_buffer
finish_size_prefixed_feature_buffer
finish_size_prefixed_header_buffer
read_geometry
Read FlatGeobuf geometry
root_as_feature
Verifies that a buffer of bytes contains a Feature and returns it. Note that verification is still experimental and may not catch every error, or be maximally performant. For the previous, unchecked, behavior use root_as_feature_unchecked.
root_as_feature_unchecked
Assumes, without verification, that a buffer of bytes contains a Feature and returns it.
root_as_feature_with_opts
Verifies, with the given options, that a buffer of bytes contains a Feature and returns it. Note that verification is still experimental and may not catch every error, or be maximally performant. For the previous, unchecked, behavior use root_as_feature_unchecked.
root_as_header
Verifies that a buffer of bytes contains a Header and returns it. Note that verification is still experimental and may not catch every error, or be maximally performant. For the previous, unchecked, behavior use root_as_header_unchecked.
root_as_header_unchecked
Assumes, without verification, that a buffer of bytes contains a Header and returns it.
root_as_header_with_opts
Verifies, with the given options, that a buffer of bytes contains a Header and returns it. Note that verification is still experimental and may not catch every error, or be maximally performant. For the previous, unchecked, behavior use root_as_header_unchecked.
size_prefixed_root_as_feature
Verifies that a buffer of bytes contains a size prefixed Feature and returns it. Note that verification is still experimental and may not catch every error, or be maximally performant. For the previous, unchecked, behavior use size_prefixed_root_as_feature_unchecked.
size_prefixed_root_as_feature_unchecked
Assumes, without verification, that a buffer of bytes contains a size prefixed Feature and returns it.
size_prefixed_root_as_feature_with_opts
Verifies, with the given verifier options, that a buffer of bytes contains a size prefixed Feature and returns it. Note that verification is still experimental and may not catch every error, or be maximally performant. For the previous, unchecked, behavior use root_as_feature_unchecked.
size_prefixed_root_as_header
Verifies that a buffer of bytes contains a size prefixed Header and returns it. Note that verification is still experimental and may not catch every error, or be maximally performant. For the previous, unchecked, behavior use size_prefixed_root_as_header_unchecked.
size_prefixed_root_as_header_unchecked
Assumes, without verification, that a buffer of bytes contains a size prefixed Header and returns it.
size_prefixed_root_as_header_with_opts
Verifies, with the given verifier options, that a buffer of bytes contains a size prefixed Header and returns it. Note that verification is still experimental and may not catch every error, or be maximally performant. For the previous, unchecked, behavior use root_as_header_unchecked.

Type Aliases§

Result