- imod.prepare.celltable(path, column, resolution, like, chunksize=10000.0)#
Process area of features by rasterizing in a chunkwise manner to limit memory usage.
Returns a table of cell indices (row, column) with for example feature ID, and feature area within cell. Essentially returns a COO sparse matrix, but with duplicate values per cell, since more than one geometry may be present.
The feature area within the cell is approximated by first rasterizing the feature, and then counting the number of occuring cells. This means the accuracy of the area depends on the cellsize of the rasterization step.
A celltable is returned, as a
pandas.DataFrame. It has the following columns:
the value of the
"col_index"are the indices of the like array in which the polygon is located. The
columnvalue holds the rasterized value of the specified column.
"area"contains the area of the polygon within the cell.
The most convenient way of using this celltable is by specifying a feature ID as
column. After creating a celltable,
pandas.DataFrame.merge()can be used to join additional data on this ID. Refer to the examples.
path (str or pathlib.Path) – path to OGR supported vector file (e.g. a shapefile)
column (str) – column name of column to burn into raster
resolution (float) – cellsize at which the rasterization, and determination of area within cellsize occurs. Very small values are recommended (e.g. <= 0.5 m).
like (xarray.DataArray) – Example DataArray of where the cells will be located. Used only for the coordinates.
chunksize (int, optional) – The size of the chunksize. Used for both x and y dimension.
- Return type:
Assume we have a shapefile called
waterways.shpand information on the model discretization is described by a
likeDataArray. The feature ID is provided by a column in the shapefile called “ID-code”. Additionally, this shapefile also specifies bed hydraulic resistance (c0). For this specific discretization, we wish to calculate a conductance (area divided by hydraulic resistance). To do so, we:
join the additional attributes (such as c0)
compute the conductance per feature
sum conductances per cell
Import the required packages.
>>> import imod >>> import geopandas as gpd
Generate the celltable.
>>> celltable = imod.prepare.celltable( path="waterways.shp", column="ID-code", resolution=0.5, like=like, )
Load the shapefile with geopandas into a
>>> gdf = gpd.read_file("waterways.shp)
Select the relevant columns into a
pandas.DataFrameand merge with the celltable.
>>> df = gdf[["ID-code", "c0"]] >>> joined = celltable.merge(gdf, on="ID-code")
We compute the conductance, and sum it per cell using
>>> joined["conductance"] = joined["area"] / joined["c0"] >>> summed_conductance = joined.groupby(["row_index", "col_index"], as_index=False)[ "conductance" ].sum()
Finally, turn the result into a DataArray so it can be used as model input:
>>> conductance = imod.prepare.rasterize_celltable( table=summed_conductance, column="conductance", like=like, )