Я использовал пакет rioxarray
, который, кажется, отлично интегрируется с xarray и действительно прост в реализации.
import xarray as xr
import rioxarray as rx
Treecover = xr.open_rasterio('/home/chandra/data/Treecover_MOD44B_2000_250m_AMAZON.tif')
[Output]:
<xarray.DataArray (band: 1, y: 32093, x: 20818)>
[668112074 values with dtype=float64]
Coordinates:
* band (band) int64 1
* y (y) float64 13.71 13.71 13.71 13.71 ... -58.35 -58.35 -58.36 -58.36
* x (x) float64 -81.38 -81.37 -81.37 -81.37 ... -34.63 -34.63 -34.62
Attributes:
transform: (0.002245788210298804, 0.0, -81.37613580017715, 0.0, -0.0022...
crs: +init=epsg:4326
res: (0.002245788210298804, 0.002245788210298804)
is_tiled: 0
nodatavals: (nan,)
geometries = [
{
'type': 'Polygon',
'coordinates': [[
[-46.23140155225633, -21.53505449239459],
[-44.91304217725633, -20.221175092759253],
[-70.22554217725633, 1.5816072875439455],
[-71.36812030225633, 0.5271132528460204]
]]
}
]
Treecover_clipped = Treecover.rio.clip(geometries, Treecover.rio.crs)
[Output]:
<xarray.DataArray (band: 1, y: 10293, x: 11779)>
array([[[nan, nan, ..., nan, nan],
[nan, nan, ..., nan, nan],
...,
[nan, nan, ..., nan, nan],
[nan, nan, ..., nan, nan]]])
Coordinates:
* band (band) int64 1
* y (y) float64 1.58 1.578 1.575 1.573 ... -21.53 -21.53 -21.53
* x (x) float64 -71.37 -71.36 -71.36 ... -44.92 -44.92 -44.91
spatial_ref int64 0
Attributes:
transform: (0.0022457882102988043, 0.0, -71.36665774687539, 0.0, -0.0...
_FillValue: nan
grid_mapping: spatial_ref