rasterstats package¶
-
rasterstats.
gen_zonal_stats
(vectors, raster, layer=0, band_num=1, nodata=None, affine=None, stats=None, all_touched=False, categorical=False, category_map=None, add_stats=None, raster_out=False, prefix=None, geojson_out=False, **kwargs)[source]¶ Zonal statistics of raster values aggregated to vector geometries.
Parameters: vectors: path to an vector source or geo-like python objects
raster: ndarray or path to a GDAL raster source
If ndarray is passed, the
affine
kwarg is required.layer: int or string, optional
If vectors is a path to an fiona source, specify the vector layer to use either by name or number. defaults to 0
band_num: int, optional
If raster is a GDAL source, the band number to use (counting from 1). defaults to 1.
nodata: float, optional
If raster is a GDAL source, this value overrides any NODATA value specified in the file’s metadata. If None, the file’s metadata’s NODATA value (if any) will be used. defaults to None.
affine: Affine instance
required only for ndarrays, otherwise it is read from src
stats: list of str, or space-delimited str, optional
Which statistics to calculate for each zone. All possible choices are listed in
utils.VALID_STATS
. defaults toDEFAULT_STATS
, a subset of these.all_touched: bool, optional
Whether to include every raster cell touched by a geometry, or only those having a center point within the polygon. defaults to False
categorical: bool, optional
category_map: dict
A dictionary mapping raster values to human-readable categorical names. Only applies when categorical is True
add_stats: dict
with names and functions of additional stats to compute, optional
raster_out: boolean
Include the masked numpy array for each feature?, optional
Each feature dictionary will have the following additional keys: mini_raster_array: The clipped and masked numpy array mini_raster_affine: Affine transformation mini_raster_nodata: NoData Value
prefix: string
add a prefix to the keys (default: None)
geojson_out: boolean
Return list of GeoJSON-like features (default: False) Original feature geometry and properties will be retained with zonal stats appended as additional properties. Use with prefix to ensure unique and meaningful property names.
Returns: generator of dicts (if geojson_out is False)
Each item corresponds to a single vector feature and contains keys for each of the specified stats.
generator of geojson features (if geojson_out is True)
GeoJSON-like Feature as python dict
-
rasterstats.
gen_point_query
(vectors, raster, band=1, layer=0, nodata=None, affine=None, interpolate='bilinear', property_name='value', geojson_out=False)[source]¶ Given a set of vector features and a raster, generate raster values at each vertex of the geometry
For features with point geometry, the values will be a 1D with the index refering to the feature
For features with other geometry types, it effectively creates a 2D list, such that the first index is the feature, the second is the vertex within the geometry
Parameters: vectors: path to an vector source or geo-like python objects
raster: ndarray or path to a GDAL raster source
If ndarray is passed, the transform kwarg is required.
layer: int or string, optional
If vectors is a path to an fiona source, specify the vector layer to use either by name or number. defaults to 0
band_num: int, optional
If raster is a GDAL source, the band number to use (counting from 1). defaults to 1.
nodata: float, optional
If raster is a GDAL source, this value overrides any NODATA value specified in the file’s metadata. If None, the file’s metadata’s NODATA value (if any) will be used. defaults to None.
affine: Affine instance
required only for ndarrays, otherwise it is read from src
interpolate: string
‘bilinear’ or ‘nearest’ interpolation
property_name: string
name of property key if geojson_out
geojson_out: boolean
generate GeoJSON-like features (default: False) original feature geometry and properties will be retained point query values appended as additional properties.
Returns: generator of arrays (if
geojson_out
is False)generator of geojson features (if
geojson_out
is True)
-
rasterstats.
zonal_stats
(*args, **kwargs)[source]¶ The primary zonal statistics entry point.
All arguments are passed directly to
gen_zonal_stats
. See its docstring for details.The only difference is that
zonal_stats
will return a list rather than a generator.
-
rasterstats.
point_query
(*args, **kwargs)[source]¶ The primary point query entry point.
All arguments are passed directly to
gen_point_query
. See its docstring for details.The only difference is that
point_query
will return a list rather than a generator.