Skip to content

Commit f52729c

Browse files
committed
Make a start on merging Bedrock GI Databases
1 parent 3141844 commit f52729c

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

examples/hk_kaitak_ags3/hk_kaitak_ags3_to_brgi_geodb.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,30 @@ def _(mo):
225225
return
226226

227227

228+
@app.cell
229+
def _():
230+
none = None
231+
boolean = not none
232+
boolean
233+
return
234+
235+
228236
@app.cell
229237
def _(CRS, zip, zipfile):
230238
from bedrock_ge.gi.ags3 import ags3_to_brgi_db_mapping
231239
from bedrock_ge.gi.ags_parser import ags_to_brgi_db_mapping
232240
from bedrock_ge.gi.mapping_models import BedrockGIMapping
233241
from bedrock_ge.gi.mapper import map_to_brgi_db
242+
from bedrock_ge.gi.db_operations import merge_databases
234243

235244
projected_crs = CRS("EPSG:2326")
236245
vertrical_crs = CRS("EPSG:5738")
246+
brgi_db_from_1_ags3_file = None
247+
brgi_db_obj = None
237248

238249
with zipfile.ZipFile(zip) as zip_ref:
239250
# Iterate over files and directories in the .zip archive
240-
for file_name in zip_ref.namelist():
251+
for i, file_name in enumerate(zip_ref.namelist()):
241252
# Only process files that have an .ags or .AGS extension
242253
if file_name.lower().endswith(".ags"):
243254
print(f"\n🖥️ Processing {file_name} ...")
@@ -246,7 +257,15 @@ def _(CRS, zip, zipfile):
246257
ags3_mapping = ags_to_brgi_db_mapping(
247258
ags3_file, projected_crs, vertrical_crs
248259
)
249-
brgi_db_obj = map_to_brgi_db(ags3_mapping)
260+
brgi_db_from_1_ags3_file = map_to_brgi_db(ags3_mapping)
261+
262+
# if not brgi_db_obj:
263+
# brgi_db_obj = brgi_db_from_1_ags3_file
264+
265+
# if brgi_db_obj and brgi_db_from_1_ags3_file:
266+
# brgi_db_obj = merge_databases(brgi_db_obj, brgi_db_from_1_ags3_file)
267+
268+
# print(f"i = {i}: brgi_db = {brgi_db_obj}")
250269

251270
# with zipfile.ZipFile(zip) as zip_ref:
252271
# file_name = "21659/9508008.AGS"

src/bedrock_ge/gi/db_operations.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import pandas as pd
2+
3+
from bedrock_ge.gi.schemas import (
4+
BedrockGIDatabase,
5+
InSituTestSchema,
6+
LocationSchema,
7+
ProjectSchema,
8+
SampleSchema,
9+
)
10+
from bedrock_ge.gi.validate import check_foreign_key
11+
12+
13+
def merge_databases(
14+
target_db: BedrockGIDatabase,
15+
incoming_db: BedrockGIDatabase,
16+
) -> BedrockGIDatabase:
17+
"""Merges the incoming Bedrock GI database into the target Bedrock GI database.
18+
19+
The function concatenates the pandas DataFrames of the second dict of
20+
DataFrames to the first dict of DataFrames for the keys they have in common.
21+
Keys that are unique to either dictionary will be included in the final
22+
concatenated dictionary.
23+
24+
Args:
25+
target_db (BedrockGIDatabase): The Bedrock GI database into which the incoming data will be merged.
26+
incoming_db (BedrockGIDatabase): The Bedrock GI database containing the data to be merged.
27+
28+
Returns:
29+
BedrockGIDatabase: Merged Bedrock GI database.
30+
"""
31+
# write the body of this function that merges the incoming_db (BedrockGIDatabase) into the target_db (BedrockGIDatabase).
32+
# duplicate rows in the incoming_db (BedrockGIDatabase) will be dropped.
33+
# After merging tables validate them with the schemas from bedrock_ge.gi.schemas and check that foreign keys are correct.
34+
# In case the incoming_db contains tables that are not in the target_db, add them to the target_db.
35+
# The function must return a BedrockGIDatabase object.
36+
merged_project = pd.concat(
37+
[target_db.Project, incoming_db.Project], ignore_index=True
38+
)
39+
ProjectSchema.validate(merged_project)
40+
41+
merged_location = pd.concat(
42+
[target_db.Location, incoming_db.Location], ignore_index=True
43+
)
44+
LocationSchema.validate(merged_location)
45+
check_foreign_key("project_uid", merged_project, merged_location)
46+
47+
merged_insitu = {}
48+
49+
merged_db = {
50+
"Project": merged_project,
51+
"Location": merged_location,
52+
}
53+
54+
# merged_db = BedrockGIDatabase(
55+
# Project=target_db.Project.append(incoming_db.Project),
56+
# Location=target_db.Location.append(incoming_db.Location),
57+
# InSituTests={
58+
# k: target_db.InSituTests[k].append(incoming_db.InSituTests[k])
59+
# for k in target_db.InSituTests
60+
# if k in incoming_db.InSituTests
61+
# },
62+
# Sample=target_db.Sample.append(incoming_db.Sample),
63+
# LabTests={
64+
# k: target_db.LabTests[k].append(incoming_db.LabTests[k])
65+
# for k in target_db.LabTests
66+
# if k in incoming_db.LabTests
67+
# },
68+
# Other={
69+
# k: target_db.Other[k].append(incoming_db.Other[k])
70+
# for k in target_db.Other
71+
# if k in incoming_db.Other
72+
# },
73+
# )
74+
return merged_db

0 commit comments

Comments
 (0)