FlatGeobuf

Overview

FlatGeobuf is a performant binary encoding for geographic data based on FlatBuffers. It supports random access via HTTP range requests, making it ideal for streaming large datasets over the web without downloading the entire file.

Key Features

FeatureDescription
Binary FormatCompact, fast to parse
StreamingHTTP range request support
Spatial IndexOptional R-tree for filtered reads
Single FileAll data in one .fgb file
SeekableRandom access to features
Attribute TypesFull type support

Advantages

AspectFlatGeobufGeoJSONShapefile
SizeSmallLargeMedium
Parse speedFastSlowMedium
StreamingYesNoNo
Spatial indexBuilt-inNoSeparate
Single fileYesYesNo

Usage

GDAL/OGR

# convert geojson to flatgeobuf
ogr2ogr -f FlatGeobuf output.fgb input.geojson
 
# with spatial index
ogr2ogr -f FlatGeobuf -lco SPATIAL_INDEX=YES output.fgb input.geojson
 
# query with bbox filter
ogrinfo output.fgb -spat -122.5 37.7 -122.3 37.9

JavaScript (Web)

import { geojson } from 'flatgeobuf';
 
// stream features from URL with bbox filter
const bbox = { minX: -122.5, minY: 37.7, maxX: -122.3, maxY: 37.9 };
const iter = geojson.deserialize('https://example.com/data.fgb', bbox);
 
for await (const feature of iter) {
  console.log(feature);
}

Leaflet Integration

import { geojson } from 'flatgeobuf';
 
async function loadFlatGeobuf(map, url) {
  const response = await fetch(url);
  const iter = geojson.deserialize(response.body);
  
  const layer = L.geoJSON();
  for await (const feature of iter) {
    layer.addData(feature);
  }
  layer.addTo(map);
}

R

library(sf)
 
# read flatgeobuf
features <- sf::st_read("data.fgb")
 
# write flatgeobuf
sf::st_write(features, "output.fgb", driver = "FlatGeobuf")

Python

import geopandas as gpd
 
# read
gdf = gpd.read_file("data.fgb")
 
# write
gdf.to_file("output.fgb", driver="FlatGeobuf")

When to Use

Choose FlatGeobuf when:

  • Web streaming of vector data
  • Need bbox filtering via HTTP
  • Medium datasets (10-500MB)
  • Binary efficiency matters
  • Building web map applications

Consider alternatives when:

Comparison with GeoParquet

AspectFlatGeobufGeoParquet
OrientationRow-basedColumnar
Best forFeature streamingAnalytics
CompressionModerateExcellent
Column selectionNoYes
EcosystemWeb mappingData science

Appendix

Created: 2024-12-23 | Modified: 2024-12-23

See Also


(c) No Clocks, LLC | 2024