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

Add dfmt.meshkernel_delete_withshp() function #548

Merged
merged 5 commits into from
Oct 5, 2023

Conversation

veenstrajelmer
Copy link
Collaborator

@veenstrajelmer veenstrajelmer commented Sep 29, 2023

No description provided.

Copy link
Collaborator Author

@veenstrajelmer veenstrajelmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rqwang, I noticed your fork when I was coincidentally browsing trough the dfm_tools forks. Your commits can be a nice contribution. If you clean it up a bit by following my review comments, I would like to merge it.

README.md Outdated
@@ -20,3 +20,6 @@ A Python package for pre- and postprocessing D-FlowFM model input and output fil
- use python>=3.9 to ensure future updates: https://github.com/Deltares/dfm_tools/issues/267
- [online documentation](https://deltares.github.io/dfm_tools) with installation guide, contributing guide, tutorials/examples, API reference and a convenient search box.
- Bug or feature request? Create a [GitHub issue](https://github.com/Deltares/dfm_tools/issues)
- This fork has a new feature: delete the mesh with shapefiles. Use it in this way:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this, but add a line to docs/whats-new.md

Comment on lines 99 to 100
if coastlines_shp == None:
print("Need a shape file.")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a situation that is expected, since the coastlines_shp argument is not optional. So this can be removed.

Comment on lines 108 to 116
for coastline_geom in coastlines_gdb['geometry']:
xx, yy = coastline_geom.exterior.coords.xy
xx = np.array(xx)
yy = np.array(yy)

delete_pol_geom = meshkernel.GeometryList(x_coordinates=xx, y_coordinates=yy) #TODO: .copy()/to_numpy() makes the array contiguous in memory, which is necessary for meshkernel.mesh2d_delete()
mk.mesh2d_delete(geometry_list=delete_pol_geom,
delete_option=meshkernel.DeleteMeshOption(2), #ALL_COMPLETE_FACES/2: Delete all faces of which the complete face is inside the polygon
invert_deletion=False)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be replaced with meshkernel_delete_withgdf() to avoid duplicate code. This would also align it with meshkernel_delete_withcoastlines(). Could you also move your function a bit up in the script, below the similar meshkernel_delete_withcoastlines() function would make more sense.

None.

---------------
Contributed by Roger Wang (rq.wang@rutgers.edu)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is fine if you like this line here, but credits are also in docs/whats-new.md

-------
None.

---------------
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these dashes, since they might mess up formatting in the docs

@@ -77,6 +77,43 @@ def meshkernel_delete_withgdf(mk:meshkernel.meshkernel.MeshKernel, coastlines_gd
delete_option=meshkernel.DeleteMeshOption(2), #ALL_COMPLETE_FACES/2: Delete all faces of which the complete face is inside the polygon
invert_deletion=False)

def meshkernel_delete_withshp(mk:meshkernel.meshkernel.MeshKernel, coastlines_shp, min_area: float=0):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could consider adding a crs argument to convert the geodataframe to, in case of models/grids generated in local/cartesian coordinates instead of WGS84. An example can be found in a different function:

if crs:
coastlines_gdb = coastlines_gdb.to_crs(crs)

Copy link
Collaborator Author

@veenstrajelmer veenstrajelmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, could you maybe also add a testcase? An example can be found here:

@pytest.mark.unittest
def test_meshkernel_delete_withcoastlines():
#generate basegrid
lon_min, lon_max, lat_min, lat_max = -68.45, -68.1, 12, 12.35
dxy = 0.005
mk = dfmt.make_basegrid(lon_min, lon_max, lat_min, lat_max, dx=dxy, dy=dxy)
assert len(mk.mesh2d_get().face_nodes) == 20732
# remove cells with GSHHS coastlines
dfmt.meshkernel_delete_withcoastlines(mk=mk, res='h')
assert len(mk.mesh2d_get().face_nodes) == 17364

The only thing to add is a shapefile, let me know if this is not feasible and I will add that later.

@sonarcloud
Copy link

sonarcloud bot commented Oct 3, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@rqwang
Copy link
Contributor

rqwang commented Oct 3, 2023

Thanks for your comments. I'm not sure how to update what's-new.md but I believe all other comments are addressed. Happy to contribute to this great work!

@veenstrajelmer veenstrajelmer merged commit ac4691c into Deltares:main Oct 5, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants