rioxarray
rioxarray copied to clipboard
Negative dimensions in merge arrays
auscan = merge_arrays(dataarrays = [aus, can], res = can.rio.resolution(), crs="EPSG:4326")
Problem description
Looking at the merge code there are some abs() checks in there - but are you supposed to be able to do the above? There is a da_res assigned with absolute resolutions and a res check used later.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[128], line 8
6 can = rioxarray.open_rasterio(gravdir + "\\" + 'GeophysicsGravity_HGM_USCanada.tif')
7 #auscan = merge_arrays(dataarrays = [aus, can], res = (can.rio.resolution()[0],can.rio.resolution()[1]), crs="EPSG:4326")
----> 8 auscan = merge_arrays(dataarrays = [aus, can], res = can.rio.resolution(), crs="EPSG:4326")
File ~\AppData\Local\miniconda3\envs\pangeo\lib\site-packages\rioxarray\merge.py:174, in merge_arrays(dataarrays, bounds, res, nodata, precision, method, crs, parse_coordinates)
171 rioduckarrays.append(RasterioDatasetDuck(dataarray))
173 # use rasterio to merge
--> 174 merged_data, merged_transform = _rio_merge(
175 rioduckarrays,
176 **{key: val for key, val in input_kwargs.items() if val is not None},
177 )
178 # generate merged data array
179 representative_array = rioduckarrays[0]._xds
File ~\AppData\Local\miniconda3\envs\pangeo\lib\site-packages\rasterio\merge.py:293, in merge(datasets, bounds, res, nodata, dtype, precision, indexes, output_count, resampling, method, target_aligned_pixels, dst_path, dst_kwds)
290 out_profile["nodata"] = nodata
292 # create destination array
--> 293 dest = np.zeros((output_count, output_height, output_width), dtype=dt)
295 if nodata is not None:
296 nodataval = nodata
ValueError: negative dimensions are not allowed
Expected Output
Environment Information
rioxarray (0.14.0) deps:
rasterio: 1.3.6
xarray: 2023.3.0
GDAL: 3.6.3
GEOS: 3.11.2
PROJ: 9.1.1
PROJ DATA: C:\Users\rscott\AppData\Local\miniconda3\envs\pangeo\Library\share\proj
GDAL DATA: C:\Users\rscott\AppData\Local\miniconda3\envs\pangeo\Library\share\gdal
Other python deps:
scipy: 1.10.1
pyproj: 3.5.0
System:
python: 3.9.16 | packaged by conda-forge | (main, Feb 1 2023, 21:28:38) [MSC v.1929 64 bit (AMD64)]
executable: C:\Users\rscott\AppData\Local\miniconda3\envs\pangeo\python.exe
machine: Windows-10-10.0.22621-SP0
This might be due to a difference in the dimensions of aus and can arrays. Can you also confirm whether the two are xarray.DataArray objects? Cuz the merge_arrays function only takes in a list of xarray.DataArray objects. If they are, then review their metadata (the CRS, datatypes, and dimensions) and check if they both match. merge_arrays() merge only xarray.DataArray arrays that have the same dimensions and metadata. Other than that, it could be the affine transform's Y coefficient (which is the 4th coefficient I think, the 4th value in the affine transform list) being negative.
Refer to the below link for more details.
https://gis.stackexchange.com/questions/414127/valueerror-negative-dimensions-are-not-allowed-when-using-raster-created-from
Yes, because rioxarray. It works, my point is more that the sign handling of the transforms in code seems inconsistent.
@RichardScottOZ are you able to provide a simple, reproducible example?
Will see if I can make one on the weekend.
Alan, the above is from this, basically:
https://github.com/RichardScottOZ/AU-US-CAN-GEOPHYSICS/blob/main/RIOXARRAY-RES-TEST.ipynb
Note the first version using reproj resolution (e.g. can resolution above) - which equals (0.31, -0.31) fails with the error above
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[19], line 8
5 usgs['GeophysicsGravity_HGM_USCanada.tif'].name = 'uscan'
7 #auscan = xr.concat([reproj, usgs['GeophysicsGravity_HGM_USCanada.tif']],dim='band')
----> 8 auscan = merge_arrays(dataarrays = [reproj, usgs['GeophysicsGravity_HGM_USCanada.tif']], res = reproj.rio.resolution(), crs="EPSG:4326")
10 auscan
File ~\AppData\Local\miniconda3\envs\pangeo\lib\site-packages\rioxarray\merge.py:174, in merge_arrays(dataarrays, bounds, res, nodata, precision, method, crs, parse_coordinates)
171 rioduckarrays.append(RasterioDatasetDuck(dataarray))
173 # use rasterio to merge
--> 174 merged_data, merged_transform = _rio_merge(
175 rioduckarrays,
176 **{key: val for key, val in input_kwargs.items() if val is not None},
177 )
178 # generate merged data array
179 representative_array = rioduckarrays[0]._xds
File ~\AppData\Local\miniconda3\envs\pangeo\lib\site-packages\rasterio\merge.py:293, in merge(datasets, bounds, res, nodata, dtype, precision, indexes, output_count, resampling, method, target_aligned_pixels, dst_path, dst_kwds)
290 out_profile["nodata"] = nodata
292 # create destination array
--> 293 dest = np.zeros((output_count, output_height, output_width), dtype=dt)
295 if nodata is not None:
296 nodataval = nodata
ValueError: negative dimensions are not allowed
but giving it (0.31, 0.31) succeeds
I can chop out a tiny uploadable piece of this that will do the same if you want. Haven't got as far as tinkering with rioxarray to throw in some print debugs for running the above to see but I remember some abs() call branches in there.
EDIT: this comment was incorrectly posted, moved to https://github.com/corteva/rioxarray/issues/174#issuecomment-2034813554
@veenstrajelmer, I don't think your example is related to this discussion. I believe that it is more closely related to: https://github.com/corteva/rioxarray/issues/174
This is odd, that was the issue where I thought I commented, but I guess I clicked a different link in the meantime. I moved my comment to that discussion. Sorry for the confusion.