Loading Vector Data into the Raster Substrate
DisSModel can load any vector file (Shapefile, GeoJSON, GeoPackage, or a
.zip containing a shapefile) and convert it directly into a
RasterBackend, so raster models can run on real geographic data without
intermediate GIS steps.
How it works
vector file → GeoDataFrame → rasterize → NumPy arrays → RasterBackend → raster model
The rasterization step (powered by rasterio.features.rasterize) runs
once; after that the model runs entirely on NumPy arrays.
!!! note "Grid regularity" This workflow is most accurate when the input already contains a regular grid of equal-area polygons (e.g. 100×100 m cells), the typical output of spatial homogenization tools. For irregular polygons (municipalities, watersheds), cell values are burned by centroid or by touch — inspect the result before running long simulations.
vector_to_raster_backend
from dissmodel.io.convert import vector_to_raster_backend
b = vector_to_raster_backend(
source = "data/mangue_grid.shp", # path, .zip, or a GeoDataFrame
resolution = 100, # 100 m cells
attrs = ["uso", "alt", "solo"],
crs = "EPSG:31984", # reproject if needed
)
print(b.shape) # (rows, cols) from bounding box + resolution
print(b.band_names()) # ['mask', 'uso', 'alt', 'solo']
Parameters
| Parameter | Type | Description |
|---|---|---|
source |
str, Path or GeoDataFrame |
Vector file or in-memory GeoDataFrame |
resolution |
float |
Cell size in CRS units (metres for a metric CRS) |
attrs |
list[str] or dict[str, default] |
Columns to rasterize |
crs |
str, int, or None |
Target CRS — reprojects if needed |
all_touched |
bool |
Burn all touched cells (default: centre only) |
nodata |
int or float |
Fill value for uncovered cells (default: 0) |
add_mask |
bool |
Add a "mask" band marking covered cells (default: True) |
To set per-column defaults for uncovered cells, pass a dict:
b = vector_to_raster_backend(
source = "data/mangue_grid.shp",
resolution = 100,
attrs = {"uso": 5, "alt": 0.0, "solo": 1},
crs = "EPSG:31984",
)
shapefile_to_raster_backend is a deprecated alias of this function and
emits a FutureWarning.
Full example — raster model from a vector file
from dissmodel.core import Environment
from dissmodel.io.convert import vector_to_raster_backend
from dissmodel.visualization.raster_map import RasterMap
# your own RasterModel subclass, e.g. the BR-MANGUE flood model
from myproject.flood import FloodRasterModel
# 1. vector file → RasterBackend
b = vector_to_raster_backend(
source = "data/mangue_grid.shp",
resolution = 100,
attrs = {"uso": 5, "alt": 0.0, "solo": 1},
crs = "EPSG:31984",
)
print(f"Grid: {b.shape[0]} rows × {b.shape[1]} cols = {b.shape[0] * b.shape[1]:,} cells")
# 2. run the raster model
env = Environment(start_time=2012, end_time=2100)
FloodRasterModel(backend=b, taxa=0.011)
RasterMap(backend=b, band="uso", title="Land Use")
env.run()
Saving results to GeoTIFF
vector_to_raster_backend stores the CRS and affine transform on the
backend, so save_geotiff writes a georeferenced file without extra
arguments:
from dissmodel.io.raster import save_geotiff
checksum = save_geotiff(b, "output/flood_result.tif") # returns SHA-256
Same data, two substrates
# ── vector substrate ─────────────────────────────────────────────────────────
import geopandas as gpd
gdf = gpd.read_file("data/mangue_grid.shp").to_crs("EPSG:31984")
# → GeoDataFrame with real geometries; use with SpatialModel / CellularAutomaton
# ── raster substrate ─────────────────────────────────────────────────────────
from dissmodel.io.convert import vector_to_raster_backend
b = vector_to_raster_backend(
"data/mangue_grid.shp", resolution=100,
attrs=["uso", "alt", "solo"], crs="EPSG:31984",
)
# → RasterBackend with the same attributes; use with RasterModel
The data source is the same file; only the substrate changes. For measured
run times of the two substrates on the same model, see the benchmarks in
benchmarks/ and the brmangue-dissmodel benchmark executor.
API Reference
dissmodel.io.convert.vector_to_raster_backend(source, resolution, attrs, crs=None, all_touched=False, nodata=0, nodata_value=None, add_mask=True)
Convert a vector source to a RasterBackend.
Accepts a file path (Shapefile, GeoJSON, GPKG, .zip) or an in-memory GeoDataFrame. Each requested attribute column is rasterized into a separate band.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str, Path, or GeoDataFrame
|
Vector source. File paths are read with GeoPandas; GeoDataFrames are used directly (a copy is made before any reprojection). |
required |
resolution
|
float
|
Cell size in the units of the CRS. |
required |
attrs
|
list[str] or dict[str, Any]
|
Columns to rasterize. A list uses |
required |
crs
|
str, int, or None
|
Target CRS for reprojection. If |
None
|
all_touched
|
bool
|
If |
False
|
nodata
|
int or float
|
Default fill for cells outside geometries. Default: |
0
|
nodata_value
|
int or float or None
|
Sentinel for out-of-extent cells. Useful when |
None
|
add_mask
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
RasterBackend
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
FileNotFoundError
|
If a file path does not exist. |
ValueError
|
If |
Examples:
# From file path
b = vector_to_raster_backend(
"data/mangue_grid.shp", resolution=100, attrs=["uso", "alt"]
)
# From in-memory GeoDataFrame
import geopandas as gpd
gdf = gpd.read_file("data/mangue_grid.shp").to_crs("EPSG:31984")
b = vector_to_raster_backend(gdf, resolution=100, attrs={"uso": -1})
Source code in dissmodel/io/convert.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
dissmodel.io.raster.save_geotiff(data, uri, minio_client=None, band_spec=None, crs=None, transform=None, compress='deflate', **kwargs)
Save a RasterBackend as GeoTIFF to any URI.
Supports local path and s3://. Returns sha256 checksum of the saved file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
(RasterBackend, dict) or RasterBackend
|
Backend and metadata dict (as returned by load_geotiff), or the backend alone. |
required |
uri
|
str
|
Destination URI. Local path or s3://bucket/key. |
required |
band_spec
|
list of (name, dtype, nodata) or None
|
Bands to write in order. Missing bands are filled with nodata. If None, all arrays in the backend are written. |
None
|
crs
|
str or None
|
CRS string (e.g. "EPSG:31984"). Overrides meta["crs"], which
overrides |
None
|
transform
|
Affine or None
|
Affine geotransform. Overrides meta["transform"], which overrides
|
None
|
compress
|
str
|
Compression algorithm. Default: "deflate". |
'deflate'
|
Notes
GeoTIFF stores a single dtype per file. When band dtypes differ
(e.g. an int32 categorical band alongside a float32 continuous band),
all bands are promoted to their common NumPy result type
(np.result_type) before writing.
Source code in dissmodel/io/raster.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |