Skip to content

Commit a210a63

Browse files
remove fiona as dependency use geopandas
1 parent d96322e commit a210a63

1 file changed

Lines changed: 43 additions & 62 deletions

File tree

schimpy/mesh_volume_tvd.py

Lines changed: 43 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -627,41 +627,30 @@ def _write_perimeter_shapefile(
627627
edge_mean_d = _edge_mean_depth(mesh, node_depth)
628628
if not path.lower().endswith(".shp"):
629629
path += ".shp"
630-
schema = {
631-
"geometry": "LineString",
632-
"properties": {
633-
"edge_id": "int",
634-
"n1": "int",
635-
"n2": "int",
636-
"label": "str",
637-
"depth_mean": "float",
638-
},
639-
}
630+
631+
polylines = []
632+
for eidx, lab in labels.items():
633+
n1, n2 = int(mesh.edges[eidx, 0]), int(mesh.edges[eidx, 1])
634+
geom = LineString(
635+
[[(float(xs[n1]), float(ys[n1])), (float(xs[n2]), float(ys[n2]))]]
636+
)
637+
polylines.append(
638+
{
639+
"edge_id": int(eidx),
640+
"n1": n1,
641+
"n2": n2,
642+
"label": str(label),
643+
"geometry": geom,
644+
"depth_mean": float(edge_mean_d[eidx]),
645+
}
646+
)
647+
648+
gdf = gpd.GeoDataFrame(polylines)
649+
640650
if epsg is not None:
641-
try:
642-
from pyproj import CRS
651+
gdf.set_crs(epsg=epsg, inplace=True)
643652

644-
wkt = CRS.from_epsg(int(epsg)).to_wkt()
645-
except Exception:
646-
wkt = ""
647-
with fiona.open(path, "w", driver="ESRI Shapefile", schema=schema, crs=wkt) as shp:
648-
for eidx, lab in labels.items():
649-
n1, n2 = int(mesh.edges[eidx, 0]), int(mesh.edges[eidx, 1])
650-
geom = LineString(
651-
[[(float(xs[n1]), float(ys[n1])), (float(xs[n2]), float(ys[n2]))]]
652-
)
653-
shp.write(
654-
{
655-
"geometry": mapping(geom),
656-
"properties": {
657-
"edge_id": int(eidx),
658-
"n1": n1,
659-
"n2": n2,
660-
"label": str(lab),
661-
"depth_mean": float(edge_mean_d[eidx]),
662-
},
663-
}
664-
)
653+
gdf.to_file(path, driver="ESRI Shapefile")
665654

666655

667656
# ------------------------ Merging + Visualization -----------------------------
@@ -779,40 +768,32 @@ def _merged_polylines_and_node_rows(mesh, labels_all: Dict[int, str]):
779768

780769

781770
def _write_merged_shapefile(polylines, path: str, epsg: int = None):
771+
"""
772+
Write 2D polyline Shapefile (.shp + .shx + .dbf, and .prj if EPSG given) for perimeter edges.
773+
Uses geopandas + shapely only.
774+
"""
775+
782776
if not path.lower().endswith(".shp"):
783777
path += ".shp"
784778

785-
schema = {
786-
"geometry": "LineString",
787-
"properties": {
788-
"edge_id": "int",
789-
"n1": "int",
790-
"n2": "int",
791-
"label": "str",
792-
"depth_mean": "float",
793-
},
794-
}
779+
# Build a GeoDataFrame from your polylines
780+
gdf = gpd.GeoDataFrame(
781+
[
782+
{
783+
"poly_id": int(p["poly_id"]),
784+
"label": str(p["label"]),
785+
"geometry": LineString(p["coords"]),
786+
}
787+
for p in polylines
788+
]
789+
)
790+
791+
# Set CRS if provided
795792
if epsg is not None:
796-
try:
797-
from pyproj import CRS
793+
gdf.set_crs(epsg=epsg, inplace=True)
798794

799-
wkt = CRS.from_epsg(int(epsg)).to_wkt()
800-
except Exception:
801-
wkt = ""
802-
with fiona.open(path, "w", driver="ESRI Shapefile", schema=schema, crs=wkt) as shp:
803-
w.field("poly_id", "N", 10, 0)
804-
w.field("label", "C", 16)
805-
for p in polylines:
806-
geom = LineString([[(float(x), float(y)) for (x, y) in p["coords"]]])
807-
shp.write(
808-
{
809-
"geometry": mapping(geom),
810-
"properties": {
811-
"poly_id": int(p["poly_id"]),
812-
"label": str(p["label"]),
813-
},
814-
}
815-
)
795+
# Write to shapefile
796+
gdf.to_file(path, driver="ESRI Shapefile")
816797

817798

818799
def _write_nodes_csv(node_rows, path: str, epsg: int = None):

0 commit comments

Comments
 (0)