Skip to content

Commit af60568

Browse files
committed
Remove old functions from Kai Tak, HK notebook
1 parent 32652d6 commit af60568

File tree

1 file changed

+34
-138
lines changed

1 file changed

+34
-138
lines changed

examples/hk_kaitak_ags3/hk_kaitak_ags3_to_brgi_geodb.py

Lines changed: 34 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -45,89 +45,38 @@ def _():
4545
from pyproj.crs import CompoundCRS
4646
from shapely import wkt, Point
4747

48-
from bedrock_ge.gi.ags.read import ags_to_dfs
49-
from bedrock_ge.gi.ags.transform import ags3_db_to_no_gis_brgi_db
50-
from bedrock_ge.gi.concatenate import concatenate_databases
51-
from bedrock_ge.gi.gis_geometry import calculate_gis_geometry
48+
# Old functions that need to be removed from the Bedrock codebase
5249
from bedrock_ge.gi.validate import check_brgi_database, check_no_gis_brgi_database
53-
from bedrock_ge.gi.write import write_gi_db_to_gpkg
50+
51+
from bedrock_ge.gi.ags_parser import ags_to_brgi_db_mapping
52+
from bedrock_ge.gi.db_operations import merge_databases
53+
from bedrock_ge.gi.mapper import map_to_brgi_db
54+
from bedrock_ge.gi.geospatial import create_brgi_geospatial_database
55+
from bedrock_ge.gi.schemas import InSituTestSchema
56+
from bedrock_ge.gi.io_utils import geodf_to_df
57+
from bedrock_ge.gi.write import write_brgi_db_to_file
5458

5559
print(platform.system())
5660
print(sys.version)
5761
# print(sys.executable)
5862
return (
5963
CRS,
6064
Point,
61-
ags3_db_to_no_gis_brgi_db,
62-
ags_to_dfs,
63-
calculate_gis_geometry,
64-
check_brgi_database,
65-
check_no_gis_brgi_database,
66-
concatenate_databases,
65+
ags_to_brgi_db_mapping,
66+
create_brgi_geospatial_database,
67+
geodf_to_df,
6768
gpd,
6869
io,
70+
map_to_brgi_db,
71+
merge_databases,
6972
mo,
70-
pd,
7173
platform,
7274
requests,
75+
write_brgi_db_to_file,
7376
zipfile,
7477
)
7578

7679

77-
@app.cell
78-
def _(
79-
ags3_db_to_no_gis_brgi_db,
80-
ags_to_dfs,
81-
check_no_gis_brgi_database,
82-
concatenate_databases,
83-
zipfile,
84-
):
85-
def zip_of_ags3s_to_bedrock_gi_database(zip, crs):
86-
"""Read AGS 3 files from a ZIP archive and convert them to a dictionary of pandas dataframes."""
87-
brgi_db = {}
88-
with zipfile.ZipFile(zip) as zip_ref:
89-
# Iterate over files and directories in the .zip archive
90-
for file_name in zip_ref.namelist():
91-
# Only process files that have an .ags or .AGS extension
92-
if file_name.lower().endswith(".ags"):
93-
print(f"\n🖥️ Processing {file_name} ...")
94-
with zip_ref.open(file_name) as ags3_file:
95-
# Convert content of a single AGS 3 file to a Dictionary of pandas dataframes (a database)
96-
ags3_db = ags_to_dfs(ags3_file)
97-
report_no = file_name.split("/")[0]
98-
ags3_db["PROJ"]["REPORT_NO"] = int(report_no)
99-
project_uid = f"{ags3_db['PROJ']['PROJ_ID'].iloc[0]}_{file_name}"
100-
ags3_db["PROJ"]["project_uid"] = project_uid
101-
# Remove (Static) CPT AGS 3 group 'STCN' from brgi_db, because CPT data processing needs to be reviewed.
102-
# Not efficient to create a GIS point for every point where a CPT measures a value.
103-
if "STCN" in ags3_db.keys():
104-
del ags3_db["STCN"]
105-
# Create GI data tables with bedrock-ge names and add columns (project_uid, location_uid, sample_uid),
106-
# such that data from multiple AGS files can be combined
107-
brgi_db_from_1_ags3_file = ags3_db_to_no_gis_brgi_db(ags3_db, crs)
108-
print(
109-
f"🧐 Validating the Bedrock GI database from AGS file {file_name}..."
110-
)
111-
check_no_gis_brgi_database(brgi_db_from_1_ags3_file)
112-
print(
113-
f"\n✅ Successfully converted {file_name} to Bedrock GI database and validated!\n"
114-
)
115-
print(
116-
f"🧵 Concatenating Bedrock GI database for {file_name} to existing Bedrock GI database...\n"
117-
)
118-
brgi_db = concatenate_databases(brgi_db, brgi_db_from_1_ags3_file)
119-
120-
# Drop all rows that have completely duplicate rows in the Project table
121-
brgi_db["Project"] = brgi_db["Project"].drop_duplicates()
122-
# Then drop all that unfortunately still have a duplicate project_uid
123-
brgi_db["Project"] = brgi_db["Project"].drop_duplicates(
124-
subset="project_uid", keep="first"
125-
)
126-
return brgi_db
127-
128-
return (zip_of_ags3s_to_bedrock_gi_database,)
129-
130-
13180
@app.cell(hide_code=True)
13281
def _(mo):
13382
mo.md(
@@ -227,26 +176,6 @@ def _(mo):
227176
return
228177

229178

230-
@app.cell
231-
def _():
232-
from bedrock_ge.gi.ags3 import ags3_to_brgi_db_mapping
233-
from bedrock_ge.gi.ags_parser import ags_to_brgi_db_mapping
234-
from bedrock_ge.gi.db_operations import merge_databases
235-
from bedrock_ge.gi.mapper import map_to_brgi_db
236-
from bedrock_ge.gi.geospatial import create_brgi_geospatial_database
237-
from bedrock_ge.gi.schemas import InSituTestSchema
238-
from bedrock_ge.gi.io_utils import geodf_to_df
239-
from bedrock_ge.gi.write import write_brgi_db_to_file
240-
return (
241-
ags_to_brgi_db_mapping,
242-
create_brgi_geospatial_database,
243-
geodf_to_df,
244-
map_to_brgi_db,
245-
merge_databases,
246-
write_brgi_db_to_file,
247-
)
248-
249-
250179
@app.cell
251180
def _(
252181
CRS,
@@ -258,6 +187,7 @@ def _(
258187
):
259188
projected_crs = CRS("EPSG:2326")
260189
vertrical_crs = CRS("EPSG:5738")
190+
261191
ags3_file_brgi_dbs = []
262192
with zipfile.ZipFile(zip) as zip_ref:
263193
# Iterate over files and directories in the .zip archive
@@ -288,31 +218,6 @@ def _(brgi_db):
288218
return
289219

290220

291-
@app.cell
292-
def _(CRS, pd, zip, zip_of_ags3s_to_bedrock_gi_database):
293-
brgi_db_old = zip_of_ags3s_to_bedrock_gi_database(zip, CRS("EPSG:2326"))
294-
295-
# Some ISPT_NVAL (SPT count) are not numeric, e.g. "100/0.29"
296-
# When converting to numeric, these non-numeric values are converted to NaN
297-
brgi_db_old["InSitu_ISPT"]["ISPT_NVAL"] = pd.to_numeric(
298-
brgi_db_old["InSitu_ISPT"]["ISPT_NVAL"], errors="coerce"
299-
)
300-
return (brgi_db_old,)
301-
302-
303-
@app.cell(hide_code=True)
304-
def _(brgi_db_old, mo):
305-
sel_brgi_table = mo.ui.dropdown(brgi_db_old, value="Project")
306-
mo.md(f"Select the Bedrock GI table you want to explore: {sel_brgi_table}")
307-
return (sel_brgi_table,)
308-
309-
310-
@app.cell(hide_code=True)
311-
def _(sel_brgi_table):
312-
sel_brgi_table.value
313-
return
314-
315-
316221
@app.cell(hide_code=True)
317222
def _(mo):
318223
mo.md(
@@ -362,13 +267,6 @@ def _(brgi_geodb):
362267
return
363268

364269

365-
@app.cell
366-
def _(brgi_db_old, calculate_gis_geometry, check_brgi_database):
367-
brgi_geodb_old = calculate_gis_geometry(brgi_db_old)
368-
check_brgi_database(brgi_geodb_old)
369-
return (brgi_geodb_old,)
370-
371-
372270
@app.cell(hide_code=True)
373271
def _(mo):
374272
mo.md(
@@ -383,12 +281,6 @@ def _(mo):
383281
return
384282

385283

386-
@app.cell
387-
def _(brgi_geodb_old):
388-
brgi_geodb_old["LonLatHeight"].explore()
389-
return
390-
391-
392284
@app.cell
393285
def _(Point, brgi_geodb):
394286
gdf = brgi_geodb.InSituTests["GEOL"]
@@ -415,9 +307,9 @@ def _(mo):
415307
return
416308

417309

418-
@app.cell(hide_code=True)
419-
def _(brgi_db_old, mo):
420-
explore_brgi_table = mo.ui.dropdown(brgi_db_old, value="InSitu_ISPT")
310+
@app.cell
311+
def _(brgi_geodb, mo):
312+
explore_brgi_table = mo.ui.dropdown(brgi_geodb.InSituTests, value="ISPT")
421313
mo.md(f"Select the GI table you want to explore: {explore_brgi_table}")
422314
return (explore_brgi_table,)
423315

@@ -429,25 +321,29 @@ def _(explore_brgi_table, mo):
429321
return (filtered_table,)
430322

431323

324+
@app.cell
325+
def _(filtered_df, filtered_table):
326+
print(f"filtered_table.value type: {type(filtered_table.value)}")
327+
print(f"filtered_df.value type: {type(filtered_df.value)}")
328+
return
329+
330+
432331
@app.cell(hide_code=True)
433-
def _(brgi_geodb_old, filtered_table, gpd, mo):
332+
def _(Point, filtered_table, gpd, mo):
434333
def gi_exploration_map(filtered_brgi_table):
435334
if "location_uid" not in filtered_brgi_table.value.columns:
436335
output = mo.md(
437336
"No interactive map with the data selected in the table above can be shown, because the you're exploring isn't linked to the `LonLatHeight` table with a `location_uid` column, i.e. doesn't have `location_uid` as a foreign key."
438337
).callout("warn")
439338
else:
440-
filtered_df = filtered_brgi_table.value.merge(
441-
brgi_geodb_old["LonLatHeight"], on="location_uid", how="inner"
339+
fltrd_gdf = gpd.GeoDataFrame(filtered_brgi_table.value.copy())
340+
fltrd_gdf["geometry"] = fltrd_gdf["geometry"].apply(
341+
lambda geom: Point(geom.coords[0])
442342
)
443-
filtered_gdf = gpd.GeoDataFrame(
444-
filtered_df,
445-
geometry=filtered_df["geometry"],
446-
crs="EPSG:4326", # 4326 is the WGS84 (lon, lat) EPSG code
447-
)
448-
output = filtered_gdf.explore()
343+
output = fltrd_gdf.explore()
449344
return output
450345

346+
451347
gi_exploration_map(filtered_table)
452348
return (gi_exploration_map,)
453349

@@ -472,8 +368,8 @@ def _(mo):
472368

473369

474370
@app.cell(hide_code=True)
475-
def _(brgi_db_old, mo):
476-
explore_brgi_df = mo.ui.dropdown(brgi_db_old, value="InSitu_WETH")
371+
def _(brgi_geodb, mo):
372+
explore_brgi_df = mo.ui.dropdown(brgi_geodb.InSituTests, value="WETH")
477373
mo.md(f"Select the GI table you want to explore: {explore_brgi_df}")
478374
return (explore_brgi_df,)
479375

0 commit comments

Comments
 (0)