Skip to content

Commit

Permalink
new function get_cl_re_ids, get_ws_wn_ids, remove_from_selection and …
Browse files Browse the repository at this point in the history
…add_to_selection
  • Loading branch information
sjib committed Nov 6, 2024
1 parent 4da9af5 commit 14a441c
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 8 deletions.
40 changes: 33 additions & 7 deletions qgepqwat2ili/qgepsia405/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
from .. import utils

# 4.10.2024
from ..utils.ili2db import skip_wwtp_structure_ids
#from ..utils.ili2db import skip_wwtp_structure_ids
# 6.11.2024 replaced with
from ..utils.ili2db import get_cl_re_ids
from ..utils.ili2db import get_ws_wn_ids
from ..utils.ili2db import add_to_selection
from ..utils.ili2db import remove_from_selection
from ..utils.various import logger
from .model_abwasser import get_abwasser_model
from .model_qgep import get_qgep_model
Expand All @@ -35,19 +40,40 @@ def qgep_export(selection=None, labels_file=None, orientation=None):
# backport from tww https://github.com/teksi/wastewater/blob/3acfba249866d299f8a22e249d9f1e475fe7b88d/plugin/teksi_wastewater/interlis/interlis_model_mapping/interlis_exporter_to_intermediate_schema.py#L83
abwasser_session.execute(text("SET CONSTRAINTS ALL DEFERRED;"))

# Filtering

# 1. Filtering - check if selection
filtered = selection is not None
subset_ids = selection if selection is not None else []

# get list of id's of class wwtp_structure (ARABauwerk) to be able to check if fk_wastewater_structure references to wwtp_structure
# 2. check if wwtp_structures exist

wastewater_structure_id_sia405abwasser_list = None
wastewater_structure_id_sia405abwasser_list = skip_wwtp_structure_ids()
wwt_structures_id_sia405abwasser_list = None
wwt_structures_id_sia405abwasser_list = get_ws_wn_ids('wwtp_structures')

logger.info(
f"wastewater_structure_id_sia405abwasser_list : {wastewater_structure_id_sia405abwasser_list}",
# 3. Show wwt_structures_id_sia405abwasser_list
logger.debug(
f"wwt_structures_id_sia405abwasser_list : {wwt_structures_id_sia405abwasser_list}",
)

# 4. check if filtered
if filtered then:
if wwt_structures_id_sia405abwasser_list then:
# take out wwt_structures_id_sia405abwasser_list from selection
subset_ids = remove_from_selection (subset_ids, get_ws_wn_ids('wwt_structures')
else:
# do nothing
else:
if wwt_structures_id_sia405abwasser_list then:
# add all data except wwt_structures to selection
subset_ids = add_to_selection (subset_ids, get_ws_wn_ids('wastewater_structure')
# take out wwt_structures_id_sia405abwasser_list from selection
subset_ids = remove_from_selection (subset_ids, wwt_structures_id_sia405abwasser_list)
# add reach_ids
subset_ids = add_to_selection (subset_ids, get_cl_re_ids('channel')
filtered = True
else:
# do nothing

# Orientation
oriented = orientation is not None
if oriented:
Expand Down
102 changes: 101 additions & 1 deletion qgepqwat2ili/utils/ili2db.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ def check_fk_provider_null():
return check_fk_provider_null


def skip_wwtp_structure_ids():
def skip_wwtp_structure_ids_old():
"""
Get list of id's of class wastewater_structure without wwtp_structure (ARABauwerk)
"""
Expand Down Expand Up @@ -489,6 +489,106 @@ def skip_wwtp_structure_ids():
return not_wwtp_structure_ids


def get_cl_re_ids(classname):
"""
Get list of id's of reaches of the channels provided
"""

# define classes that this is allowed to use - adapt for TWW to include model changes
if classname IN ('channel')
logger.info(f"get list of id's of wastewater_nodes of {classname} ...")

connection = psycopg2.connect(get_pgconf_as_psycopg2_dsn())
connection.set_session(autocommit=True)
cursor = connection.cursor()

cl_re_ids = []

# select all obj_id of the wastewater_nodes of wwtp_structure
cursor.execute(
"SELECT wn.obj_id FROM qgep_od.channel LEFT JOIN qgep_od.wastewater_networkelement wn ON wn.fk_wastewater_structure = channel.obj_id;"
)

# cursor.fetchall() - see https://pynative.com/python-cursor-fetchall-fetchmany-fetchone-to-read-rows-from-table/
# cl_re_ids_count = int(cursor.fetchone()[0])
# if cl_re_ids_count == 0:
if cursor.fetchone() is None:
cl_re_ids = None
else:
records = cursor.fetchall()
for row in records:
logger.debug(f" row[0] = {row[0]}")
# https://www.pythontutorial.net/python-string-methods/python-string-concatenation/
strrow = str(row[0])
cl_re_ids.append(strrow)
logger.debug(f" building up '{cl_re_ids}' ...")

return cl_re_ids
else
logger.warning(f"Do not use this function with {classname} !")
return None

def get_ws_wn_ids(classname):
"""
Get list of id's of wastewater_nodes of the wastewater_structure (sub)class provided, eg. wwtp_structure (ARABauwerk, does not work for channel
"""

# define classes that this is allowed to use - adapt for TWW to include model changes
if classname IN ('discharge_point', 'manhole', 'infiltration_installation', 'wastewater_structure')
logger.info(f"get list of id's of wastewater_nodes of {classname} ..."):
connection = psycopg2.connect(get_pgconf_as_psycopg2_dsn())
connection.set_session(autocommit=True)
cursor = connection.cursor()

ws_wn_ids = []

# select all obj_id of the wastewater_nodes of wwtp_structure
cursor.execute(
"SELECT wn.obj_id FROM qgep_od.{classname} LEFT JOIN qgep_od.wastewater_networkelement wn ON wn.fk_wastewater_structure = {classname}.obj_id;"
)

# cursor.fetchall() - see https://pynative.com/python-cursor-fetchall-fetchmany-fetchone-to-read-rows-from-table/
# ws_wn_ids_count = int(cursor.fetchone()[0])
# if ws_wn_ids_count == 0:
if cursor.fetchone() is None:
ws_wn_ids = None
else:
records = cursor.fetchall()
for row in records:
logger.debug(f" row[0] = {row[0]}")
# https://www.pythontutorial.net/python-string-methods/python-string-concatenation/
strrow = str(row[0])
ws_wn_ids.append(strrow)
logger.debug(f" building up '{ws_wn_ids}' ...")

return ws_wn_ids
else
logger.warning(f"Do not use this function with {classname} !")
return None


def remove_from_selection (selected_ids, remove_ids)
"""
Remove ids from selected_ids
"""
for list_item in remove_ids:
selected_ids = selected_ids.append(list_item)
return selected_ids
def add_to_selection (selected_ids, add_ids)
"""
Remove ids from selected_ids
"""
for list_item in add_ids:
selected_ids = selected_ids.add(list_item)
return selected_ids
def create_ili_schema(schema, model, log_path, recreate_schema=False):
"""
Create schema for INTERLIS import
Expand Down

0 comments on commit 14a441c

Please sign in to comment.