Skip to content

Instantly share code, notes, and snippets.

@cbur24
Last active May 27, 2021 03:01
Show Gist options
  • Save cbur24/70b161f168bf608613f4968bed20c189 to your computer and use it in GitHub Desktop.
Save cbur24/70b161f168bf608613f4968bed20c189 to your computer and use it in GitHub Desktop.
Create xarray mask from vector
import xarray as xr
import geopandas as gpd
from dea_tools.spatial import xr_rasterize
#read shapefile
mask_vector = gpd.read_file('mask.shp')
#import xarray ds
ds = xr.open_dataset('testing.nc')
#create mask using ds as template
mask = xr_rasterize(gdf=mask_vector, ds)
#mask the ds using the rasterized shape
ds = ds.where(mask)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment