corteva / rioxarray

geospatial xarray extension powered by rasterio
https://corteva.github.io/rioxarray
Other
507 stars 80 forks source link

Invalid bounds returned by `.rio.bounds()` #645

Closed vincentsarago closed 1 year ago

vincentsarago commented 1 year ago

👋 I've looked at all issues mentioning bounds but I don't think I found one similar to the one I have (I'm sorry if there is)

When using ds.rio.bounds() for this dataset s3://power-analysis-ready-datastore/power_901_annual_meteorology_utc.zarr, I'm getting values that exceed the values in the array itself 👇

ds = xarray.open_dataset("s3://power-analysis-ready-datastore/power_901_annual_meteorology_utc.zarr", engine="zarr", decode_coords="all") 

ds.rio._obj.coords
>> 
Coordinates:
  * lat      (lat) float64 -90.0 -89.5 -89.0 -88.5 -88.0 ... 88.5 89.0 89.5 90.0
  * lon      (lon) float64 -180.0 -179.4 -178.8 -178.1 ... 178.1 178.8 179.4
  * time     (time) datetime64[ns] 1981-12-31 1982-12-31 ... 2021-12-31

# Print XMIN, YMIN, XMAX, YMAX
bounds = list(map(float, [ds["lon"].min(), ds["lat"].min(), ds["lon"].max(), ds["lat"].max()]))
print(bounds)
>> [-180.0, -90.0, 179.375, 90.0]

# Print Bounds from rioxarray
print(ds.rio.bounds())
>> (-180.3125, -90.25, 179.6875, 90.25)

print(ds.rio.bounds(recalc=True))
>> (-180.3125, -90.25, 179.6875, 90.25)

print(ds.rio.transform())
>> Affine(0.625, 0.0, -180.3125, 0.0, 0.5, -90.25)

print(ds.rio._internal_bounds())
>> (-180.0, 90.0, 179.375, -90.0)

print(ds.rio._unordered_bounds())
>> (-180.3125, 90.25, 179.6875, -90.25)

print(ds.rio.resolution())
>> (0.625, 0.5)

Environment Information

python -c "import rioxarray; rioxarray.show_versions()"
rioxarray (0.12.2) deps:
  rasterio: 1.4.dev0
    xarray: 2022.6.0
      GDAL: 3.5.0
      GEOS: 3.10.3
      PROJ: 9.0.0
 PROJ DATA: /opt/homebrew/share/proj
 GDAL DATA: /opt/homebrew/share/gdal

Other python deps:
     scipy: 1.9.1
    pyproj: 3.4.0

System:
    python: 3.9.13 (main, May 24 2022, 21:13:51)  [Clang 13.1.6 (clang-1316.0.21.2)]
executable: /Users/vincentsarago/Dev/venv/py39/bin/python
   machine: macOS-13.2-arm64-arm-64bit
snowman2 commented 1 year ago

Welcome!

The coordinates of the grid cell represent the centroid of the grid cell. To get the boundary of the raster, you have to take into account the resolution of the grid cell and add/subtract from the bounding centroid coordinates.

vincentsarago commented 1 year ago

@snowman2 so I'm right to assume that the coordinates from the zarr file are incorrect because they represent UpperLeff corner of the grid instead of the centroid?

It's unlikely that a user will want the bounds of it's data to overflow the valid bounds for epsg:4326 (-180.0, 90.0, 179.375, -90.0)

snowman2 commented 1 year ago

I'm right to assume that the coordinates from the zarr file are incorrect because they represent UpperLeff corner of the grid instead of the centroid?

I have only encountered files with the coordinates representing the centroid. It is likely incorrect or at least non-standard

vincentsarago commented 1 year ago

thank you @snowman2 🙏