Skip to content

Commit c51670b

Browse files
EliEli
authored andcommitted
Added yaml-root to convert_polygons.
1 parent a403b52 commit c51670b

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

schimpy/convert_polygons.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,29 @@
2020
type=click.Path(),
2121
help="Output file (YAML or Shapefile).",
2222
)
23+
@click.option(
24+
"--yaml-root",
25+
default=None,
26+
type=str,
27+
help="Dot-separated path to the parent of the polygons key in a nested YAML file, "
28+
"e.g. --yaml-root=mesh.depth_enforcement. Only used with YAML input.",
29+
)
2330
@click.help_option("-h", "--help")
24-
def convert_polygons_cli(input, output):
31+
def convert_polygons_cli(input, output, yaml_root):
2532
"""CLI wrapper for converting polygon files."""
26-
convert_polys(input, output)
33+
convert_polys(input, output, yaml_root=yaml_root)
2734

2835

29-
def convert_polys(input, output):
36+
def convert_polys(input, output, yaml_root=None):
3037
if input.endswith(".yaml"):
31-
polygons = read_polygons(input)
38+
polygons = read_polygons(input, yaml_root=yaml_root)
3239
if output.endswith(".shp"):
3340
write_polygons(output, polygons)
3441
else:
3542
raise ValueError("Not supported output file type")
3643
elif input.endswith(".shp"):
44+
if yaml_root is not None:
45+
raise ValueError("--yaml-root is only supported with YAML input files.")
3746
polygons = read_polygons(input)
3847
write_polygons(output, polygons)
3948
else:

schimpy/schism_polygon.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,17 @@ def read(self, data):
156156
class SchismPolygonYamlReader(SchismPolygonIo):
157157
"""Read polygons from a SCHISM YAML polygon file"""
158158

159-
def read(self, fpath):
159+
def read(self, fpath, yaml_root=None):
160160
if os.path.exists(fpath):
161161
with open(fpath, "r") as f_in:
162162
raw = schism_yaml.load_raw(f_in)
163+
if yaml_root is not None:
164+
for key in yaml_root.split("."):
165+
if not isinstance(raw, dict) or key not in raw:
166+
raise ValueError(
167+
f"yaml_root key '{key}' not found in YAML structure"
168+
)
169+
raw = raw[key]
163170
return SchismPolygonDictConverter().read(raw)
164171
else:
165172
raise ValueError("File not found")
@@ -326,10 +333,19 @@ def get_writer(self, name):
326333
raise ValueError("Not in the SchismPolygonIoFactory")
327334

328335

329-
def read_polygons(fpath):
330-
"""Read a polygon file"""
336+
def read_polygons(fpath, yaml_root=None):
337+
"""Read a polygon file
338+
339+
Parameters
340+
----------
341+
fpath : str
342+
Path to the polygon file (YAML or Shapefile).
343+
yaml_root : str, optional
344+
Dot-separated path to navigate into a nested YAML structure
345+
before reading polygons (e.g., 'mesh.depth_enforcement').
346+
"""
331347
if fpath.endswith(".yaml"):
332-
return SchismPolygonIoFactory().get_reader("yaml").read(fpath)
348+
return SchismPolygonIoFactory().get_reader("yaml").read(fpath, yaml_root=yaml_root)
333349
elif fpath.endswith(".shp"):
334350
return SchismPolygonIoFactory().get_reader("shp").read(fpath)
335351
else:

0 commit comments

Comments
 (0)