Skip to content

Commit 574b8b5

Browse files
authored
Merge pull request #170 from felicio93/rivermapper_tri
Rivermapper tri
2 parents aec574f + e223d56 commit 574b8b5

File tree

10 files changed

+181
-10
lines changed

10 files changed

+181
-10
lines changed

.github/workflows/functional_test.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959

6060
- name: 'Upload conda environment'
6161
if: runner.os != 'Windows'
62-
uses: actions/upload-artifact@v2
62+
uses: actions/upload-artifact@v4
6363
with:
6464
name: conda-environment-${{ matrix.python-version }}
6565
path: environment_py${{ matrix.python-version }}.yml
@@ -68,7 +68,7 @@ jobs:
6868

6969
- name: 'Upload test dem'
7070
if: runner.os != 'Windows'
71-
uses: actions/upload-artifact@v2
71+
uses: actions/upload-artifact@v4
7272
with:
7373
name: test-dem-${{ matrix.python-version }}
7474
path: /tmp/test_dem.tif
@@ -81,7 +81,7 @@ jobs:
8181

8282
- name: 'Upload Geom build results'
8383
if: runner.os != 'Windows'
84-
uses: actions/upload-artifact@v2
84+
uses: actions/upload-artifact@v4
8585
with:
8686
name: geom-build-results-${{ matrix.python-version }}
8787
path: test_shape
@@ -94,7 +94,7 @@ jobs:
9494

9595
- name: 'Upload Hfun build results'
9696
if: runner.os != 'Windows'
97-
uses: actions/upload-artifact@v2
97+
uses: actions/upload-artifact@v4
9898
with:
9999
name: hfun-build-results-${{ matrix.python-version }}
100100
path: test.2dm
@@ -107,7 +107,7 @@ jobs:
107107

108108
- name: 'Upload remesh results'
109109
if: runner.os != 'Windows'
110-
uses: actions/upload-artifact@v2
110+
uses: actions/upload-artifact@v4
111111
with:
112112
name: remesh-bydem-results-${{ matrix.python-version }}
113113
path: remeshed.2dm

.github/workflows/functional_test_2.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
6161
6262
- name: 'Upload test dem'
63-
uses: actions/upload-artifact@v2
63+
uses: actions/upload-artifact@v4
6464
with:
6565
name: test-dem-${{ matrix.python-version }}
6666
path: /tmp/test_dem.tif
@@ -71,7 +71,7 @@ jobs:
7171
run: source tests/cli/build_geom.sh
7272

7373
- name: 'Upload Geom build results'
74-
uses: actions/upload-artifact@v2
74+
uses: actions/upload-artifact@v4
7575
with:
7676
name: geom-build-results-${{ matrix.python-version }}
7777
path: test_shape
@@ -82,7 +82,7 @@ jobs:
8282
run: source tests/cli/build_hfun.sh
8383

8484
- name: 'Upload Hfun build results'
85-
uses: actions/upload-artifact@v2
85+
uses: actions/upload-artifact@v4
8686
with:
8787
name: hfun-build-results-${{ matrix.python-version }}
8888
path: test.2dm
@@ -93,7 +93,7 @@ jobs:
9393
run: source tests/cli/remesh_by_dem.sh
9494

9595
- name: 'Upload remesh results'
96-
uses: actions/upload-artifact@v2
96+
uses: actions/upload-artifact@v4
9797
with:
9898
name: remesh-bydem-results-${{ matrix.python-version }}
9999
path: remeshed.2dm

ocsmesh/utils.py

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
RectBivariateSpline, griddata)
2222
from scipy import sparse, constants
2323
from scipy.spatial import cKDTree
24+
from shapely import difference
2425
from shapely.geometry import ( # type: ignore[import]
2526
Polygon, MultiPolygon,
2627
box, GeometryCollection, Point, MultiPoint,
2728
LineString, LinearRing)
28-
from shapely.ops import polygonize, linemerge, unary_union
29+
from shapely.ops import polygonize, linemerge, unary_union, triangulate
2930
import geopandas as gpd
3031
import pandas as pd
3132
import utm
@@ -3579,3 +3580,137 @@ def batched(iterable, n):
35793580
iterator = iter(iterable)
35803581
while batch := tuple(islice(iterator, n)):
35813582
yield batch
3583+
3584+
3585+
def delaunay_within(gdf):
3586+
'''
3587+
Creates the initial delaunay triangules for
3588+
a gpd composed of polygons (only).
3589+
Selects those delaunay triangules that fall within domain.
3590+
3591+
Parameters
3592+
----------
3593+
gdf : gpd of polygons
3594+
3595+
Returns
3596+
-------
3597+
gdf : gpd of triangulated polygons
3598+
3599+
Notes
3600+
-----
3601+
3602+
'''
3603+
3604+
tt=[]
3605+
for polygon in gdf['geometry']:
3606+
try:
3607+
tri = [triangle for triangle in \
3608+
triangulate(polygon) if triangle.within(polygon)]
3609+
tt.append(MultiPolygon(tri))
3610+
except:
3611+
pass
3612+
shape_tri = gpd.GeoDataFrame(geometry=gpd.GeoSeries(tt)).set_crs(crs=4326)
3613+
shape_tri = shape_tri[~shape_tri.is_empty].dropna()
3614+
return shape_tri
3615+
3616+
3617+
def triangulate_shp(gdf):
3618+
'''
3619+
Fills out the gaps left by the delaunay_within
3620+
3621+
Parameters
3622+
----------
3623+
gdf : gpd of polygons
3624+
3625+
Returns
3626+
-------
3627+
gdf : gpd of triangulated polygons
3628+
3629+
Notes
3630+
-----
3631+
3632+
'''
3633+
3634+
shape_tri = [delaunay_within(gdf)]
3635+
shape_diff = gpd.GeoDataFrame(geometry =
3636+
gpd.GeoSeries(
3637+
gdf.difference(shape_tri[0])))
3638+
shape_diff = shape_diff[~shape_diff.is_empty].dropna()#.explode()
3639+
shape_diff_len = len(shape_diff)
3640+
while shape_diff_len>0:
3641+
shape_diff_tri = delaunay_within(shape_diff)
3642+
shape_tri.append(shape_diff_tri)
3643+
shape_diff = gpd.GeoDataFrame(geometry=
3644+
gpd.GeoSeries(difference(
3645+
shape_diff.union_all(),
3646+
shape_diff_tri.union_all()
3647+
)))
3648+
shape_diff = shape_diff[~shape_diff.is_empty].dropna().explode()
3649+
if len(shape_diff) == shape_diff_len:
3650+
break
3651+
shape_diff_len = len(shape_diff)
3652+
3653+
shape_final = gpd.GeoDataFrame(pd.concat(
3654+
shape_tri, ignore_index=True)).explode()
3655+
shape_final = shape_final[shape_final.geometry.type == 'Polygon']
3656+
shape_final.reset_index(drop=True, inplace=True)
3657+
3658+
return shape_final
3659+
3660+
3661+
def shptri_to_msht(triangulated_shp):
3662+
'''
3663+
Converts a triangulated shapefile to msh_t
3664+
3665+
Parameters
3666+
----------
3667+
triangulated_shp : triangulated gpd
3668+
3669+
Returns
3670+
-------
3671+
jigsaw_msh_t
3672+
3673+
Notes
3674+
-----
3675+
3676+
'''
3677+
3678+
coords = []
3679+
verts = []
3680+
for t in enumerate(triangulated_shp['geometry']):
3681+
if len(np.array(t[-1].boundary.xy).T) == 4:
3682+
coords.append(np.array(t[-1].boundary.xy).T[:-1])
3683+
verts.append(np.array([0,1,2])+3*t[0])
3684+
3685+
msht = msht_from_numpy(coordinates = np.vstack(coords),
3686+
triangles = verts,
3687+
)
3688+
cleanup_duplicates(msht)
3689+
cleanup_isolates(msht)
3690+
put_id_tags(msht)
3691+
3692+
return msht
3693+
3694+
3695+
def triangulate_rivermapper_poly(rm_poly):
3696+
'''
3697+
Creates triangulated mesh using the RiverMapperoutputs
3698+
3699+
Parameters
3700+
----------
3701+
rm_poly : .shp (gpd) file with the RiverMapper outputs
3702+
3703+
Returns
3704+
-------
3705+
jigsaw_msh_t
3706+
River Mesh
3707+
3708+
Notes
3709+
-----
3710+
3711+
'''
3712+
3713+
rm_poly = triangulate_shp(rm_poly)
3714+
rm_mesh = shptri_to_msht(rm_poly)
3715+
3716+
return rm_mesh

tests/api/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ def test_clean_concv(self):
116116

117117

118118
class RiverMapper(unittest.TestCase):
119+
def test_triangulate_rivermapper_poly(self):
120+
p = Path(__file__).parents[1] / "data" / "rm_poly.shp"
121+
rm_poly = gpd.read_file(p)
122+
trias = utils.triangulate_rivermapper_poly(rm_poly)
123+
124+
self.assertEqual(len(trias.tria3), 56)
125+
119126
def test_quadrangulate_rivermapper_arcs(self):
120127
p = Path(__file__).parents[1] / "data" / "diag_arcs_clipped.shp"
121128
arcs_shp = gpd.read_file(p)

tests/data/rm_poly.cpg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UTF-8

tests/data/rm_poly.dbf

306 Bytes
Binary file not shown.

tests/data/rm_poly.prj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]

tests/data/rm_poly.qmd

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
2+
<qgis version="3.36.0-Maidenhead">
3+
<identifier></identifier>
4+
<parentidentifier></parentidentifier>
5+
<language></language>
6+
<type></type>
7+
<title></title>
8+
<abstract></abstract>
9+
<links/>
10+
<dates/>
11+
<fees></fees>
12+
<encoding></encoding>
13+
<crs>
14+
<spatialrefsys nativeFormat="Wkt">
15+
<wkt></wkt>
16+
<proj4>+proj=longlat +datum=WGS84 +no_defs</proj4>
17+
<srsid>0</srsid>
18+
<srid>0</srid>
19+
<authid></authid>
20+
<description></description>
21+
<projectionacronym></projectionacronym>
22+
<ellipsoidacronym></ellipsoidacronym>
23+
<geographicflag>false</geographicflag>
24+
</spatialrefsys>
25+
</crs>
26+
<extent/>
27+
</qgis>

tests/data/rm_poly.shp

4.22 KB
Binary file not shown.

tests/data/rm_poly.shx

260 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)