Skip to content

Commit 3f1dc5a

Browse files
committed
[EJP] OCSMesh function add
1 parent ea6bc10 commit 3f1dc5a

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

bluemath_tk/topo_bathy/mesh_utils.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
import matplotlib.pyplot as plt
1010
import numpy as np
1111
import ocsmesh
12+
import pandas as pd
1213
import rasterio
1314
from jigsawpy.msh_t import jigsaw_msh_t
1415
from matplotlib.axes import Axes
1516
from netCDF4 import Dataset
1617
from pyproj.enums import TransformDirection
1718
from rasterio.mask import mask
18-
from shapely.geometry import Polygon, mapping
19+
from shapely.geometry import LineString, MultiLineString, Polygon, mapping
1920
from shapely.ops import transform
2021

2122
from ..core.geo import buffer_area_for_polygon
@@ -1084,3 +1085,33 @@ def define_mesh_target_size(
10841085
simpl_UTM = 100.0 # Simplification tolerance in meters
10851086
simplified_shape = simply_polygon(base_shape, simpl_UTM, project)
10861087
print(simplified_shape)
1088+
1089+
1090+
def read_lines(poly_line: str) -> MultiLineString:
1091+
"""
1092+
Reads a CSV file containing coordinates of a polyline and returns a MultiLineString.
1093+
The CSV file should have two columns for x and y coordinates, with NaN values indicating breaks in the line.
1094+
Parameters
1095+
----------
1096+
poly_line : str
1097+
Path to the CSV file containing the polyline coordinates
1098+
Returns
1099+
-------
1100+
MultiLineString
1101+
A MultiLineString object representing the polyline segments
1102+
"""
1103+
1104+
coords_line = pd.read_csv(poly_line, sep=",", header=None)
1105+
segments = []
1106+
current_segment = []
1107+
for index, row in coords_line.iterrows():
1108+
if row.isna().any():
1109+
if current_segment:
1110+
segments.append(LineString(current_segment))
1111+
current_segment = []
1112+
else:
1113+
current_segment.append(tuple(row))
1114+
1115+
if current_segment:
1116+
segments.append(LineString(current_segment))
1117+
return MultiLineString(segments)

0 commit comments

Comments
 (0)