Rasterize ↔ vectorize¶
Cross the raster/vector divide both ways:
- Vectorize a raster:
to_feature_collection()(polygonize equal-valued regions) andcontour(interval=...)(iso-value lines). - Rasterize a vector:
Dataset.from_features(fc, ...)burns an attribute column into a new raster grid.
Setup¶
In [1]:
Copied!
%matplotlib inline
import tempfile
from pathlib import Path
import numpy as np
DATA = Path('../../../examples/data')
WORK = Path(tempfile.mkdtemp(prefix='pyramids-ops-'))
DATA.is_dir(), WORK.is_dir()
%matplotlib inline
import tempfile
from pathlib import Path
import numpy as np
DATA = Path('../../../examples/data')
WORK = Path(tempfile.mkdtemp(prefix='pyramids-ops-'))
DATA.is_dir(), WORK.is_dir()
Out[1]:
(True, True)
In [2]:
Copied!
from pyramids.dataset import Dataset
from pyramids.feature import FeatureCollection
ds = Dataset.read_file(DATA / 'acc4000.tif')
ds.shape, ds.epsg
from pyramids.dataset import Dataset
from pyramids.feature import FeatureCollection
ds = Dataset.read_file(DATA / 'acc4000.tif')
ds.shape, ds.epsg
2026-07-11 14:42:10 | INFO | pyramids.base.config | Logging is configured.
Out[2]:
((1, 13, 14), 32618)
Vectorize — polygonize¶
to_feature_collection(add_geometry='Polygon') merges equal-valued cells into polygons.
In [3]:
Copied!
polys = ds.to_feature_collection(add_geometry='Polygon')
type(polys).__name__, len(polys), list(polys.columns)
polys = ds.to_feature_collection(add_geometry='Polygon')
type(polys).__name__, len(polys), list(polys.columns)
Out[3]:
('GeoDataFrame', 89, ['Band_1', 'geometry'])
Plot the polygons produced by polygonizing the raster — each equal-valued region becomes a polygon.
In [4]:
Copied!
polys.plot(column='Band_1')
polys.plot(column='Band_1')
Out[4]:
<Axes: >
Vectorize — contour lines¶
contour(interval=...) traces iso-value lines into a FeatureCollection.
In [5]:
Copied!
lines = ds.contour(interval=10.0)
type(lines).__name__, len(lines)
lines = ds.contour(interval=10.0)
type(lines).__name__, len(lines)
Out[5]:
('FeatureCollection', 19)
Rasterize — burn a vector into a raster¶
Dataset.from_features rasterises an attribute column onto a new grid. Give it a numeric
column to burn and a cell_size (or a template raster to inherit the grid).
In [6]:
Copied!
zones = FeatureCollection.read_file(DATA / 'coello_polygons.geojson')
zones['value'] = np.arange(1, len(zones) + 1, dtype='int32') # numeric column to burn
burned = Dataset.from_features(zones, cell_size=ds.cell_size, column_name='value')
burned.shape, burned.epsg
zones = FeatureCollection.read_file(DATA / 'coello_polygons.geojson')
zones['value'] = np.arange(1, len(zones) + 1, dtype='int32') # numeric column to burn
burned = Dataset.from_features(zones, cell_size=ds.cell_size, column_name='value')
burned.shape, burned.epsg
Out[6]:
((1, 13, 13), 32618)
The round-trip, visualized: the source vector zones (coloured by the value column) and the raster they
were burned into.
In [7]:
Copied!
zones.plot(column='value')
burned.plot(band=0, title='rasterized zones')
zones.plot(column='value')
burned.plot(band=0, title='rasterized zones')
Out[7]:
<cleopatra.array_glyph.ArrayGlyph at 0x7fded0f5a3c0>
Notes¶
to_feature_collection()withoutadd_geometryreturns a plain table of cell values; passadd_geometry='Polygon'(or'Point') to attach geometry.from_features(..., template=ds)inherits an existing raster's grid instead ofcell_size.sieve()removes speckle (small regions) before vectorizing — useful on classified rasters.- See also: Zonal statistics, Crop & mask.