Skip to main content

Module memory

Module memory 

Source
Expand description

Datasets in memory: opening from bytes and writing to bytes, for callers that hold the data already (uploads, HTTP bodies, language bindings) or have no file system (WebAssembly).

A file name goes with the bytes: its extension tells the format, as a path’s does, and its stem names vector layers.

In native builds, the bytes go through a temporary directory and the file drivers, so every format reads and writes exactly as from files. WebAssembly builds have no file system and use in-memory codecs instead, for the formats that have one:

  • reading: GeoTIFF, Esri ASCII grid, JPEG 2000 (JP2 and J2K), GeoPackage (vector layers and rasters), PMTiles and MBTiles (raster and vector tiles), MVT tiles, GeoJSON, TopoJSON, FlatGeobuf, CSV, GeoParquet, KML, KMZ, GML, GPX, OSM PBF and XML, and zip archives of these, and Shapefile and MapInfo (MIF/MID and TAB) in zip archives (or their .shp or .mif alone);
  • writing: GeoTIFF and COG (with lossless compressions), Esri ASCII grid, JPEG 2000, GeoPackage (vector layers and rasters), PMTiles and MBTiles, GeoJSON, TopoJSON, FlatGeobuf, CSV, GeoParquet, KML, KMZ, GML (without its schema) and GPX.

Formats made of several files (a shapefile’s .shp, .dbf, .prj…) are written as a zip archive of them.

With the gdal feature, what terrio cannot read is opened by GDAL from memory, through its /vsimem/ file system, and extensions terrio has no driver for are written by the GDAL driver that claims them (see gdal_backend::open_bytes and gdal_backend::write_vector_bytes).

let bytes = std::fs::read("countries.geojson")?;
let ds = terrio::memory::open_bytes(bytes, "countries.geojson")?;
let fgb = terrio::memory::write_vector_bytes(&layers, "countries.fgb", &Default::default())?;

Functions§

open_bytes
Opens a dataset, raster or vector, from its bytes. name is a file name whose extension tells the format (dem.tif, roads.fgb, parcels.zip).
open_raster_bytes
Opens a raster from its bytes.
open_raster_lazy_bytes
Opens a raster from its bytes for reading by window. GeoTIFFs are decoded one window at a time; other formats are decoded whole.
open_vector_bytes
Opens a vector dataset from its bytes.
open_zarr_store
Opens a Zarr store (or GeoZarr) from its files: (path, bytes) pairs, paths relative to the store’s directory or its parent (.zgroup, dem/.zarray, dem/0.0…), as a key-value store holds them.
write_raster_bytes
Writes a raster to bytes, declaring its nodata value when it has one. name is a file name whose extension tells the format, unless the options name the driver.
write_vector_bytes
Writes vector layers to bytes. name is a file name whose extension tells the format, unless the options name the driver. GeoPackage, GPX, KML, KMZ and TopoJSON hold several layers.