Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions pipelines/rsdynamics_to_athena/layer_definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,47 @@
{
"name": "low_lying_floodplain_area",
"friendly_name": "Low-Lying Floodplain Area",
"dtype": "double"
"dtype": "double",
"data_unit": "m**2"
},
{
"name": "low_lying_floodplain_prop",
"friendly_name": "Low-Lying Floodplain Proportion",
"dtype": "double"
"dtype": "double",
"data_unit": "dimensionless",
"description": "Ratio of low-lying floodplain to DGO area"
},
{
"name": "active_channel_area",
"friendly_name": "Active Channel Area",
"dtype": "double"
"dtype": "double",
"data_unit": "m**2"
},
{
"name": "active_channel_prop",
"friendly_name": "Active Channel Proportion",
"dtype": "double"
"dtype": "double",
"data_unit": "dimensionless",
"description": "Ratio of active channel to DGO area"
},
{
"name": "elevated_floodplain_area",
"friendly_name": "Elevated Floodplain Area",
"dtype": "double"
"dtype": "double",
"data_unit": "m**2"
},
{
"name": "elevated_floodplain_prop",
"friendly_name": "Elevated Floodplain Proportion",
"dtype": "double"
"dtype": "double",
"data_unit": "dimensionless",
"description": "Ratio of elevated floodplain to DGO area"
},
{
"name": "floodplain_area",
"friendly_name": "Floodplain Area",
"dtype": "double"
"dtype": "double",
"data_unit": "m**2"
},
{
"name": "floodplain_prop",
Expand All @@ -71,17 +81,20 @@
{
"name": "centerline_length",
"friendly_name": "Centerline Length",
"dtype": "double"
"dtype": "double",
"data_unit": "m"
},
{
"name": "segment_area",
"friendly_name": "Segment Area",
"dtype": "double"
"dtype": "double",
"data_unit": "m**2"
},
{
"name": "integrated_width",
"friendly_name": "Integrated Width",
"dtype": "double"
"dtype": "double",
"data_unit": "m"
},
{
"name": "dgo_geom",
Expand All @@ -96,7 +109,8 @@
{
"name": "rd_project_id",
"friendly_name": "RSDynamics Project ID",
"dtype": "string"
"dtype": "string",
"data_unit": "NA"
},
{
"name": "rd_date_created_ts",
Expand All @@ -123,12 +137,13 @@
{
"layer_id": "rsdynamics_metrics",
"layer_name": "RSDynamics Metrics",
"description": "Riverscapes Dynamics Project Metrics data for each DGO, landcover, epoch",
"description": "Riverscapes Dynamics Project Metrics data for each DGO, landcover, epoch. Join with rsdynamics on rd_project_id + dgo_id.",
"columns": [
{
"name": "dgo_id",
"friendly_name": "DGO ID",
"dtype": "bigint"
"dtype": "bigint",
"data_unit": "NA"
},
{
"name": "landcover",
Expand All @@ -153,22 +168,26 @@
{
"name": "area",
"friendly_name": "Area",
"dtype": "double"
"dtype": "double",
"data_unit": "m**2"
},
{
"name": "areapc",
"friendly_name": "Area Percent",
"dtype": "double"
"dtype": "double",
"data_unit": "%"
},
{
"name": "width",
"friendly_name": "Width",
"dtype": "double"
"dtype": "double",
"data_unit": "m"
},
{
"name": "widthpc",
"friendly_name": "Width Percent",
"dtype": "double"
"dtype": "double",
"data_unit": "%"
},
{
"name": "huc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def extract_dgo_metrics_to_dataframe(gpkg_path: str, spatialite_path: str) -> pd

t2 = time.time()
sql = f'SELECT {", ".join(col_names)} FROM vbet_dgos'
log.debug(f"Select query:\n{sql}")
df = pd.read_sql_query(sql, conn)
log.debug(f"Loaded data from sql to dataframe in {time.time() - t2:.2f}s. Shape {df.shape}")

Expand Down Expand Up @@ -208,7 +209,8 @@ def extract_dgo_metrics_to_dataframe(gpkg_path: str, spatialite_path: str) -> pd
df_final = df_final.pivot_table(
index=['fid', 'landcover', 'epoch_length', 'epoch_name', 'confidence'],
columns='metric_name',
values='measurement'
values='measurement',
observed=True
).reset_index()
log.debug(f"Pivot data {time.time() - t6:.2f}s")

Expand All @@ -222,6 +224,7 @@ def extract_dgo_metrics_to_dataframe(gpkg_path: str, spatialite_path: str) -> pd
def extract_dgos_to_geodataframe(gpkg_path: str, spatialite_path: str) -> gpd.GeoDataFrame:
"""
Connect to the GeoPackage, run the SQL, and return a GeoDataFrame.
Assumes data are in EPSG:2193 (True for at least one of the New Zealand projects)
"""
conn = apsw.Connection(gpkg_path)
conn.enable_load_extension(True)
Expand Down Expand Up @@ -265,7 +268,8 @@ def extract_dgos_to_geodataframe(gpkg_path: str, spatialite_path: str) -> gpd.Ge
df = df.loc[:, [col for col in df.columns if col != 'dgoid']]
# convert wkb geometry to shapely objects
df['dgo_geom'] = df['dgo_geom'].apply(wkb.loads) # pyright: ignore[reportCallIssue, reportArgumentType]
gdf = gpd.GeoDataFrame(df, geometry='dgo_geom', crs='EPSG:4326')
gdf = gpd.GeoDataFrame(df, geometry='dgo_geom', crs='EPSG:2193') # SOURCE EPSG
gdf = gdf.to_crs('EPSG:4326') # DESTINATION EPSG

bbox_df = gdf.geometry.bounds.rename(columns={'minx': 'xmin', 'miny': 'ymin', 'maxx': 'xmax', 'maxy': 'ymax'})
# Combine into a struct-like dict for each row
Expand Down