fit_spatial_grid
cellseg_gsontools.grid.fit_spatial_grid(gdf, grid_type='square', **kwargs)
¶
Quick wrapper to fit either a hex or square grid to a geopandas.GeoDataFrame
.
Note
- Hexagonal grid requires the
h3
package to be installed. - Hexagonal grid only works for a gdf containing one single polygon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gdf |
GeoDataFrame
|
GeoDataFrame to fit grid to. |
required |
grid_type |
str
|
Type of grid to fit, by default "square". |
'square'
|
**kwargs |
Dict[str, Any]
|
Keyword arguments to pass to grid fitting functions. |
{}
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: Fitted grid. |
Raises:
Type | Description |
---|---|
ValueError
|
If grid_type is not one of "square" or "hex". |
ImportError
|
If grid_type is "hex" and the |
Examples:
Fit a hexagonal grid to a gdf:
>>> from cellseg_gsontools import read_gdf
>>> from cellseg_gsontools.grid import fit_spatial_grid
>>> # Read in the tissue areas
>>> area_gdf = gpd.read_file("path/to/area.geojson")
>>> # Fit the grid
>>> hex_grid = fit_spatial_grid(area_gdf, grid_type="hex", resolution=9)
>>> hex_grid
gpd.GeoDataFrame
Fit a square grid to a gdf:
>>> from cellseg_gsontools import read_gdf
>>> from cellseg_gsontools.grid import fit_spatial_grid
>>> # Read in the tissue areas
>>> area_gdf = gpd.read_file("path/to/area.geojson")
>>> # Fit the grid
>>> sq_grid = fit_spatial_grid(
... area_gdf, grid_type="square", patch_size=(256, 256), stride=(256, 256)
... )
>>> sq_grid
gpd.GeoDataFrame