polygon_to_array#

geowombat.polygon_to_array(polygon, col=None, data=None, cellx=None, celly=None, band_name=None, row_chunks=512, col_chunks=512, src_res=None, fill=0, default_value=1, all_touched=True, dtype='uint8', sindex=None, tap=False, bounds_by='intersection')#

Converts a polygon geometry to an xarray.DataArray.

Parameters:
  • polygon (GeoDataFrame | str) – The geopandas.DataFrame or file with polygon geometry.

  • col (Optional[str]) – The column in polygon you want to assign values from. If not set, creates a binary raster.

  • data (Optional[DataArray]) – An xarray.DataArray to use as a reference for rasterizing.

  • cellx (Optional[float]) – The output cell x size.

  • celly (Optional[float]) – The output cell y size.

  • band_name (Optional[list]) – The xarray.DataArray band name.

  • row_chunks (Optional[int]) – The dask row chunk size.

  • col_chunks (Optional[int]) – The dask column chunk size.

  • (Optional[tuple] (src_res) – A source resolution to align to.

  • fill (Optional[int]) – Used as fill value for all areas not covered by input geometries to rasterio.features.rasterize.

  • default_value (Optional[int]) – Used as value for all geometries, if not provided in shapes to rasterio.features.rasterize.

  • all_touched (Optional[bool]) – If True, all pixels touched by geometries will be burned in. If false, only pixels whose center is within the polygon or that are selected by Bresenham’s line algorithm will be burned in. The all_touched value for rasterio.features.rasterize().

  • dtype (Optional[str | numpy data type]) – The output data type for rasterio.features.rasterize().

  • sindex (Optional[object]) – An instanced of geopandas.GeoDataFrame.sindex.

  • tap (Optional[bool]) – Whether to target align pixels.

  • bounds_by (Optional[str]) –

    How to concatenate the output extent. Choices are [‘intersection’, ‘union’, ‘reference’].

    • reference: Use the bounds of the reference image

    • intersection: Use the intersection (i.e., minimum extent) of all the image bounds

    • union: Use the union (i.e., maximum extent) of all the image bounds

  • src_res (Sequence[float] | None) –

Return type:

DataArray

Returns:

xarray.DataArray

Example

>>> import geowombat as gw
>>> import geopandas as gpd
>>>
>>> df = gpd.read_file('polygons.gpkg')
>>>
>>> # 100x100 cell size
>>> data = gw.polygon_to_array(df, 100.0, 100.0)
>>>
>>> # Align to an existing image
>>> with gw.open('image.tif') as src:
>>>     data = gw.polygon_to_array(df, data=src)