Skip to content

Parse CARTO .mesh file

Full example code: examples/parser/carto_mesh.py

We can parse our surface mesh from the .mesh file of our map. This will give us the raw vertices and elements (triangles) that make up the mesh. Vertices contains both the coordinates as well as the scalars of the points.

from dgmr.parser import CartoReader

meshfile = "path_to_your_mesh_file"
vertices, triangles = CartoReader.load_mesh_file(meshfile)

To remove bad triangulation, we can apply a mask to the triangles variable.

mask = triangles["group_id"] == 0 # Apply default cutout OR
mask = triangles["group_id"] != -1000000 # Only remove bad triangles

# Apply the mask
triangles = triangles[mask]

print(vertices[:5])
print(triangles[:5])

Output:

        x       y        z       nx       ny       nz  ...      SCI      ICL      ACL     Force     Paso      µBi
0 -12.883   9.075   89.692  0.09460 -0.13217 -0.98670  ... -10000.0 -10000.0 -10000.0   0.00000 -10000.0 -10000.0
1 -58.444  28.414  119.256 -0.26408  0.95955  0.09756  ... -10000.0 -10000.0 -10000.0  12.01265 -10000.0 -10000.0
2 -36.364  -9.256   83.833  0.51936 -0.40373 -0.75317  ... -10000.0 -10000.0 -10000.0   0.00000 -10000.0 -10000.0
3 -22.976  26.233   97.169  0.21820  0.49316 -0.84213  ... -10000.0 -10000.0 -10000.0  11.09402 -10000.0 -10000.0
4 -77.287   2.650   89.792  0.42450  0.38718 -0.81847  ... -10000.0 -10000.0 -10000.0   0.00000 -10000.0 -10000.0

[5 rows x 20 columns]
[[ 4391  5027  4171]
[ 2211  1061 43394]
[ 1193   383 39702]
[ 4415  4361  2336]
[ 4343  4301  3122]]

If we want to visualize our mesh, we can extract the needed arrays from our dataframes.

from dgmr.visualization import SurfaceBuilder

coords = vertices[["x", "y", "z"]].values
scalars = vertices["LAT"].values
triangles = triangles[["vertex_0", "vertex_1", "vertex_2"]].values

surface = SurfaceBuilder(coords, triangles).set_scalars(scalars)