Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2022 11 add sia405 to kek export #84

Merged
merged 42 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
81caf28
def export_xtf_data add option export_model_name
sjib Nov 26, 2022
723fcf0
Added network export in addition to KEK export
sjib Nov 26, 2022
a72b205
Added MODEL names in xtf filenames and messages / config update
sjib Nov 26, 2022
4a19bff
adjusting filenames and messages
sjib Nov 26, 2022
c90e8ff
testdata case_c_import_all.xtf korrigiert
sjib Nov 26, 2022
c57b315
testdata ch080qwzAK001099 commented out
sjib Nov 26, 2022
467d54a
added ilival log
sjib Nov 26, 2022
45c2fed
Correction test_data case_c_import_all
sjib Nov 28, 2022
2fe90ec
added additional export also to qgepqwat2ili\_init_.py
sjib Nov 28, 2022
00599aa
ident fixed
sjib Nov 28, 2022
ca8900d
ident fixing 2
sjib Nov 28, 2022
95a928a
Renamed case_c_import_all.xtf again to the data with errors
sjib Nov 28, 2022
26af6ad
Adjust back to latest testdata
sjib Dec 23, 2022
816f99c
Integrate Testdata 2
sjib Dec 23, 2022
119c920
integrating corrected tests test_qgep.py
sjib Dec 23, 2022
f5eea65
Add logger.info Perform various checks
sjib Jan 3, 2023
5e79987
Replace self.assertEquals with self.assertEqual
sjib Jan 3, 2023
3b667ad
try to display path
sjib Jan 3, 2023
8dc9c02
adapted root path as result file gets other name now
sjib Jan 4, 2023
b197883
adapted resultpath
sjib Jan 4, 2023
2a9d3a7
logger.warning(resultpath)
sjib Jan 4, 2023
1e447e9
add print result file
sjib Jan 4, 2023
728447a
adjusted indent
sjib Jan 4, 2023
9b916f6
delete character
sjib Jan 4, 2023
6279172
intend corrected
sjib Jan 4, 2023
78e905b
intention corrected
sjib Jan 4, 2023
51432d9
Add argument export_model_name to cmd call
sjib Jan 11, 2023
da1d349
Adjusted export_model_name to --export_model_name
sjib Jan 12, 2023
6535bf3
adjust test_qgep.py to "--export_model_name" flag
sjib Jan 12, 2023
e5e1287
added info when --export_model_name is set, correction test_qgep.py
sjib Jan 12, 2023
b670749
ILI_EXPORT_MODEL_NAME = config.ABWASSER_ILI_EXPORT_MODEL_NAME added
sjib Jan 12, 2023
4810304
Add checks also for SIA405_ABWASSER_LV95
sjib Jan 12, 2023
4158409
corrections
sjib Jan 12, 2023
834aa85
add file.close() for file1 and file2
sjib Jan 12, 2023
94b8364
find_all_xml - make two versions as bid is different
sjib Jan 12, 2023
9ad3fb7
missing root instead of root2
sjib Jan 12, 2023
dc737da
Merge branch 'master' into 2022-11-add-sia405-to-kek-export
sjib Mar 4, 2023
9830bd7
fix lost piece of code during merge
olivierdalang Mar 9, 2023
abd939b
code style
olivierdalang Mar 9, 2023
72f1b5e
update readme
olivierdalang Mar 9, 2023
8eaf12e
(minor)
olivierdalang Mar 9, 2023
85b58c1
fix export and more refactoring
olivierdalang Mar 9, 2023
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: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ optional arguments:
--pgservice PGSERVICE
name of the pgservice to use to connect to the database (default: pg_qgep)
--log saves a log file next to the input/output file (default: False)
--export_sia405 export the model SIA405_ABWASSER_2015_LV95 (instead of default VSA_KEK_2019_LV95) (default: False)
```

### Import/export QWAT
Expand Down
25 changes: 22 additions & 3 deletions qgepqwat2ili/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ def main(args):
action="store_true",
help="saves the log files next to the input/output file",
)
parser_qgep.add_argument(
"--export_sia405",
action="store_true",
help="export the model SIA405_ABWASSER_2015_LV95 (instead of default VSA_KEK_2019_LV95)",
)

parser_qwat = subparsers.add_parser(
"qwat",
Expand Down Expand Up @@ -129,13 +134,23 @@ def main(args):
config.PGSERVICE = args.pgservice
SCHEMA = config.ABWASSER_SCHEMA
ILI_MODEL = config.ABWASSER_ILI_MODEL
ILI_MODEL_NAME = config.ABWASSER_ILI_MODEL_NAME
if args.export_sia405:
ILI_MODEL_NAME = config.ABWASSER_ILI_MODEL_NAME_SIA405
ILI_EXPORT_MODEL_NAME = config.ABWASSER_ILI_MODEL_NAME_SIA405
else:
ILI_MODEL_NAME = config.ABWASSER_ILI_MODEL_NAME
ILI_EXPORT_MODEL_NAME = None

if args.direction == "export":
utils.ili2db.create_ili_schema(
SCHEMA, ILI_MODEL, make_log_path(log_path, "ilicreate"), recreate_schema=args.recreate_schema
)
qgep_export(selection=args.selection.split(",") if args.selection else None, labels_file=args.labels_file)
utils.ili2db.export_xtf_data(SCHEMA, ILI_MODEL_NAME, args.path, make_log_path(log_path, "iliexport"))

utils.ili2db.export_xtf_data(
SCHEMA, ILI_MODEL_NAME, ILI_EXPORT_MODEL_NAME, args.path, make_log_path(log_path, "iliexport")
)

if not args.skip_validation:
try:
utils.ili2db.validate_xtf_data(args.path, make_log_path(log_path, "ilivalidate"))
Expand Down Expand Up @@ -164,12 +179,16 @@ def main(args):
SCHEMA = config.WASSER_SCHEMA
ILI_MODEL = config.WASSER_ILI_MODEL
ILI_MODEL_NAME = config.WASSER_ILI_MODEL_NAME
ILI_EXPORT_MODEL_NAME = None

if args.direction == "export":
utils.ili2db.create_ili_schema(
SCHEMA, ILI_MODEL, make_log_path(log_path, "ilicreate"), recreate_schema=args.recreate_schema
)
qwat_export(include_hydraulics=args.include_hydraulics)
utils.ili2db.export_xtf_data(SCHEMA, ILI_MODEL_NAME, args.path, make_log_path(log_path, "iliexport"))
utils.ili2db.export_xtf_data(
SCHEMA, ILI_MODEL_NAME, ILI_EXPORT_MODEL_NAME, args.path, make_log_path(log_path, "iliexport")
)
if not args.skip_validation:
try:
utils.ili2db.validate_xtf_data(args.path, make_log_path(log_path, "ilivalidate"))
Expand Down
2 changes: 2 additions & 0 deletions qgepqwat2ili/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
ABWASSER_SCHEMA = "pg2ili_abwasser"
ABWASSER_ILI_MODEL = os.path.join(ILI_FOLDER, "VSA_KEK_2019_2_d_LV95-20210120.ili")
ABWASSER_ILI_MODEL_NAME = "VSA_KEK_2019_LV95"
ABWASSER_ILI_MODEL_NAME_SIA405 = "SIA405_ABWASSER_2015_LV95"
ABWASSER_ILI_EXPORT_MODEL = os.path.join(ILI_FOLDER, "SIA405_Abwasser_2015_2_d-20180417.ili")

QWAT_DEFAULT_PGSERVICE = "qwat"
QWAT_SCHEMA = "qwat_od"
Expand Down
88 changes: 51 additions & 37 deletions qgepqwat2ili/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ def action_do_export():
return
QgsSettings().setValue("qgep_pluging/last_interlis_path", os.path.dirname(file_name))

# File name without extension (used later for export)
file_name_base, _ = os.path.splitext(file_name)

# Configure logging
if export_dialog.logs_next_to_file:
base_log_path = file_name
Expand Down Expand Up @@ -254,48 +257,59 @@ def action_do_export():
# Cleanup
tempdir.cleanup()

# Export from ili2pg model to file
progress_dialog.setLabelText("Saving XTF file...")
QApplication.processEvents()
log_path = make_log_path(base_log_path, "ili2pg-export")
try:
export_xtf_data(
config.ABWASSER_SCHEMA,
config.ABWASSER_ILI_MODEL_NAME,
file_name,
log_path,
)
except CmdException:
progress_dialog.close()
show_failure(
"Could not export the ili2pg schema",
"Open the logs for more details on the error.",
log_path,
)
return
progress_dialog.setValue(75)
for model_name, export_model_name, progress in [
(config.ABWASSER_ILI_MODEL_NAME, None, 50),
(config.ABWASSER_ILI_MODEL_NAME_SIA405, config.ABWASSER_ILI_MODEL_NAME_SIA405, 70),
]:

export_file_name = f"{file_name_base}_{model_name}.xtf"

# Export from ili2pg model to file
progress_dialog.setLabelText(f"Saving XTF file [{model_name}]...")
QApplication.processEvents()
log_path = make_log_path(base_log_path, f"ili2pg-export-{model_name}")
try:
export_xtf_data(
config.ABWASSER_SCHEMA,
model_name,
export_model_name,
export_file_name,
log_path,
)
except CmdException:
progress_dialog.close()
show_failure(
"Could not export the ili2pg schema",
"Open the logs for more details on the error.",
log_path,
)
continue
progress_dialog.setValue(progress + 10)

progress_dialog.setLabelText(f"Validating the network output file [{model_name}]...")
QApplication.processEvents()
log_path = make_log_path(base_log_path, f"ilivalidator-{model_name}")
try:
validate_xtf_data(
export_file_name,
log_path,
)
except CmdException:
progress_dialog.close()
show_failure(
"Invalid file",
f"The created file is not a valid {model_name} XTF file.",
log_path,
)
continue

progress_dialog.setValue(progress + 20)

progress_dialog.setLabelText("Validating the output file...")
QApplication.processEvents()
log_path = make_log_path(base_log_path, "ilivalidator")
try:
validate_xtf_data(
file_name,
log_path,
)
except CmdException:
progress_dialog.close()
show_failure(
"Invalid file",
"The created file is not a valid XTF file.",
log_path,
)
return
progress_dialog.setValue(100)

show_success(
"Sucess",
f"Data successfully exported to {file_name}",
f"Data successfully exported to {file_name_base}",
os.path.dirname(log_path),
)

Expand Down
87 changes: 77 additions & 10 deletions qgepqwat2ili/tests/test_qgep.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
logger.addHandler(handler)


def findall_in_xml(root, tag, basket="VSA_KEK_2019_LV95.KEK"):
def findall_in_xml_kek_2019(root, tag, basket="VSA_KEK_2019_LV95.KEK"):
ns = {"ili": "http://www.interlis.ch/INTERLIS2.3"}
return root.findall(f"ili:DATASECTION/ili:{basket}/ili:{tag}", ns)


def findall_in_xml_sia_abwasser_2015(root, tag, basket="SIA405_ABWASSER_2015_LV95.SIA405_Abwasser"):
ns = {"ili": "http://www.interlis.ch/INTERLIS2.3"}
return root.findall(f"ili:DATASECTION/ili:{basket}/ili:{tag}", ns)

Expand Down Expand Up @@ -86,15 +91,16 @@ def test_case_b_export_complete_qgep_to_xtf(self):
path = os.path.join(tempfile.mkdtemp(), "export.xtf")
main(["qgep", "export", path, "--recreate_schema"])


def test_case_d_import_complete_xtf_to_qgep(self):
"""
# D. import a whole valid interlis transfer file into QGEP
"""

# Incomming XTF case_c_import_all_without_errors.xtf
# THIS INPUT FILE IS VALID !
path = os.path.join(os.path.dirname(__file__), "..", "data", "test_data", "case_d_import_all_without_errors.xtf")
path = os.path.join(
os.path.dirname(__file__), "..", "data", "test_data", "case_d_import_all_without_errors.xtf"
)

# Prepare subset db (we import in an empty schema)
main(["setupdb", "empty"])
Expand All @@ -116,7 +122,7 @@ def test_case_d_import_complete_xtf_to_qgep(self):
# checking some properties # TODO : add some more...
self.assertEqual(session.query(QGEP.manhole).get("ch080qwzNS000113").year_of_construction, 1950)
session.close()

def test_case_e_export_selection(self):
"""
# E. export a selection
Expand All @@ -125,7 +131,52 @@ def test_case_e_export_selection(self):
# Prepare db
main(["setupdb", "full"])

path = os.path.join(tempfile.mkdtemp(), "export.xtf")
# 1. Export VSA_KEK_2019_LV95
path = os.path.join(tempfile.mkdtemp(), "export_VSA_KEK_2019_LV95.xtf")
selection = [
# reach_id
"ch13p7mzRE001221",
# node_a_id
"ch13p7mzWN003445",
# node_b_id
"ch13p7mzWN008122",
]
labels_file = os.path.join(os.path.dirname(__file__), "data", "labels.geojson")
main(
[
"qgep",
"export",
path,
"--recreate_schema",
"--selection",
",".join(selection),
"--labels_file",
labels_file,
]
)

# Perform various checks
logger.warning("Perform various checks VSA_KEK_2019_LV95 ...")

root = ET.parse(path)

self.assertEqual(len(findall_in_xml_kek_2019(root, "SIA405_ABWASSER_2015_LV95.SIA405_Abwasser.Kanal")), 1)
self.assertEqual(
len(findall_in_xml_kek_2019(root, "SIA405_ABWASSER_2015_LV95.SIA405_Abwasser.Normschacht")), 2
)
self.assertEqual(
len(findall_in_xml_kek_2019(root, "SIA405_ABWASSER_2015_LV95.SIA405_Abwasser.Haltung_Text")), 3
)
self.assertEqual(
len(findall_in_xml_kek_2019(root, "SIA405_ABWASSER_2015_LV95.SIA405_Abwasser.Abwasserbauwerk_Text")), 6
)

def test_case_f_export_selection_sia405(self):
"""
# F. export a selection
"""

path = os.path.join(tempfile.mkdtemp(), "export_VSA_KEK_2019_LV95.xtf")
selection = [
# reach_id
"ch13p7mzRE001221",
Expand All @@ -140,20 +191,36 @@ def test_case_e_export_selection(self):
"qgep",
"export",
path,
"--export_sia405",
"--recreate_schema",
"--selection",
",".join(selection),
"--labels_file",
labels_file,
]
)

# Perform various checks
logger.warning("Perform various checks SIA405_ABWASSER_2015_LV95 ...")

root = ET.parse(path)

This comment was marked as resolved.

This comment was marked as resolved.

self.assertEquals(len(findall_in_xml(root, "SIA405_ABWASSER_2015_LV95.SIA405_Abwasser.Kanal")), 1)
self.assertEquals(len(findall_in_xml(root, "SIA405_ABWASSER_2015_LV95.SIA405_Abwasser.Normschacht")), 2)
self.assertEquals(len(findall_in_xml(root, "SIA405_ABWASSER_2015_LV95.SIA405_Abwasser.Haltung_Text")), 3)
self.assertEquals(
len(findall_in_xml(root, "SIA405_ABWASSER_2015_LV95.SIA405_Abwasser.Abwasserbauwerk_Text")), 6

self.assertEqual(
len(findall_in_xml_sia_abwasser_2015(root, "SIA405_ABWASSER_2015_LV95.SIA405_Abwasser.Kanal")), 1
)
self.assertEqual(
len(findall_in_xml_sia_abwasser_2015(root, "SIA405_ABWASSER_2015_LV95.SIA405_Abwasser.Normschacht")), 2
)
self.assertEqual(
len(findall_in_xml_sia_abwasser_2015(root, "SIA405_ABWASSER_2015_LV95.SIA405_Abwasser.Haltung_Text")), 3
)
self.assertEqual(
len(
findall_in_xml_sia_abwasser_2015(
root, "SIA405_ABWASSER_2015_LV95.SIA405_Abwasser.Abwasserbauwerk_Text"
)
),
6,
)


Expand Down
10 changes: 9 additions & 1 deletion qgepqwat2ili/utils/ili2db.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ def import_xtf_data(schema, xtf_file, log_path):
)


def export_xtf_data(schema, model_name, xtf_file, log_path):
def export_xtf_data(schema, model_name, export_model_name, xtf_file, log_path):
logger.info("EXPORT ILIDB...")

# if optional export_model_name is set, add it to the args
if export_model_name:
export_model_name_args = ["--exportModels", export_model_name]
else:
export_model_name_args = []

exec_(
" ".join(
[
Expand All @@ -108,6 +115,7 @@ def export_xtf_data(schema, model_name, xtf_file, log_path):
"--export",
"--models",
f"{model_name}",
*export_model_name_args,
*get_pgconf_as_ili_args(),
"--dbschema",
f"{schema}",
Expand Down