[Python] rioxarray and clip for a netcdf

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    I'm looking to use a shapefile to rio.clip a raster so I can find where they overlap.

    I'm using the polygon for the overlap (in operation it will be a state or county shapefile, this is simplified for testing):

    {"coordinates": [[[-103.623047,39.808536],[-95.932617,39.808536],[-95.932617,42.90816],[-103.623047,42.90816],[-103.623047,39.808536]]], "type": "Polygon"}


    A description if the raster is below, its read in from a netcdf

    Dimensions: (gust_cat: 3, rose_group: 80, latitude: 1597, longitude: 2145)
    Coordinates:
    Dimensions without coordinates: latitude, longitude
    Data variables:
    lat (latitude, longitude) float64 27MB ...
    lon (latitude, longitude) float64 27MB ...
    ws10m_mean (latitude, longitude) float64 27MB ...


    My test script is as such: #!/opt/pyenv/ncei-windclim/bin/python3

    import xarray as xr

    testdata = '/data/windclim/data.nc'
    dnc = xr.open_dataset(testdata, decode_coords="all")
    dnc = dnc.rio.write_crs("EPSG:32611")
    dnc['ws10m_mean'].rio.clip(geometries=[{"coordinates": [[[-103.623047,39.808536],[-95.932617,39.808536],[-95.932617,42.90816],[-103.623047,42.90816],[-103.623047,39.808536]]], "type": "Polygon"}], all_touched=True)


    When i run the script I get the error: rioxarray.exceptions.NoDataInBounds: No data found in bounds. Data variable: ws10m_mean. My thought are that the raster is in Mercator but the geometry has lat/lon. I'm really unsure at this point how to reproject the raster to lat/lon given it has a strange structure.

    Continue reading...

Compartilhe esta Página