Skip to content

Commit

Permalink
resolved merging conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
veenstrajelmer committed Oct 5, 2023
2 parents e0d752a + 5b22506 commit ac4691c
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ A Python package for pre- and postprocessing D-FlowFM model input and output fil
- install with ``pip install dfm_tools -U`` (or [installation guide](https://deltares.github.io/dfm_tools/installation))
- [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)

27 changes: 27 additions & 0 deletions dfm_tools/meshkernel_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,34 @@ def meshkernel_delete_withcoastlines(mk:meshkernel.meshkernel.MeshKernel, res:st

meshkernel_delete_withgdf(mk, coastlines_gdf)

def meshkernel_delete_withshp(mk:meshkernel.meshkernel.MeshKernel, coastlines_shp, min_area: float=0, crs:(int,str) = None):
"""
Delete parts of mesh that are inside the shapefile polygon.
Parameters
----------
mk : meshkernel.meshkernel.MeshKernel
DESCRIPTION.
coastlines_shp : path to the shp file
DESCRIPTION.
Returns
-------
None.
"""

mesh_bnds = mk.mesh2d_get_mesh_boundaries_as_polygons()

bbox = (mesh_bnds.x_coordinates.min(), mesh_bnds.y_coordinates.min(), mesh_bnds.x_coordinates.max(), mesh_bnds.y_coordinates.max())

coastlines_gdb = gpd.read_file(coastlines_shp, include_fields= ['polygons'], bbox=bbox)

if crs:
coastlines_gdb = coastlines_gdb.to_crs(crs)

meshkernel_delete_withgdf(mk, coastlines_gdb)

def meshkernel_delete_withgdf(mk:meshkernel.meshkernel.MeshKernel, coastlines_gdf:gpd.GeoDataFrame):
"""
Delete parts of mesh that are inside the polygons/Linestrings in a GeoDataFrame.
Expand Down
2 changes: 1 addition & 1 deletion docs/whats-new.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## UNRELEASED

### Feat

- added `dfmt.meshkernel_delete_withshp()` to delete parts of a mesh with a shapefile, while only reading the shapefile within the bounding box of the mesh by [@rqwang](https://github.com/rqwang) in [#548](https://github.com/Deltares/dfm_tools/pull/548)
- improved spatial interpolation in `dfmt.interp_regularnc_to_plipoints()` by combining linear with nearest interpolation by [@veenstrajelmer] in [#561](https://github.com/Deltares/dfm_tools/pull/561)
- added `GTSMv4.1` and `GTSMv4.1_opendap` datasets for tide interpolation with `dfmt.interpolate_tide_to_bc()` by [@veenstrajelmer] in [#544](https://github.com/Deltares/dfm_tools/pull/544)
- support for `preprocess` argument in `dfmt.open_partitioned_dataset()` by [@veenstrajelmer] in [#530](https://github.com/Deltares/dfm_tools/pull/530)
Expand Down
1 change: 1 addition & 0 deletions tests/shp/test.cpg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UTF-8
Binary file added tests/shp/test.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions tests/shp/test.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]
Binary file added tests/shp/test.shp
Binary file not shown.
Binary file added tests/shp/test.shx
Binary file not shown.
13 changes: 13 additions & 0 deletions tests/test_meshkernel_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ def test_meshkernel_delete_withcoastlines():

assert len(mk.mesh2d_get().face_nodes) == 17364

@pytest.mark.unittest
def test_meshkernel_delete_withshp():
#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 a shapefile
dfmt.meshkernel_delete_withshp(mk=mk, coastlines_shp='./shp/test.shp')

assert len(mk.mesh2d_get().face_nodes) == 17180

@pytest.mark.unittest
def test_meshkernel_delete_withgdf():
Expand Down

0 comments on commit ac4691c

Please sign in to comment.