I'm trying to compare 4 different metrics of green space in 97 cities. I am able to merge the data from the 4 images for all but 9 of the cities using xr.combine_by_coords([df1, df2, df3, df4]). When I looked into why 9 failed to merge it appears that the bottom and top coordinates of the bounding box for one of the four images within each 9 cities are flipped. However, when I graph them, they all are projected correctly and line up.
For example,
creating dictionary of cities with each of the 4 metrics: d1 = {} for city in city_list: print(city) #load in greenspace metric1 and then create df m1=rxr.open_rasterio(m1_path+city+'.tif',masked=True).squeeze() m1_df=m1.to_dataset(name='m1') #load in greenspace metric2 and then create df m1=rxr.open_rasterio(m2_path+city+'.tif',masked=True).squeeze() m2_df=ga.to_dataset(name='m2') #load in greenspace metric3 and then create df m3=rxr.open_rasterio(m3_path+city+'.tif',masked=True).squeeze() m3_df=m3.to_dataset(name='m3') #load in greenspace metric4 and then create df m4=rxr.open_rasterio(m4_path+city+'.tif',masked=True).squeeze() m4_df=gba.to_dataset(name='m4') merged=xr.combine_by_coords([m1_df, m2_df, m3_df, m4_df]) d1[city]=merged
error message: ValueError: unsafe to merge dictionaries without overriding values; conflicting key 'GeoTransform'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
Input In [34] in <cell line: 2> merged=xr.combine_by_coords([m1_df, m2_df, m3_df, m4_df])
File ~/miniconda3/envs/gkm/lib/python3.10/site-packages/xarray/core/combine.py:993 in combine_by_coords return merge(
File ~/miniconda3/envs/gkm/lib/python3.10/site-packages/xarray/core/merge.py:1023 in merge merge_result = merge_core(
File ~/miniconda3/envs/gkm/lib/python3.10/site-packages/xarray/core/merge.py:757 in merge_core variables, out_indexes = merge_collected(
File ~/miniconda3/envs/gkm/lib/python3.10/site-packages/xarray/core/merge.py:312 in merge_collected merged_vars[name].attrs = merge_attrs(
File ~/miniconda3/envs/gkm/lib/python3.10/site-packages/xarray/core/merge.py:652 in merge_attrs raise MergeError(
MergeError: combine_attrs='no_conflicts', but some values are not the same. Merging {'crs_wkt': 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]', 'semi_major_axis': 6378137.0, 'semi_minor_axis': 6356752.314245179, 'inverse_flattening': 298.257223563, 'reference_ellipsoid_name': 'WGS 84', 'longitude_of_prime_meridian': 0.0, 'prime_meridian_name': 'Greenwich', 'geographic_crs_name': 'WGS 84', 'grid_mapping_name': 'latitude_longitude', 'spatial_ref': 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]', 'GeoTransform': '126.57262353244059 0.0008983152841195215 0.0 37.76427622910057 0.0 -0.0008983152841195215'} with {'crs_wkt': 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]', 'semi_major_axis': 6378137.0, 'semi_minor_axis': 6356752.314245179, 'inverse_flattening': 298.257223563, 'reference_ellipsoid_name': 'WGS 84', 'longitude_of_prime_meridian': 0.0, 'prime_meridian_name': 'Greenwich', 'geographic_crs_name': 'WGS 84', 'grid_mapping_name': 'latitude_longitude', 'spatial_ref': 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]', 'GeoTransform': '126.57262353244059 0.0008983152841195215 0.0 37.049217262941426 0.0 0.0008983152841195215'}
printing the resolution and bounds for the images that conflict I get:
#image 1 The spatial resolution for your data is: (0.0008983152841195215, -0.0008983152841195215) The bounding box is: (126.57262353244059, 37.049217262941426, 127.25534314837144, 37.76427622910057)
#image 2 The spatial resolution for your data is: (0.0008983152841195215, 0.0008983152841195215) The bounding box is: (126.57262353244059, 37.76427622910057, 127.25534314837144, 37.049217262941426)