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

Fixes related to structures #169

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions hydromt_delft3dfm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,29 +475,27 @@
"""
# Add compound structures
cmp_structures = gdf.groupby(["chainage", "branchid"])["id"].apply(list)
list_df = []
for cmp_count, cmp_st in enumerate(cmp_structures, start=1):
gdf = pd.concat(
[
gdf,
pd.DataFrame(
index=[max(gdf.index) + 1],
data={
"id": [f"CompoundStructure_{cmp_count}"],
"name": [f"CompoundStructure_{cmp_count}"],
"type": ["compound"],
"numStructures": [len(cmp_st)],
"structureIds": [";".join(cmp_st)],
},
),
],
axis=0,
)

data = {
"id": [f"CompoundStructure_{cmp_count}"],
"name": [f"CompoundStructure_{cmp_count}"],
"type": ["compound"],
"numStructures": [len(cmp_st)],
"structureIds": [";".join(cmp_st)],
}
list_df.append(pd.DataFrame(data))
if len(list_df) == 0:
gdf_cmp = gdf_cmp = pd.DataFrame()

Check warning on line 489 in hydromt_delft3dfm/utils.py

View check run for this annotation

Codecov / codecov/patch

hydromt_delft3dfm/utils.py#L489

Added line #L489 was not covered by tests
else:
gdf_cmp = pd.concat(list_df, axis=0)
# replace nan with None
gdf = gdf.replace(np.nan, None)

# Write structures
structures = StructureModel(structure=gdf.to_dict("records"))
structures = StructureModel(
structure=gdf.to_dict("records") + gdf_cmp.to_dict("records")
)

structures_fn = structures._filename() + ".ini"
structures.save(
Expand Down
5 changes: 2 additions & 3 deletions hydromt_delft3dfm/workflows/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,13 @@ def prepare_1dstructures(
]
)
# add to structures
gdf_st = gdf_st.merge(
gdf_st_crsdefs.drop(columns="geometry"), left_index=True, right_index=True
)
gdf_st = gdf_st.sjoin(gdf_st_crsdefs, how="left")

# 6. replace np.nan as None
gdf_st = gdf_st.replace(np.nan, None)

# 7. remove index and add name
gdf_st = gdf_st.reset_index(names=id_col) # force colname to index_col with names=
gdf_st = gdf_st.drop_duplicates(subset=id_col, keep="first")
gdf_st["structure_name"] = gdf_st[id_col]
return gdf_st
44 changes: 44 additions & 0 deletions tests/test_dflowfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,50 @@ def test_setup_channels(tmpdir):
)


def test_setup_bridges(tmpdir):
# Instantiate a dummy model
model = DFlowFMModel(root=join(EXAMPLEDIR, "dflowfm_local"), mode="r")
model.read()
model.set_root(tmpdir, mode="w")

# first add channels to obtain friction values for branches
# see also https://github.com/Deltares/hydromt_delft3dfm/issues/168
region = {'geom': join(TESTDATADIR, "local_data","1D_extent.geojson")}
channels_fn = join(TESTDATADIR, "local_data","1D_rivers.geojson")
crosssections_fn = join(TESTDATADIR, "local_data","1D_rivers_pointcrosssections.geojson")
model.setup_channels(
region=region, channels_fn=channels_fn,
crosssections_fn=crosssections_fn,
crosssections_type='point'
)

# setup bridges (total of 2 bridges)
bridges_fn = join(TESTDATADIR, "local_data","bridges.geojson")
model.setup_bridges(bridges_fn=bridges_fn)
assert len(model.geoms['bridges']) == 2

def test_setup_culverts(tmpdir):
# Instantiate a dummy model
model = DFlowFMModel(root=join(EXAMPLEDIR, "dflowfm_local"), mode="r")
model.read()
model.set_root(tmpdir, mode="w")

# first add channels to obtain friction values for branches
# see also https://github.com/Deltares/hydromt_delft3dfm/issues/168
region = {'geom': join(TESTDATADIR, "local_data","1D_extent.geojson")}
channels_fn = join(TESTDATADIR, "local_data","1D_rivers.geojson")
crosssections_fn = join(TESTDATADIR, "local_data","1D_rivers_pointcrosssections.geojson")
model.setup_channels(
region=region, channels_fn=channels_fn,
crosssections_fn=crosssections_fn,
crosssections_type='point'
)

# setup culverts (total of 1 culvert)
culverts_fn = join(TESTDATADIR, "local_data","culverts.geojson")
model.setup_culverts(culverts_fn=culverts_fn)
assert len(model.geoms['culverts']) == 1

def test_write_structures(tmpdir):
"""
failed before for dflowfm_local model due to nan values in gdf
Expand Down
Loading