From 53205a11f0cb327a0a7666e4739552966a9aae32 Mon Sep 17 00:00:00 2001 From: veenstrajelmer <60435591+veenstrajelmer@users.noreply.github.com> Date: Thu, 5 Oct 2023 09:04:00 +0200 Subject: [PATCH] cleaned up meshkernel_delete_withshp and testcase (#566) --- README.md | 1 - dfm_tools/meshkernel_helpers.py | 18 ++++++++++-------- tests/shp/test.cpg | 1 - tests/shp/test.dbf | Bin 88 -> 0 bytes tests/shp/test.prj | 1 - tests/shp/test.shp | Bin 948 -> 0 bytes tests/shp/test.shx | Bin 116 -> 0 bytes tests/test_meshkernel_helpers.py | 30 ++++++++++++++++++++++++++++-- 8 files changed, 38 insertions(+), 13 deletions(-) delete mode 100644 tests/shp/test.cpg delete mode 100644 tests/shp/test.dbf delete mode 100644 tests/shp/test.prj delete mode 100644 tests/shp/test.shp delete mode 100644 tests/shp/test.shx diff --git a/README.md b/README.md index ebd08b279..3e1272e7b 100644 --- a/README.md +++ b/README.md @@ -21,4 +21,3 @@ 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) - diff --git a/dfm_tools/meshkernel_helpers.py b/dfm_tools/meshkernel_helpers.py index 4538797fe..c0a3de37c 100644 --- a/dfm_tools/meshkernel_helpers.py +++ b/dfm_tools/meshkernel_helpers.py @@ -48,8 +48,9 @@ def meshkernel_delete_withcoastlines(mk:meshkernel.meshkernel.MeshKernel, res:st coastlines_gdf = get_coastlines_gdb(bbox=bbox, res=res, min_area=min_area, crs=crs) meshkernel_delete_withgdf(mk, coastlines_gdf) - -def meshkernel_delete_withshp(mk:meshkernel.meshkernel.MeshKernel, coastlines_shp, min_area: float=0, crs:(int,str) = None): + + +def meshkernel_delete_withshp(mk:meshkernel.meshkernel.MeshKernel, coastlines_shp:str, crs:(int,str) = None): """ Delete parts of mesh that are inside the shapefile polygon. @@ -57,8 +58,10 @@ def meshkernel_delete_withshp(mk:meshkernel.meshkernel.MeshKernel, coastlines_sh ---------- mk : meshkernel.meshkernel.MeshKernel DESCRIPTION. - coastlines_shp : path to the shp file - DESCRIPTION. + coastlines_shp : str + Path to the shp file. + crs : (int,str), optional + DESCRIPTION. The default is None. Returns ------- @@ -67,16 +70,15 @@ def meshkernel_delete_withshp(mk:meshkernel.meshkernel.MeshKernel, coastlines_sh """ 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) + coastlines_gdb = gpd.read_file(coastlines_shp, 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. diff --git a/tests/shp/test.cpg b/tests/shp/test.cpg deleted file mode 100644 index 3ad133c04..000000000 --- a/tests/shp/test.cpg +++ /dev/null @@ -1 +0,0 @@ -UTF-8 \ No newline at end of file diff --git a/tests/shp/test.dbf b/tests/shp/test.dbf deleted file mode 100644 index 706d9a551684c7f4559ea496ac1045f3cd788c49..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 88 pcmZRs=3-`IU|?`$;0BVIATtFn<_BVN!MP9yuL2wx!dVJNQUIt&1qlEE diff --git a/tests/shp/test.prj b/tests/shp/test.prj deleted file mode 100644 index f45cbadf0..000000000 --- a/tests/shp/test.prj +++ /dev/null @@ -1 +0,0 @@ -GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]] \ No newline at end of file diff --git a/tests/shp/test.shp b/tests/shp/test.shp deleted file mode 100644 index bb9bc7a35828e516978672cebba03ce3ba5d63e3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 948 zcma)4eJqt>6o2ovi^cU>zO#fJrG)ec(ji^n*ItTf5-Jo!J_{*LQM3ptlSYxXcKebc zd221HX_B;+J{M_{YOeBC9b50L=8vB3`Q!JT-*e9I{LWz*i+RkPNAP@5fnh`x^Bil< z?l)k$-4UL`a8*Fr+liWZRhIJ^xL{aT1vuyTzaIq*BY^+OMMP%tpTC!;R;|0H&vK$V ziDB+(z@&i33&-?k>yBtT-8Ru>xuPoqNoWP2hf`At>ag75=FS^Cj{#2D+1y*E#c~=i zQ|x7#fKIi|O`kMbF4ONgyCNMB&gmWcpuut-mD=`a(g1t=omKX#v7A~=Y}ocBzye=! zPlR|D^L-b8nd&M@05noFxOjqm2w67}do%{{QCnHNDe+%DNPX43128dXeRnY7=y0&} z+ydC2cBJkA@pT&WIwxHL+q&*IbP%@MB=?DM1pIngQGAfWx~y~a$mr( zlS_Yj6Q9vBl9p0!J|C}zujkwyP&(}mSd#K S?I@x~3=E7KKpY1YtpET-f)Q8% diff --git a/tests/test_meshkernel_helpers.py b/tests/test_meshkernel_helpers.py index e5fe82d4c..c711b8133 100644 --- a/tests/test_meshkernel_helpers.py +++ b/tests/test_meshkernel_helpers.py @@ -12,6 +12,9 @@ import meshkernel import xarray as xr import numpy as np +import geopandas as gpd +from shapely.geometry import Polygon +import glob @pytest.mark.unittest @@ -71,8 +74,26 @@ def test_meshkernel_delete_withcoastlines(): assert len(mk.mesh2d_get().face_nodes) == 17364 + @pytest.mark.unittest def test_meshkernel_delete_withshp(): + # write shapefile from coords + file_shp = 'mk_delete_test.shp' + points_x = np.array([-68.40156631636155, -68.36143523088661, -68.28392176442131, -68.26413109213229, + -68.20915700244058, -68.1965129618115, -68.20860726154366, -68.199811407193, + -68.23059689742034, -68.23389534280184, -68.26303161033846, -68.29436684146273, + -68.27512591007063, -68.3064611411949, -68.37462901241263, -68.41146165250606, + -68.41311087519682, -68.40156631636155]) + points_y = np.array([12.301796772360385, 12.303445995051137, 12.243524237287176, 12.233628901142668, + 12.225382787688911, 12.206141856296814, 12.184152220420131, 12.115984349202414, + 12.080800931799722, 12.028025805695682, 12.033523214664854, 12.113785385614745, + 12.144021134945184, 12.20119418822456, 12.220984860513575, 12.226482269482746, + 12.286404027246707, 12.301796772360385]) + + geom = Polygon(zip(points_x, points_y)) + gdf = gpd.GeoDataFrame(geometry=[geom], crs='EPSG:4326') + gdf.to_file(file_shp) + #generate basegrid lon_min, lon_max, lat_min, lat_max = -68.45, -68.1, 12, 12.35 dxy = 0.005 @@ -81,9 +102,14 @@ def test_meshkernel_delete_withshp(): assert len(mk.mesh2d_get().face_nodes) == 20732 # remove cells with a shapefile - dfmt.meshkernel_delete_withshp(mk=mk, coastlines_shp='./shp/test.shp') + dfmt.meshkernel_delete_withshp(mk=mk, coastlines_shp=file_shp) - assert len(mk.mesh2d_get().face_nodes) == 17180 + assert len(mk.mesh2d_get().face_nodes) == 17272 + + # delete shapefile + shp_list = glob.glob(file_shp.replace('.shp','.*')) + [os.remove(x) for x in shp_list] + @pytest.mark.unittest def test_meshkernel_delete_withgdf():