Skip to content
Open
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
1 change: 0 additions & 1 deletion backend/Data/flood-hazard/VT_Flood

This file was deleted.

87 changes: 87 additions & 0 deletions backend/Data/flood-hazard/table_build.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: "Flood Hazard Table Build"
format: html
---

## Load dataset

```{python}
import duckdb
import pandas as pd

con = duckdb.connect("flood.db")
raw = con.execute("SELECT * FROM raw").df()
raw.head()

raw.isna().sum().sort_values(ascending=False)

for col in raw.columns:
print("COLUMN:", col)
try:
print(raw[col].value_counts().head())
except Exception as e:
print("Could not compute value counts:", e)
print("\n")


```

## Null values

```{python}
for col in raw.columns:
print(col)
print(raw[col].value_counts(dropna=False).head(10))
print("\n")
import numpy as np

raw["VELOCITY"] = raw["VELOCITY"].replace(-9999, np.nan)


```

## Dropping columns

```{python}


drop_cols = [
"AR_SUBTRV",
"BFE_REVERT",
"DEP_REVERT"
]

drop_cols

info_cols = [
"OBJECTID",
"AR_REVERT",
"DFIRM_ID",
"VERSION_ID",
"FLD_AR_ID",
"STUDY_TYP",
"FLD_ZONE",
"ZONE_SUBTYP",
"SFHA_TF",
"STATIC_BFE",
"V_DATUM",
"DEPTH",
"LEN_UNIT",
"VELOCITY",
"VEL_UNIT",
"DUAL_ZONE",
"SOURCE_CIT"
]

info_cols

```

```{python}
geom_cols = ["OBJECTID", "geometry"]
geom_cols

final_cols = info_cols + ["geometry"]
final_cols

```
Binary file added backend/Data/flood-hazard/vt-flood-hazard.parquet
Binary file not shown.
30 changes: 30 additions & 0 deletions backend/build/flooding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from build import BACKEND, CON, data_dir

PARQUET = data_dir / "Flooding" / "vt-flood-hazard.parquet"
proc_dir = BACKEND / "Data" / "_Processed" / "flood"
SQL_DIR = BACKEND / "build" / "sql"


def main():

# Load raw table from parquet
CON.execute(f"""
CREATE OR REPLACE VIEW raw AS
SELECT *
FROM read_parquet('{PARQUET}')
""")

# Run SQL files in order
for name in ["flood_info.sql", "flood_geom.sql", "flood_build.sql"]:
CON.execute((SQL_DIR / name).read_text())

for table in ["flood_hazard"]:
CON.execute(
f"COPY (SELECT * FROM {table}) TO '{proc_dir / 'Flooding' / f'{table}.parquet'}' "
)

print("Flood hazard tables built.")


if __name__ == "__main__":
main()
21 changes: 21 additions & 0 deletions backend/build/sql/flood_build.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CREATE OR REPLACE TABLE hazard AS
SELECT
i.id,
i.DFIRM_ID,
i.FLD_AR_ID,
i.STUDY_TYP,
i.FLD_ZONE,
i.ZONE_SUBTY,
i.SFHA_TF,
i.STATIC_BFE,
i.V_DATUM,
i.DEPTH,
i.LEN_UNIT,
i.VELOCITY,
i.VEL_UNIT,
i.AR_REVERT,
i.DUAL_ZONE,
i.SOURCE_CIT,
g.geometry AS geom
FROM info AS i
INNER JOIN geom AS g USING (id);
5 changes: 5 additions & 0 deletions backend/build/sql/flood_geom.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE OR REPLACE TABLE geom AS
SELECT
OBJECTID AS id,
geometry
FROM raw;
54 changes: 54 additions & 0 deletions backend/build/sql/flood_info.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
CREATE OR REPLACE TABLE info AS
SELECT

-- Unique ID assigned by FEMA
OBJECTID AS id,

-- Indicates whether the flood zone is under heightened risk
AR_REVERT,

-- FEMA Digital Flood Insurance Rate Map identifier
DFIRM_ID,

-- Version number of the FEMA flood hazard dataset
VERSION_ID,

-- Unique flood area identifier used by FEMA
FLD_AR_ID,

-- Type of flood study conducted
STUDY_TYP,

-- FEMA flood zone designation
FLD_ZONE,

--Subtype of the flood zone providing additional classification detail
ZONE_SUBTY,

-- Indicates whether the area is a Special Flood Hazard Area
SFHA_TF,

-- Base Flood Elevation in feet
STATIC_BFE,

-- Vertical datum used for the BFE measurement
V_DATUM,

-- Flood depth in feet
DEPTH,

-- Unit of measurement for linear values
LEN_UNIT,

-- Flood velocity in feet per second
VELOCITY,

-- Unit of measurement for velocity values
VEL_UNIT,

-- Indicates whether the area is part of dual-zone classification
DUAL_ZONE,

-- Citation or reference for the source of the flood hazard data
SOURCE_CIT
FROM raw;
Loading
Loading