Skip to content

Commit

Permalink
cross_sections and river_reaches methods call _get_polylines
Browse files Browse the repository at this point in the history
  • Loading branch information
thwllms committed Jul 2, 2024
1 parent 216c591 commit b2903b5
Showing 1 changed file with 8 additions and 37 deletions.
45 changes: 8 additions & 37 deletions src/rashdf/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class RasGeomHdf(RasHdf):
BREAKLINES_PATH = f"{GEOM_PATH}/2D Flow Area Break Lines"
REFERENCE_LINES_PATH = f"{GEOM_PATH}/Reference Lines"
REFERENCE_POINTS_PATH = f"{GEOM_PATH}/Reference Points"
CROSS_SECTIONS = f"{GEOM_PATH}/Cross Sections"
RIVER_CENTERLINES = f"{GEOM_PATH}/River Centerlines"

def __init__(self, name: str, **kwargs):
"""Open a HEC-RAS Geometry HDF file.
Expand Down Expand Up @@ -606,33 +608,17 @@ def cross_sections(self, datetime_to_str: bool = False) -> GeoDataFrame:
GeoDataFrame
A GeoDataFrame containing the model 1D cross sections if they exist.
"""
if "/Geometry/Cross Sections" not in self:
if self.CROSS_SECTIONS not in self:
return GeoDataFrame()

xs_data = self["/Geometry/Cross Sections"]
xs_data = self[self.CROSS_SECTIONS]
v_conv_val = np.vectorize(convert_ras_hdf_value)
xs_attrs = xs_data["Attributes"][()]
xs_dict = {"xs_id": range(xs_attrs.shape[0])}
xs_dict.update(
{name: v_conv_val(xs_attrs[name]) for name in xs_attrs.dtype.names}
)
geoms = list()
for pnt_start, pnt_cnt, part_start, part_cnt in xs_data["Polyline Info"][()]:
points = xs_data["Polyline Points"][()][pnt_start : pnt_start + pnt_cnt]
if part_cnt == 1:
geoms.append(LineString(points))
else:
parts = xs_data["Polyline Parts"][()][
part_start : part_start + part_cnt
]
geoms.append(
MultiLineString(
list(
points[part_pnt_start : part_pnt_start + part_pnt_cnt]
for part_pnt_start, part_pnt_cnt in parts
)
)
)
geoms = self._get_polylines(self.CROSS_SECTIONS)
xs_gdf = GeoDataFrame(
xs_dict,
geometry=geoms,
Expand All @@ -652,33 +638,18 @@ def river_reaches(self, datetime_to_str: bool = False) -> GeoDataFrame:
GeoDataFrame
A GeoDataFrame containing the model 1D river reach lines if they exist.
"""
if "/Geometry/River Centerlines" not in self:
if self.RIVER_CENTERLINES not in self:
return GeoDataFrame()

river_data = self["/Geometry/River Centerlines"]
river_data = self[self.RIVER_CENTERLINES]
v_conv_val = np.vectorize(convert_ras_hdf_value)
river_attrs = river_data["Attributes"][()]
river_dict = {"river_id": range(river_attrs.shape[0])}
river_dict.update(
{name: v_conv_val(river_attrs[name]) for name in river_attrs.dtype.names}
)
geoms = list()
for pnt_start, pnt_cnt, part_start, part_cnt in river_data["Polyline Info"][()]:
points = river_data["Polyline Points"][()][pnt_start : pnt_start + pnt_cnt]
if part_cnt == 1:
geoms.append(LineString(points))
else:
parts = river_data["Polyline Parts"][()][
part_start : part_start + part_cnt
]
geoms.append(
MultiLineString(
list(
points[part_pnt_start : part_pnt_start + part_pnt_cnt]
for part_pnt_start, part_pnt_cnt in parts
)
)
)
geoms = self._get_polylines(self.RIVER_CENTERLINES)
river_gdf = GeoDataFrame(
river_dict,
geometry=geoms,
Expand Down

0 comments on commit b2903b5

Please sign in to comment.