From 2a4a3e14e1417a48926275edacba35361200c212 Mon Sep 17 00:00:00 2001 From: Bas des Tombe Date: Wed, 6 Dec 2023 14:04:07 +0100 Subject: [PATCH 1/5] gdf_to_da should return array with fill_value if shape is outside of grid --- nlmod/dims/grid.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/nlmod/dims/grid.py b/nlmod/dims/grid.py index 13bc8715..9e7ea73c 100644 --- a/nlmod/dims/grid.py +++ b/nlmod/dims/grid.py @@ -1201,7 +1201,13 @@ def gdf_to_da( da : xarray DataArray The DataArray with the projected vector data. """ + da = util.get_da_from_da_ds(ds, dims=ds.top.dims, data=fill_value) + gdf_cellid = gdf_to_grid(gdf, ds, ix=ix) + + if gdf_cellid.size == 0: + return da + if min_total_overlap > 0: gdf_cellid["area"] = gdf_cellid.area area_sum = gdf_cellid[["cellid", "area"]].groupby("cellid").sum() @@ -1232,8 +1238,6 @@ def gdf_to_da( else: gdf_agg.set_index(gdf_cellid.cellid.values, inplace=True) - da = util.get_da_from_da_ds(ds, dims=ds.top.dims, data=fill_value) - if ds.gridtype == "structured": ixs, iys = zip(*gdf_agg.index.values) da.values[ixs, iys] = gdf_agg[column] @@ -1654,7 +1658,15 @@ def gdf_to_grid( elif shp[geometry].geom_type in ["Polygon", "MultiPolygon"]: shpn["area"] = r["areas"][i] shps.append(shpn) - gdfg = gpd.GeoDataFrame(shps, geometry=geometry, crs=gdf.crs) + + if len(shps) == 0: + # Unable to determine the column names, because no geometries intersect with the grid + logger.info("No geometries intersect with the grid") + columns = gdf.columns.to_list() + ["area", "length", "cellid"] + else: + columns = None # adopt from shps + + gdfg = gpd.GeoDataFrame(shps, columns=columns, geometry=geometry, crs=gdf.crs) gdfg.index.name = gdf.index.name return gdfg From 7488df275c49d4c4651538cdfdebfca82cf2f928 Mon Sep 17 00:00:00 2001 From: Bas des Tombe Date: Wed, 6 Dec 2023 15:42:09 +0100 Subject: [PATCH 2/5] cache: check length of gridintersect props --- nlmod/cache.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/nlmod/cache.py b/nlmod/cache.py index 6763f1a3..ed91886f 100644 --- a/nlmod/cache.py +++ b/nlmod/cache.py @@ -438,7 +438,9 @@ def _same_function_arguments(func_args_dic, func_args_dic_cache): mfgrid1 = {k: v for k, v in item.mfgrid.__dict__.items() if k not in excl} mfgrid2 = {k: v for k, v in i2.mfgrid.__dict__.items() if k not in excl} - if not is_method_equal or mfgrid1.keys() != mfgrid2.keys(): + is_same_length_props = all([np.all(np.size(v) == np.size(mfgrid2[k])) for k, v in mfgrid1.items()]) + + if not is_method_equal or mfgrid1.keys() != mfgrid2.keys() or not is_same_length_props: logger.info( "cache was created using different gridintersect, do not use cached data" ) @@ -446,6 +448,13 @@ def _same_function_arguments(func_args_dic, func_args_dic_cache): is_other_props_equal = all([np.all(v == mfgrid2[k]) for k, v in mfgrid1.items()]) + for k, v in mfgrid1.items(): + print(k, np.size(v)) + try: + np.all(v == mfgrid2[k]) + except: + print(k, v, mfgrid2[k]) + if not is_other_props_equal: logger.info( "cache was created using different gridintersect, do not use cached data" From 14b0f20880e32ff171521ecc25bf8b68dbcee191 Mon Sep 17 00:00:00 2001 From: Bas des Tombe Date: Wed, 6 Dec 2023 15:43:28 +0100 Subject: [PATCH 3/5] Removed some accidentally committed verbose print statements --- nlmod/cache.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/nlmod/cache.py b/nlmod/cache.py index ed91886f..0bdbb8d1 100644 --- a/nlmod/cache.py +++ b/nlmod/cache.py @@ -448,13 +448,6 @@ def _same_function_arguments(func_args_dic, func_args_dic_cache): is_other_props_equal = all([np.all(v == mfgrid2[k]) for k, v in mfgrid1.items()]) - for k, v in mfgrid1.items(): - print(k, np.size(v)) - try: - np.all(v == mfgrid2[k]) - except: - print(k, v, mfgrid2[k]) - if not is_other_props_equal: logger.info( "cache was created using different gridintersect, do not use cached data" From a1569e56508b6b912a54fb224fb2c3bca96bba27 Mon Sep 17 00:00:00 2001 From: Bas des Tombe Date: Thu, 7 Dec 2023 17:11:25 +0100 Subject: [PATCH 4/5] Pleasing Codacy --- nlmod/cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nlmod/cache.py b/nlmod/cache.py index 0bdbb8d1..bb5626ac 100644 --- a/nlmod/cache.py +++ b/nlmod/cache.py @@ -446,7 +446,7 @@ def _same_function_arguments(func_args_dic, func_args_dic_cache): ) return False - is_other_props_equal = all([np.all(v == mfgrid2[k]) for k, v in mfgrid1.items()]) + is_other_props_equal = all(np.all(v == mfgrid2[k]) for k, v in mfgrid1.items()) if not is_other_props_equal: logger.info( From 0b1fcf5187f33b9da719ad5999a0ce16923d5b3e Mon Sep 17 00:00:00 2001 From: Bas des Tombe Date: Thu, 7 Dec 2023 17:12:56 +0100 Subject: [PATCH 5/5] Please Codacy --- nlmod/cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nlmod/cache.py b/nlmod/cache.py index bb5626ac..91faa5f3 100644 --- a/nlmod/cache.py +++ b/nlmod/cache.py @@ -438,7 +438,7 @@ def _same_function_arguments(func_args_dic, func_args_dic_cache): mfgrid1 = {k: v for k, v in item.mfgrid.__dict__.items() if k not in excl} mfgrid2 = {k: v for k, v in i2.mfgrid.__dict__.items() if k not in excl} - is_same_length_props = all([np.all(np.size(v) == np.size(mfgrid2[k])) for k, v in mfgrid1.items()]) + is_same_length_props = all(np.all(np.size(v) == np.size(mfgrid2[k])) for k, v in mfgrid1.items()) if not is_method_equal or mfgrid1.keys() != mfgrid2.keys() or not is_same_length_props: logger.info(