import matplotlib.pyplot as plt
from matplotlib_inline.backend_inline import set_matplotlib_formats
'png')
set_matplotlib_formats("figure.figsize"] = (6,6)
plt.rcParams[
import mikeio
Mesh
- read mesh file
- plot mesh
- convert to shapely
- check if point is inside or outside mesh
- subset mesh, plot subset
- change z values
- change boundary codes
= r"../tests/testdata/odense_rough.mesh"
meshfilename = mikeio.Mesh(meshfilename)
msh msh
Flexible Mesh
Number of elements: 654
Number of nodes: 399
Projection: UTM-33
msh.plot()=['Land','Open boundary']); msh.plot.boundary_nodes(boundary_names
Convert mesh to shapely
Convert mesh to shapely MultiPolygon object, requires that the shapely
library is installed.
= msh.to_shapely()
mp mp
Now a lot of methods are available
mp.area
68931409.58160606
mp.bounds
(211068.501175313, 6153077.66681803, 224171.617336507, 6164499.42751662)
= mp.buffer(0)
domain domain
= domain.buffer(-500)
open_water
= domain - open_water
coastalzone coastalzone
Find if points are inside the domain
from shapely.geometry import Point
= Point(216000, 6162000)
p1 = Point(220000, 6156000)
p2 print(mp.contains(p1))
print(mp.contains(p2))
True
False
Mesh class can also check if a mesh contains points
= [[216000, 6162000], [220000, 6156000]]
p1p2 msh.contains(p1p2)
array([ True, False])
= msh.plot()
ax ="*", s=200, c="red", label="inside")
ax.scatter(p1.x, p1.y, marker="+", s=200, c="green", label="outside")
ax.scatter(p2.x, p2.y, marker; ax.legend()
Subset mesh
Select only elements with more than 3m depth. Plot these elements.
= msh.element_coordinates[:,2] zc
= msh.element_ids[zc<-3] elem_ids
# TODO: not yet supported
# msh.plot(elements=elem_ids);
Change z values and boundary code
Assume that we want to have a minimum depth of 2 meters and change the open boundary (code 2) to a closed one (code 1).
print(f'max z before: {msh.node_coordinates[:,2].max()}')
= msh.node_coordinates[:,2]
zc >-2] = -2
zc[zc= zc
msh.zn print(f'max z after: {msh.node_coordinates[:,2].max()}')
max z before: -0.200000002980232
max z after: -2.0
print(f'valid codes before: {msh.valid_codes}')
= msh.codes
c ==2] = 1
c[c= c
msh.codes print(f'valid codes after: {msh.valid_codes}')
valid codes before: [0, 1, 2]
valid codes after: [0, 1]
; msh.plot()