Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative dimensions in merge arrays #692

Open
RichardScottOZ opened this issue Aug 19, 2023 · 10 comments
Open

Negative dimensions in merge arrays #692

RichardScottOZ opened this issue Aug 19, 2023 · 10 comments
Labels
bug Something isn't working

Comments

@RichardScottOZ
Copy link
Contributor

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
@RichardScottOZ RichardScottOZ added the bug Something isn't working label Aug 19, 2023
@Diacod-I
Copy link

Diacod-I commented Sep 4, 2023

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

@RichardScottOZ
Copy link
Contributor Author

Yes, because rioxarray. It works, my point is more that the sign handling of the transforms in code seems inconsistent.

@snowman2
Copy link
Member

snowman2 commented Nov 9, 2023

@RichardScottOZ are you able to provide a simple, reproducible example?

@RichardScottOZ
Copy link
Contributor Author

Will see if I can make one on the weekend.

@RichardScottOZ
Copy link
Contributor Author

@RichardScottOZ
Copy link
Contributor Author

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

image

@RichardScottOZ
Copy link
Contributor Author

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.

@RichardScottOZ RichardScottOZ changed the title Negative dimenions in merge arrays Negative dimensions in merge arrays Nov 11, 2023
@veenstrajelmer
Copy link

veenstrajelmer commented Apr 3, 2024

EDIT: this comment was incorrectly posted, moved to #174 (comment)

@snowman2
Copy link
Member

snowman2 commented Apr 3, 2024

@veenstrajelmer, I don't think your example is related to this discussion. I believe that it is more closely related to: #174

@veenstrajelmer
Copy link

veenstrajelmer commented Apr 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants