import matplotlib.pyplot as plt
import mikeioMesh
Read mesh file
A mesh file holds the mesh topology — a set of Nodes and Elements — with no dynamic data. Load it with mikeio.Mesh:
msh = mikeio.Mesh("../data/odense_rough.mesh")
msh<Mesh>
number of nodes: 399
number of elements: 654
projection: UTM-33
Plot mesh
Visualize the mesh structure and boundary nodes:
msh.plot()
msh.plot.boundary_nodes(boundary_names=['Land','Open boundary']);
Convert mesh to shapely
Convert the mesh to a shapely MultiPolygon (requires shapely):
mp = msh.to_shapely()
mpNow a lot of methods are available, e.g. computing the area and extents:
mp.area68931409.58160606
mp.bounds(211068.501175313, 6153077.66681803, 224171.617336507, 6164499.42751662)
Create a buffered copy of the domain, and extract the coastal zone (the ring within 500 m of the boundary):
domain = mp.buffer(0)
domainopen_water = domain.buffer(-500)
coastalzone = domain - open_water
coastalzoneCheck if points are inside the domain
Test individual points against the shapely polygon:
from shapely.geometry import Point
p1 = Point(216000, 6162000)
p2 = Point(220000, 6156000)
print(mp.contains(p1))
print(mp.contains(p2))True
False
The same check is available directly on the mesh geometry:
p1p2 = [[216000, 6162000], [220000, 6156000]]
msh.geometry.contains(p1p2)array([ True, False])
ax = msh.plot()
ax.scatter(p1.x, p1.y, marker="*", s=200, c="red", label="inside")
ax.scatter(p2.x, p2.y, marker="+", s=200, c="green", label="outside")
ax.legend();
Spatial queries
Find element containing a point
Use find_index() to find the element that contains a given point:
g = msh.geometry
idx = g.find_index(coords=[216000, 6162000])
idxarray([239])
Find nearest elements
Use find_nearest_elements() to find the closest element centers:
g.find_nearest_elements([216000, 6162000], return_distances=True)(np.int64(239), 108.30930897320103)
g.find_nearest_elements([216000, 6162000], n_nearest=3)array([239, 377, 70])
Boundary polygons
Extract the mesh boundary as polygons, e.g. to inspect the exterior ring:
bnd = g.boundary_polygons
exterior = bnd.exteriors[0]
exterior.xy.shape(129, 2)
Change z values and boundary code
The mesh nodes carry z coordinates that can be edited in place. Here we enforce a minimum depth of 2 meters:
g = msh.geometry
print(f'max z before: {g.node_coordinates[:,2].max()}')
zc = g.node_coordinates[:,2]
zc[zc>-2] = -2
g.node_coordinates[:,2] = zc
print(f'max z after: {g.node_coordinates[:,2].max()}')max z before: -0.200000002980232
max z after: -2.0
Also change the open boundary (code 2) to a land boundary (code 1):
c = g.codes
c[c==2] = 1
g.codes = cSave the modified geometry to a new mesh file
Write the edited geometry to a new file with to_mesh():
g.to_mesh("new_mesh.mesh")