From 0fbd41d94e66deaae730e2737a10a99992161e3f Mon Sep 17 00:00:00 2001 From: hboisgon Date: Tue, 23 Apr 2024 14:31:07 +0800 Subject: [PATCH 1/4] improvements to 1dmodel connection --- hydromt_wflow/wflow.py | 18 +++++++++++++++++- hydromt_wflow/workflows/connect.py | 14 +++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/hydromt_wflow/wflow.py b/hydromt_wflow/wflow.py index de8d6a07..f54f7b4c 100644 --- a/hydromt_wflow/wflow.py +++ b/hydromt_wflow/wflow.py @@ -2851,8 +2851,24 @@ def setup_1dmodel_connection( ) self.set_geoms(gdf_tributary, name=f"gauges_{mapname}") + # Add a check that all gauges are on the river + if ( + self.grid[self._MAPS["rivmsk"]].raster.sample(gdf_tributary) + == self.grid[self._MAPS["rivmsk"]].raster.nodata + ).any(): + river_upa = self.grid[self._MAPS["rivmsk"]].attrs.get("river_upa", "") + self.logger.warning( + "Not all tributary gauges are on the river network and river " + "discharge canot be saved. You should use a higher threshold " + f"for the subbasin area than {area_max} to match better the " + f"wflow river in your model {river_upa}." + ) + all_gauges_on_river = False + else: + all_gauges_on_river = True + # Update toml - if update_toml: + if update_toml and all_gauges_on_river: self.setup_config_output_timeseries( mapname=f"wflow_gauges_{mapname}", toml_output=toml_output, diff --git a/hydromt_wflow/workflows/connect.py b/hydromt_wflow/workflows/connect.py index 399d4505..1f50fe55 100644 --- a/hydromt_wflow/workflows/connect.py +++ b/hydromt_wflow/workflows/connect.py @@ -146,7 +146,9 @@ def wflow_1dmodel_connection( subids = rivmerge.index[rivmerge > rivlen_avg * 5].values subcatch_to_split = gdf_edges_subbas[gdf_edges_subbas["value"].isin(subids)] subcatch_to_split = subcatch_to_split.to_crs(ds_model.raster.crs) - da_subcatch_to_split = ds_model.raster.rasterize(subcatch_to_split) + da_subcatch_to_split = ds_model.raster.rasterize( + subcatch_to_split, col_name="value" + ).astype(np.float32) # First tributaries are the edges that are not included in the subcatch_to_split gdf_tributaries = riv1d_edges[~riv1d_edges.index.isin(subids)] @@ -184,6 +186,16 @@ def wflow_1dmodel_connection( & (flwdir.downstream(da_flwpaths) != da_flwpaths.raster.nodata), trib_msk.raster.nodata, ) + # Make sure we vectorize to single cells and not to polygons for adjacent cells + trib_msk = trib_msk.stack(z=(trib_msk.raster.y_dim, trib_msk.raster.x_dim)) + nodata = trib_msk.raster.nodata + trib_msk = trib_msk.where(trib_msk != nodata, drop=True) + trib_msk.values = np.arange(1, len(trib_msk) + 1) + trib_msk = trib_msk.unstack(fill_value=nodata) + trib_msk = trib_msk.reindex_like( + da_subcatch_to_split, fill_value=nodata + ).astype(np.int32) + gdf_trib = trib_msk.raster.vectorize() # Test if there gdf_trib is empty if gdf_trib.empty: From aa099cf647b803b9eb17463fb27abbd119798e79 Mon Sep 17 00:00:00 2001 From: hboisgon Date: Mon, 29 Apr 2024 12:19:35 +0800 Subject: [PATCH 2/4] bugfixes for connect1d method --- hydromt_wflow/wflow.py | 2 +- hydromt_wflow/workflows/connect.py | 22 +++++++++++++++++++++- tests/conftest.py | 2 +- tests/data/rivers.geojson | 12 ------------ tests/test_model_methods.py | 19 ++++++------------- 5 files changed, 29 insertions(+), 28 deletions(-) delete mode 100644 tests/data/rivers.geojson diff --git a/hydromt_wflow/wflow.py b/hydromt_wflow/wflow.py index f54f7b4c..202412e5 100644 --- a/hydromt_wflow/wflow.py +++ b/hydromt_wflow/wflow.py @@ -2747,7 +2747,7 @@ def setup_1dmodel_connection( self, river1d_fn: Union[str, Path, gpd.GeoDataFrame], connection_method: str = "subbasin_area", - area_max: float = 10.0, + area_max: float = 30.0, add_tributaries: bool = True, include_river_boundaries: bool = True, mapname: str = "1dmodel", diff --git a/hydromt_wflow/workflows/connect.py b/hydromt_wflow/workflows/connect.py index 1f50fe55..4de923e2 100644 --- a/hydromt_wflow/workflows/connect.py +++ b/hydromt_wflow/workflows/connect.py @@ -19,7 +19,7 @@ def wflow_1dmodel_connection( gdf_riv: gpd.GeoDataFrame, ds_model: xr.Dataset, connection_method: str = "subbasin_area", - area_max: float = 10.0, + area_max: float = 30.0, add_tributaries: bool = True, include_river_boundaries: bool = True, logger=logger, @@ -88,6 +88,26 @@ def wflow_1dmodel_connection( # If tributaries or subbasins area method, # need to derive the tributaries areas first if connection_method == "subbasin_area" or add_tributaries: + # 0. Check that the max_area is not too small + # Should be bigger than the wflow river threshold + # Get from attrs if available for newer wflow models built with hydromt + riv_upa = ds_model["rivmsk"].attrs.get("river_upa", None) + if riv_upa is None: + # Derive from the uparea and rivmsk + riv_upa = xr.where(ds_model["rivmsk"] > 0, ds_model["uparea"], np.nan) + riv_upa = float(riv_upa.min()) + if area_max < riv_upa: + new_area_max = np.ceil(riv_upa / 0.5) * 0.5 + logger.warning( + f"The area_max {area_max} is smaller than the minimum upstream area of " + f"the wflow river {riv_upa} which means tributaries will " + "not be connected to the wflow river. Changing and setting area_max to " + f"{new_area_max} km2. " + f"To keep {area_max} km2 threshold, please update the wflow model to " + "include more detailed rivers." + ) + area_max = new_area_max + logger.info("Linking 1D river to wflow river") # 1. Derive the river edges / boundaries # merge multilinestrings in gdf_riv to linestrings diff --git a/tests/conftest.py b/tests/conftest.py index b8bda6f4..38f0dfa4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -92,7 +92,7 @@ def floodplain1d_testdata(): @pytest.fixture() def rivers1d(): data = gpd.read_file( - join(TESTDATADIR, "rivers.geojson"), + join(EXAMPLEDIR, "data", "rivers.geojson"), ) return data diff --git a/tests/data/rivers.geojson b/tests/data/rivers.geojson deleted file mode 100644 index e03a10a5..00000000 --- a/tests/data/rivers.geojson +++ /dev/null @@ -1,12 +0,0 @@ -{ -"type": "FeatureCollection", -"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } }, -"features": [ -{ "type": "Feature", "properties": { "height": 22.5, "branchtype": "river", "branchid": "river_1", "bedlev": 865.60003662109375, "width": 60.500781333333329, "branchorder": "-1", "friction_type": "Manning", "friction_value": 0.023, "shape": "rectangle", "t_width": 20.0, "closed": "no", "ORIG_branchid": "river_1", "frictiontype": "Manning", "frictionvalue": 0.023, "frictionid": "Manning_0.023", "index": null, "diameter": null, "index_right": null, "elevtn_up": null, "elevtn_dn": null, "invlev_up": null, "invlev_dn": null, "manhole_up": null, "manhole_dn": null }, "geometry": { "type": "LineString", "coordinates": [ [ 1394694.070276216836646, 5867648.108302162960172 ], [ 1394694.070276216836646, 5867513.239712204784155 ], [ 1394601.304033889202401, 5867378.373192292638123 ], [ 1394508.537791561568156, 5867243.508742331527174 ], [ 1394415.771549233933911, 5867243.508742331527174 ], [ 1394323.005306906066835, 5867108.646362232975662 ], [ 1394230.23906457843259, 5867108.646362232975662 ], [ 1394137.472822251031175, 5867108.646362232975662 ], [ 1394044.70657992339693, 5866973.78605190385133 ], [ 1393951.940337595762685, 5866973.78605190385133 ], [ 1393859.17409526812844, 5866973.78605190385133 ], [ 1393766.407852940494195, 5866838.927811251021922 ], [ 1393673.641610612627119, 5866838.927811251021922 ], [ 1393580.875368284992874, 5866838.927811251021922 ], [ 1393488.109125957358629, 5866838.927811251021922 ], [ 1393395.342883629724383, 5866838.927811251021922 ], [ 1393302.576641302322969, 5866838.927811251021922 ], [ 1393209.810398974688724, 5866838.927811251021922 ], [ 1393117.044156646821648, 5866704.071640183217824 ], [ 1393024.277914319187403, 5866704.071640183217824 ], [ 1392931.511671991553158, 5866704.071640183217824 ], [ 1392838.745429663918912, 5866704.071640183217824 ], [ 1392745.979187336284667, 5866569.217538607306778 ], [ 1392653.212945008650422, 5866569.217538607306778 ], [ 1392560.446702681016177, 5866569.217538607306778 ], [ 1392467.680460353381932, 5866569.217538607306778 ], [ 1392374.914218025747687, 5866569.217538607306778 ], [ 1392282.147975698113441, 5866434.365506432950497 ], [ 1392189.381733370479196, 5866299.515543567948043 ], [ 1392096.615491042844951, 5866299.515543567948043 ], [ 1392003.849248715210706, 5866299.515543567948043 ], [ 1391911.083006387576461, 5866299.515543567948043 ], [ 1391818.316764059709385, 5866164.667649918235838 ], [ 1391725.55052173207514, 5866164.667649918235838 ], [ 1391632.784279404673725, 5866164.667649918235838 ], [ 1391540.01803707703948, 5866164.667649918235838 ], [ 1391447.251794749405235, 5866164.667649918235838 ], [ 1391354.48555242177099, 5866164.667649918235838 ], [ 1391261.719310093903914, 5866164.667649918235838 ], [ 1391168.953067766269669, 5866164.667649918235838 ], [ 1391076.186825438635424, 5866164.667649918235838 ], [ 1390983.420583111001179, 5866164.667649918235838 ], [ 1390890.654340783366933, 5866164.667649918235838 ], [ 1390797.888098455965519, 5866029.821825394406915 ], [ 1390705.121856128098443, 5866029.821825394406915 ], [ 1390612.355613800464198, 5866029.821825394406915 ], [ 1390519.589371472829953, 5866029.821825394406915 ], [ 1390426.823129145195708, 5866029.821825394406915 ], [ 1390334.056886817561463, 5866029.821825394406915 ], [ 1390241.290644489927217, 5866029.821825394406915 ], [ 1390148.524402162292972, 5865894.978069902397692 ], [ 1390055.758159834658727, 5865894.978069902397692 ], [ 1389962.991917507024482, 5865760.136383352801204 ], [ 1389870.225675179390237, 5865760.136383352801204 ], [ 1389777.459432851755992, 5865760.136383352801204 ], [ 1389684.693190524121746, 5865760.136383352801204 ], [ 1389591.926948196487501, 5865760.136383352801204 ], [ 1389499.160705868853256, 5865625.296765649691224 ], [ 1389406.39446354098618, 5865625.296765649691224 ], [ 1389406.39446354098618, 5865490.459216705523431 ], [ 1389313.628221213351935, 5865355.623736422508955 ], [ 1389313.628221213351935, 5865220.790324714966118 ], [ 1389313.628221213351935, 5865085.958981488831341 ], [ 1389220.86197888571769, 5864951.129706650041044 ], [ 1389128.095736558316275, 5864816.302500107325613 ], [ 1389128.095736558316275, 5864681.477361769415438 ], [ 1389035.32949423068203, 5864546.654291545972228 ], [ 1389128.095736558316275, 5864411.833289342001081 ], [ 1389128.095736558316275, 5864277.014355068095028 ], [ 1389035.32949423068203, 5864142.197488628327847 ], [ 1388942.563251903047785, 5864007.382689936086535 ], [ 1388849.797009575180709, 5863872.569958896376193 ], [ 1388757.030767247546464, 5863872.569958896376193 ], [ 1388664.264524919912219, 5863737.759295416995883 ], [ 1388571.498282592277974, 5863602.950699406675994 ], [ 1388571.498282592277974, 5863468.144170773215592 ], [ 1388571.498282592277974, 5863333.339709427207708 ], [ 1388478.732040264643729, 5863333.339709427207708 ], [ 1388385.965797937009484, 5863198.537315272726119 ], [ 1388293.199555609375238, 5863198.537315272726119 ], [ 1388200.433313281740993, 5863198.537315272726119 ], [ 1388107.667070954106748, 5863063.736988220363855 ], [ 1388014.900828626472503, 5863063.736988220363855 ], [ 1387922.134586298838258, 5863063.736988220363855 ], [ 1387829.368343971204013, 5862928.938728176057339 ], [ 1387736.602101643569767, 5862794.142535050399601 ], [ 1387736.602101643569767, 5862659.348408752121031 ], [ 1387736.602101643569767, 5862524.556349185295403 ], [ 1387643.835859315935522, 5862389.766356259584427 ], [ 1387551.069616988068447, 5862254.978429886512458 ], [ 1387458.303374660667032, 5862254.978429886512458 ], [ 1387365.537132333032787, 5862254.978429886512458 ], [ 1387272.770890005398542, 5862254.978429886512458 ], [ 1387180.004647677764297, 5862254.978429886512458 ], [ 1387087.238405350130051, 5862254.978429886512458 ], [ 1386994.472163022495806, 5862120.192569970153272 ] ] } }, -{ "type": "Feature", "properties": { "height": 13.574996948242188, "branchtype": "river", "branchid": "river_2", "bedlev": 813.29998779296875, "width": 63.595319, "branchorder": "-1", "friction_type": "Manning", "friction_value": 0.023, "shape": "rectangle", "t_width": 20.0, "closed": "no", "ORIG_branchid": "river_2", "frictiontype": "Manning", "frictionvalue": 0.023, "frictionid": "Manning_0.023", "index": null, "diameter": null, "index_right": null, "elevtn_up": null, "elevtn_dn": null, "invlev_up": null, "invlev_dn": null, "manhole_up": null, "manhole_dn": null }, "geometry": { "type": "LineString", "coordinates": [ [ 1386066.809739745920524, 5868052.726493230089545 ], [ 1386159.575982073554769, 5868052.726493230089545 ], [ 1386252.342224401189014, 5867917.851692584343255 ], [ 1386345.108466728823259, 5867917.851692584343255 ], [ 1386437.874709056690335, 5867917.851692584343255 ], [ 1386530.64095138432458, 5867917.851692584343255 ], [ 1386623.407193711958826, 5867917.851692584343255 ], [ 1386716.17343603936024, 5867782.978962258435786 ], [ 1386808.939678366994485, 5867782.978962258435786 ], [ 1386901.70592069462873, 5867782.978962258435786 ], [ 1386994.472163022495806, 5867648.108302162960172 ], [ 1387087.238405350130051, 5867648.108302162960172 ], [ 1387180.004647677764297, 5867513.239712204784155 ], [ 1387272.770890005398542, 5867378.373192292638123 ], [ 1387365.537132333032787, 5867243.508742331527174 ], [ 1387458.303374660667032, 5867108.646362232975662 ], [ 1387551.069616988068447, 5867108.646362232975662 ], [ 1387643.835859315935522, 5867108.646362232975662 ], [ 1387736.602101643569767, 5866973.78605190385133 ], [ 1387829.368343971204013, 5866973.78605190385133 ], [ 1387829.368343971204013, 5866838.927811251021922 ], [ 1387829.368343971204013, 5866704.071640183217824 ], [ 1387829.368343971204013, 5866569.217538607306778 ], [ 1387922.134586298838258, 5866434.365506432950497 ], [ 1387922.134586298838258, 5866299.515543567948043 ], [ 1388014.900828626472503, 5866164.667649918235838 ], [ 1388014.900828626472503, 5866029.821825394406915 ], [ 1388014.900828626472503, 5865894.978069902397692 ], [ 1388014.900828626472503, 5865760.136383352801204 ], [ 1387922.134586298838258, 5865625.296765649691224 ], [ 1387922.134586298838258, 5865490.459216705523431 ], [ 1387922.134586298838258, 5865355.623736422508955 ], [ 1387922.134586298838258, 5865220.790324714966118 ], [ 1387922.134586298838258, 5865085.958981488831341 ], [ 1387829.368343971204013, 5864951.129706650041044 ], [ 1387829.368343971204013, 5864816.302500107325613 ], [ 1387829.368343971204013, 5864681.477361769415438 ], [ 1387829.368343971204013, 5864546.654291545972228 ], [ 1387829.368343971204013, 5864411.833289342001081 ], [ 1387829.368343971204013, 5864277.014355068095028 ], [ 1387736.602101643569767, 5864142.197488628327847 ], [ 1387643.835859315935522, 5864007.382689936086535 ], [ 1387551.069616988068447, 5863872.569958896376193 ], [ 1387551.069616988068447, 5863737.759295416995883 ], [ 1387458.303374660667032, 5863602.950699406675994 ], [ 1387365.537132333032787, 5863468.144170773215592 ], [ 1387272.770890005398542, 5863333.339709427207708 ], [ 1387272.770890005398542, 5863198.537315272726119 ], [ 1387180.004647677764297, 5863063.736988220363855 ], [ 1387087.238405350130051, 5862928.938728176057339 ], [ 1386994.472163022495806, 5862794.142535050399601 ], [ 1386901.70592069462873, 5862794.142535050399601 ], [ 1386808.939678366994485, 5862659.348408752121031 ], [ 1386716.17343603936024, 5862524.556349185295403 ], [ 1386716.17343603936024, 5862389.766356259584427 ], [ 1386716.17343603936024, 5862254.978429886512458 ], [ 1386808.939678366994485, 5862254.978429886512458 ], [ 1386901.70592069462873, 5862120.192569970153272 ], [ 1386994.472163022495806, 5862120.192569970153272 ] ] } }, -{ "type": "Feature", "properties": { "height": 21.70001220703125, "branchtype": "river", "branchid": "river_3", "bedlev": 952.79998779296875, "width": 50.0, "branchorder": "-1", "friction_type": "Manning", "friction_value": 0.023, "shape": "rectangle", "t_width": 20.0, "closed": "no", "ORIG_branchid": "river_3", "frictiontype": "Manning", "frictionvalue": 0.023, "frictionid": "Manning_0.023", "index": null, "diameter": null, "index_right": null, "elevtn_up": null, "elevtn_dn": null, "invlev_up": null, "invlev_dn": null, "manhole_up": null, "manhole_dn": null }, "geometry": { "type": "LineString", "coordinates": [ [ 1391540.01803707703948, 5858077.577220032922924 ], [ 1391447.251794749405235, 5858077.577220032922924 ], [ 1391354.48555242177099, 5858077.577220032922924 ], [ 1391261.719310093903914, 5858077.577220032922924 ], [ 1391168.953067766269669, 5858077.577220032922924 ], [ 1391076.186825438635424, 5858212.301128903403878 ], [ 1390983.420583111001179, 5858212.301128903403878 ], [ 1390890.654340783366933, 5858212.301128903403878 ], [ 1390797.888098455965519, 5858077.577220032922924 ], [ 1390705.121856128098443, 5858077.577220032922924 ], [ 1390612.355613800464198, 5858077.577220032922924 ], [ 1390519.589371472829953, 5858077.577220032922924 ], [ 1390426.823129145195708, 5858077.577220032922924 ], [ 1390334.056886817561463, 5857942.855374777689576 ], [ 1390241.290644489927217, 5857942.855374777689576 ], [ 1390148.524402162292972, 5857942.855374777689576 ], [ 1390055.758159834658727, 5857808.135593040846288 ], [ 1389962.991917507024482, 5857808.135593040846288 ], [ 1389962.991917507024482, 5857673.41787473578006 ], [ 1389870.225675179390237, 5857673.41787473578006 ], [ 1389777.459432851755992, 5857673.41787473578006 ], [ 1389684.693190524121746, 5857808.135593040846288 ], [ 1389591.926948196487501, 5857808.135593040846288 ], [ 1389499.160705868853256, 5857808.135593040846288 ], [ 1389406.39446354098618, 5857808.135593040846288 ], [ 1389313.628221213351935, 5857942.855374777689576 ], [ 1389220.86197888571769, 5858077.577220032922924 ], [ 1389128.095736558316275, 5858212.301128903403878 ], [ 1389035.32949423068203, 5858212.301128903403878 ], [ 1388942.563251903047785, 5858347.02710147574544 ], [ 1388849.797009575180709, 5858347.02710147574544 ], [ 1388757.030767247546464, 5858347.02710147574544 ], [ 1388664.264524919912219, 5858481.755137845873833 ], [ 1388571.498282592277974, 5858481.755137845873833 ], [ 1388478.732040264643729, 5858481.755137845873833 ], [ 1388385.965797937009484, 5858481.755137845873833 ], [ 1388293.199555609375238, 5858481.755137845873833 ], [ 1388200.433313281740993, 5858616.485238100402057 ], [ 1388107.667070954106748, 5858751.217402336187661 ], [ 1388014.900828626472503, 5858885.951630645431578 ], [ 1387922.134586298838258, 5858885.951630645431578 ], [ 1387829.368343971204013, 5858885.951630645431578 ], [ 1387736.602101643569767, 5858885.951630645431578 ], [ 1387643.835859315935522, 5858885.951630645431578 ], [ 1387551.069616988068447, 5858885.951630645431578 ], [ 1387458.303374660667032, 5858885.951630645431578 ], [ 1387365.537132333032787, 5858885.951630645431578 ], [ 1387272.770890005398542, 5858885.951630645431578 ], [ 1387180.004647677764297, 5858885.951630645431578 ], [ 1387087.238405350130051, 5858885.951630645431578 ], [ 1386994.472163022495806, 5858885.951630645431578 ], [ 1386901.70592069462873, 5858885.951630645431578 ], [ 1386808.939678366994485, 5858885.951630645431578 ], [ 1386716.17343603936024, 5859020.687923114746809 ], [ 1386623.407193711958826, 5859020.687923114746809 ], [ 1386530.64095138432458, 5859020.687923114746809 ], [ 1386437.874709056690335, 5859020.687923114746809 ], [ 1386345.108466728823259, 5859020.687923114746809 ] ] } }, -{ "type": "Feature", "properties": { "height": 14.050003051757812, "branchtype": "river", "branchid": "river_4", "bedlev": 724.29998779296875, "width": 60.93007025, "branchorder": "1", "friction_type": "Manning", "friction_value": 0.023, "shape": "rectangle", "t_width": 20.0, "closed": "no", "ORIG_branchid": "river_4", "frictiontype": "Manning", "frictionvalue": 0.023, "frictionid": "Manning_0.023", "index": null, "diameter": null, "index_right": null, "elevtn_up": null, "elevtn_dn": null, "invlev_up": null, "invlev_dn": null, "manhole_up": null, "manhole_dn": null }, "geometry": { "type": "LineString", "coordinates": [ [ 1386994.472163022495806, 5862120.192569970153272 ], [ 1386901.70592069462873, 5861985.408776421099901 ], [ 1386808.939678366994485, 5861850.627049146220088 ], [ 1386716.17343603936024, 5861715.847388052381575 ], [ 1386716.17343603936024, 5861581.069793051108718 ], [ 1386716.17343603936024, 5861446.294264048337936 ], [ 1386648.271799877518788, 5861347.644815167412162 ] ] } }, -{ "type": "Feature", "properties": { "height": 14.050003051757812, "branchtype": "river", "branchid": "river_4-1", "bedlev": 724.29998779296875, "width": 60.93007025, "branchorder": "1", "friction_type": "Manning", "friction_value": 0.023, "shape": "rectangle", "t_width": 20.0, "closed": "no", "ORIG_branchid": "river_4", "frictiontype": "Manning", "frictionvalue": 0.023, "frictionid": "Manning_0.023", "index": null, "diameter": null, "index_right": null, "elevtn_up": null, "elevtn_dn": null, "invlev_up": null, "invlev_dn": null, "manhole_up": null, "manhole_dn": null }, "geometry": { "type": "LineString", "coordinates": [ [ 1386648.271799877518788, 5861347.644815167412162 ], [ 1386623.407193711958826, 5861311.520800951868296 ], [ 1386623.407193711958826, 5861176.749403670430183 ], [ 1386530.64095138432458, 5861176.749403670430183 ], [ 1386437.874709056690335, 5861041.98007211368531 ], [ 1386437.874709056690335, 5860907.212806186638772 ], [ 1386437.874709056690335, 5860772.447605801746249 ], [ 1386437.874709056690335, 5860637.684470863081515 ], [ 1386345.108466728823259, 5860502.92340128030628 ], [ 1386345.108466728823259, 5860368.164396961219609 ], [ 1386252.342224401189014, 5860233.407457816414535 ], [ 1386159.575982073554769, 5860098.652583750896156 ], [ 1386159.575982073554769, 5859963.899774673394859 ], [ 1386159.575982073554769, 5859829.149030494503677 ], [ 1386159.575982073554769, 5859694.40035112015903 ], [ 1386252.342224401189014, 5859559.653736459091306 ], [ 1386252.342224401189014, 5859424.909186420030892 ], [ 1386252.342224401189014, 5859290.166700910776854 ], [ 1386252.342224401189014, 5859155.426279840059578 ], [ 1386345.108466728823259, 5859020.687923114746809 ] ] } }, -{ "type": "Feature", "properties": { "height": 17.349960327148438, "branchtype": "river", "branchid": "river_5", "bedlev": 701.4000244140625, "width": 60.93007025, "branchorder": "-1", "friction_type": "Manning", "friction_value": 0.023, "shape": "rectangle", "t_width": 20.0, "closed": "no", "ORIG_branchid": "river_5", "frictiontype": "Manning", "frictionvalue": 0.023, "frictionid": "Manning_0.023", "index": null, "diameter": null, "index_right": null, "elevtn_up": null, "elevtn_dn": null, "invlev_up": null, "invlev_dn": null, "manhole_up": null, "manhole_dn": null }, "geometry": { "type": "LineString", "coordinates": [ [ 1386345.108466728823259, 5859020.687923114746809 ], [ 1386252.342224401189014, 5858885.951630645431578 ], [ 1386159.575982073554769, 5858751.217402336187661 ], [ 1386159.575982073554769, 5858616.485238100402057 ], [ 1386159.575982073554769, 5858481.755137845873833 ], [ 1386159.575982073554769, 5858347.02710147574544 ], [ 1386066.809739745920524, 5858212.301128903403878 ], [ 1385974.043497418286279, 5858212.301128903403878 ], [ 1385881.277255090652034, 5858212.301128903403878 ], [ 1385788.511012763017789, 5858212.301128903403878 ], [ 1385695.744770435383543, 5858212.301128903403878 ], [ 1385602.978528107749298, 5858077.577220032922924 ], [ 1385510.212285780115053, 5857942.855374777689576 ], [ 1385510.212285780115053, 5857808.135593040846288 ], [ 1385417.446043452480808, 5857673.41787473578006 ], [ 1385324.679801124846563, 5857673.41787473578006 ], [ 1385231.913558797212318, 5857538.702219764702022 ], [ 1385231.913558797212318, 5857403.988628041930497 ], [ 1385231.913558797212318, 5857269.277099471539259 ], [ 1385231.913558797212318, 5857134.567633965052664 ], [ 1385324.679801124846563, 5856999.86023142747581 ], [ 1385231.913558797212318, 5856865.154891769401729 ], [ 1385231.913558797212318, 5856730.451614898629487 ], [ 1385139.147316469578072, 5856595.750400722958148 ], [ 1385046.381074141710997, 5856595.750400722958148 ], [ 1384953.614831814309582, 5856461.051249152049422 ], [ 1384860.848589486675337, 5856461.051249152049422 ], [ 1384768.082347159041092, 5856326.354160091839731 ], [ 1384768.082347159041092, 5856191.659133453853428 ], [ 1384675.316104831406847, 5856056.966169144026935 ], [ 1384582.549862503772601, 5856056.966169144026935 ], [ 1384489.783620175905526, 5855922.275267072021961 ], [ 1384397.017377848271281, 5855787.586427145637572 ], [ 1384304.251135520637035, 5855787.586427145637572 ], [ 1384211.48489319300279, 5855652.899649272672832 ], [ 1384118.718650865368545, 5855518.21493336185813 ], [ 1384025.952408537967131, 5855383.532279322855175 ], [ 1383933.186166210100055, 5855248.851687061600387 ], [ 1383840.41992388246581, 5855248.851687061600387 ], [ 1383747.653681554831564, 5855114.1731564886868 ], [ 1383747.653681554831564, 5854979.496687510982156 ], [ 1383747.653681554831564, 5854844.822280039079487 ], [ 1383654.887439227197319, 5854844.822280039079487 ], [ 1383562.121196899563074, 5854710.149933977983892 ], [ 1383469.354954571928829, 5854710.149933977983892 ], [ 1383376.588712244294584, 5854575.479649240151048 ], [ 1383283.822469916660339, 5854440.811425729654729 ], [ 1383191.056227589026093, 5854440.811425729654729 ] ] } } -] -} diff --git a/tests/test_model_methods.py b/tests/test_model_methods.py index 8efe7e39..848ae5b5 100644 --- a/tests/test_model_methods.py +++ b/tests/test_model_methods.py @@ -624,8 +624,8 @@ def test_setup_1dmodel_connection(example_wflow_model, rivers1d): assert "subcatch_1dmodel" in example_wflow_model.geoms assert "subcatch_riv_1dmodel" in example_wflow_model.geoms - assert len(example_wflow_model.geoms["gauges_1dmodel"]) == 6 - assert len(example_wflow_model.geoms["subcatch_1dmodel"]) == 3 + assert len(example_wflow_model.geoms["gauges_1dmodel"]) == 3 + assert len(example_wflow_model.geoms["subcatch_1dmodel"]) == 2 conf_dict = { "name": "Q", "map": "gauges_1dmodel", @@ -637,7 +637,7 @@ def test_setup_1dmodel_connection(example_wflow_model, rivers1d): example_wflow_model.setup_1dmodel_connection( river1d_fn=rivers1d, connection_method="subbasin_area", - area_max=10.0, + area_max=30.0, add_tributaries=True, include_river_boundaries=False, mapname="1dmodel-nobounds", @@ -645,14 +645,8 @@ def test_setup_1dmodel_connection(example_wflow_model, rivers1d): toml_output="csv", ) - assert len(example_wflow_model.geoms["gauges_1dmodel-nobounds"]) == 3 - assert len(example_wflow_model.geoms["subcatch_1dmodel-nobounds"]) == 3 - conf_dict = { - "header": "Q", - "map": "gauges_1dmodel-nobounds", - "parameter": "lateral.river.q_av", - } - assert conf_dict in example_wflow_model.config["csv"]["column"] + assert len(example_wflow_model.geoms["gauges_1dmodel-nobounds"]) == 1 + assert len(example_wflow_model.geoms["subcatch_1dmodel-nobounds"]) == 2 assert np.all( example_wflow_model.geoms["subcatch_1dmodel"].geometry.geom_equals( example_wflow_model.geoms["subcatch_1dmodel-nobounds"].geometry @@ -663,7 +657,6 @@ def test_setup_1dmodel_connection(example_wflow_model, rivers1d): example_wflow_model.setup_1dmodel_connection( river1d_fn=rivers1d, connection_method="nodes", - area_max=10.0, add_tributaries=False, include_river_boundaries=False, mapname="1dmodel-nodes", @@ -671,7 +664,7 @@ def test_setup_1dmodel_connection(example_wflow_model, rivers1d): ) assert "gauges_1dmodel-nodes" not in example_wflow_model.geoms - assert len(example_wflow_model.geoms["subcatch_1dmodel-nodes"]) == 7 + assert len(example_wflow_model.geoms["subcatch_1dmodel-nodes"]) == 6 def test_skip_nodata_reservoir(clipped_wflow_model): From c596d4a5d2ba9f5c1175f50eab37ecf439386cdb Mon Sep 17 00:00:00 2001 From: hboisgon Date: Mon, 29 Apr 2024 12:35:48 +0800 Subject: [PATCH 3/4] bugfix linux test --- tests/conftest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 38f0dfa4..ecd21dc2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -91,8 +91,9 @@ def floodplain1d_testdata(): @pytest.fixture() def rivers1d(): + # Also for linux the data is in the normal example folder data = gpd.read_file( - join(EXAMPLEDIR, "data", "rivers.geojson"), + join(dirname(abspath(__file__)), "..", "examples", "data", "rivers.geojson"), ) return data From 6691c23aea019b411934be1abda4662031903854 Mon Sep 17 00:00:00 2001 From: hboisgon Date: Fri, 24 May 2024 11:53:51 +0800 Subject: [PATCH 4/4] Clipping of the river --- hydromt_wflow/workflows/connect.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/hydromt_wflow/workflows/connect.py b/hydromt_wflow/workflows/connect.py index e64dd1fa..71e3ea5c 100644 --- a/hydromt_wflow/workflows/connect.py +++ b/hydromt_wflow/workflows/connect.py @@ -95,6 +95,9 @@ def wflow_1dmodel_connection( gdf_riv = gdf_riv.to_crs(ds_model.raster.crs) # Derive flwdir flwdir = hydromt.flw.flwdir_from_da(ds_model["flwdir"]) + # Basin mask + basin_mask = ds_model["basins"].raster.vectorize() + basin_mask = basin_mask.buffer(-2 * max(ds_model.raster.res, key=abs)) # If tributaries or subbasins area method, # need to derive the tributaries areas first @@ -123,6 +126,8 @@ def wflow_1dmodel_connection( # 1. Derive the river edges / boundaries # merge multilinestrings in gdf_riv to linestrings riv1d = gdf_riv.explode(index_parts=True).reset_index(drop=True) + # clip to basins + riv1d = riv1d.clip(basin_mask) # get the edges of the riv1d riv1d_edges = riv1d.geometry.apply(lambda x: Point(x.coords[0])) riv1d_edges = pd.concat( @@ -297,6 +302,8 @@ def wflow_1dmodel_connection( logger.info("Deriving subbasins based on 1D river nodes snapped to wflow river") # from multiline to line gdf_riv = gdf_riv.explode(ignore_index=True, index_parts=False) + # Clip to basin + gdf_riv = gdf_riv.clip(basin_mask) nodes = [] for bi, branch in gdf_riv.iterrows(): nodes.append([Point(branch.geometry.coords[0]), bi]) # start