Skip to content

Commit

Permalink
check for empty list added
Browse files Browse the repository at this point in the history
  • Loading branch information
sjib committed Dec 12, 2024
1 parent 8eb5752 commit 2d71f2b
Showing 1 changed file with 79 additions and 76 deletions.
155 changes: 79 additions & 76 deletions qgepqwat2ili/utils/qgep_export_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,101 +852,104 @@ def get_connected_we_from_re(subset_reaches):


# 10.12.2024
def get_connected_overflow_to_wn_ids(selected_ids):
def get_connected_we_to_re(subset_reaches):
"""
Get all connected wastewater_nodes from overflows.fk_overflow_to
Get connected wastewater_networkelements (wastewater_nodes and reaches) to subset of reaches
"""
if selected_ids is None:
connected_overflow_to_wn_ids = None
else:
logger.info(
f"Get all connected wastewater_nodes from overflows.fk_overflow_to {selected_ids} ..."
)
connection = psycopg2.connect(get_pgconf_as_psycopg2_dsn())
connection.set_session(autocommit=True)
cursor = connection.cursor()

connected_overflow_to_wn_ids = []
if subset_reaches is None:
connected_wn_to_re_ids = None
if not subset_reaches:
connected_wn_to_re_ids = None
else:
logger.info(
f"get list of id's of connected wastewater_nodes of provides subset of reaches {subset_reaches} ..."
)
connection = psycopg2.connect(get_pgconf_as_psycopg2_dsn())
connection.set_session(autocommit=True)
cursor = connection.cursor()

subset_text = get_selection_text_for_in_statement(selected_ids)
connected_wn_to_re_ids = []

# select all connected to wastewater_nodes from provided subset of reaches
cursor.execute(
f"SELECT ov.fk_overflow_to FROM qgep_od.wastewater_node wn LEFT JOIN qgep_od.overflow ov ON wn.obj_id = ov.fk_wastewater_node WHERE wn.obj_id IN ({subset_text}) AND NOT ov.fk_overflow_to isNULL;"
)
subset_reaches_text = get_selection_text_for_in_statement(subset_reaches)

# 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:
connected_overflow_to_wn_ids = None
else:
# added cursor.execute again to see if with this all records will be available
# 15.11.2024 added - see https://stackoverflow.com/questions/58101874/cursor-fetchall-or-other-method-fetchone-is-not-working
# select all connected to wastewater_nodes from provided subset of reaches
cursor.execute(
f"SELECT ov.fk_overflow_to FROM qgep_od.wastewater_node wn LEFT JOIN qgep_od.overflow ov ON wn.obj_id = ov.fk_wastewater_node WHERE wn.obj_id IN ({subset_text}) AND NOT ov.fk_overflow_to isNULL;"
f"SELECT wet.obj_id as wet_obj_id FROM qgep_od.reach re LEFT JOIN qgep_od.reach_point rpt ON rpt.obj_id = re.fk_reach_point_to LEFT JOIN qgep_od.wastewater_networkelement wet ON wet.obj_id = rpt.fk_wastewater_networkelement WHERE re.obj_id IN ({subset_reaches_text}) AND NOT wet.obj_id isNull;"
)
records = cursor.fetchall()

# 15.11.2024 - does not get all records, but only n-1
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])
if strrow is not None:
connected_overflow_to_wn_ids.append(strrow)
# logger.debug(f" building up '{connected_overflow_to_wn_ids}' ...")
logger.info(f" connected_overflow_to_wn_ids: '{connected_overflow_to_wn_ids}'")
return connected_overflow_to_wn_ids
# 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:
connected_wn_to_re_ids = None
else:
# added cursor.execute again to see if with this all records will be available
# 15.11.2024 added - see https://stackoverflow.com/questions/58101874/cursor-fetchall-or-other-method-fetchone-is-not-working
cursor.execute(
f"SELECT wet.obj_id as wet_obj_id FROM qgep_od.reach re LEFT JOIN qgep_od.reach_point rpt ON rpt.obj_id = re.fk_reach_point_to LEFT JOIN qgep_od.wastewater_networkelement wet ON wet.obj_id = rpt.fk_wastewater_networkelement WHERE re.obj_id IN ({subset_reaches_text}) AND NOT wet.obj_id isNull;"
)
records = cursor.fetchall()

# 15.11.2024 - does not get all records, but only n-1
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])
if strrow is not None:
connected_wn_to_re_ids.append(strrow)
# logger.debug(f" building up '{connected_wn_to_re_ids}' ...")
logger.info(f" connected_wn_to_re_ids: '{connected_wn_to_re_ids}'")
return connected_wn_to_re_ids

# 10.12.2024
def get_connected_we_to_re(subset_reaches):
def get_connected_overflow_to_wn_ids(selected_ids):
"""
Get connected wastewater_networkelements (wastewater_nodes and reaches) to subset of reaches
Get all connected wastewater_nodes from overflows.fk_overflow_to
"""
if subset_reaches is None:
connected_wn_to_re_ids = None
else:
logger.info(
f"get list of id's of connected wastewater_nodes of provides subset of reaches {subset_reaches} ..."
)
connection = psycopg2.connect(get_pgconf_as_psycopg2_dsn())
connection.set_session(autocommit=True)
cursor = connection.cursor()

connected_wn_to_re_ids = []
if selected_ids is None:
connected_overflow_to_wn_ids = None
if not selected_ids:
connected_overflow_to_wn_ids = None
else:
logger.info(
f"Get all connected wastewater_nodes from overflows.fk_overflow_to {selected_ids} ..."
)
connection = psycopg2.connect(get_pgconf_as_psycopg2_dsn())
connection.set_session(autocommit=True)
cursor = connection.cursor()

subset_reaches_text = get_selection_text_for_in_statement(subset_reaches)
connected_overflow_to_wn_ids = []

# select all connected to wastewater_nodes from provided subset of reaches
cursor.execute(
f"SELECT wet.obj_id as wet_obj_id FROM qgep_od.reach re LEFT JOIN qgep_od.reach_point rpt ON rpt.obj_id = re.fk_reach_point_to LEFT JOIN qgep_od.wastewater_networkelement wet ON wet.obj_id = rpt.fk_wastewater_networkelement WHERE re.obj_id IN ({subset_reaches_text}) AND NOT wet.obj_id isNull;"
)
subset_text = get_selection_text_for_in_statement(selected_ids)

# 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:
connected_wn_to_re_ids = None
else:
# added cursor.execute again to see if with this all records will be available
# 15.11.2024 added - see https://stackoverflow.com/questions/58101874/cursor-fetchall-or-other-method-fetchone-is-not-working
# select all connected to wastewater_nodes from provided subset of reaches
cursor.execute(
f"SELECT wet.obj_id as wet_obj_id FROM qgep_od.reach re LEFT JOIN qgep_od.reach_point rpt ON rpt.obj_id = re.fk_reach_point_to LEFT JOIN qgep_od.wastewater_networkelement wet ON wet.obj_id = rpt.fk_wastewater_networkelement WHERE re.obj_id IN ({subset_reaches_text}) AND NOT wet.obj_id isNull;"
f"SELECT ov.fk_overflow_to FROM qgep_od.wastewater_node wn LEFT JOIN qgep_od.overflow ov ON wn.obj_id = ov.fk_wastewater_node WHERE wn.obj_id IN ({subset_text}) AND NOT ov.fk_overflow_to isNULL;"
)
records = cursor.fetchall()

# 15.11.2024 - does not get all records, but only n-1
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])
if strrow is not None:
connected_wn_to_re_ids.append(strrow)
# logger.debug(f" building up '{connected_wn_to_re_ids}' ...")
logger.info(f" connected_wn_to_re_ids: '{connected_wn_to_re_ids}'")
return connected_wn_to_re_ids
# 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:
connected_overflow_to_wn_ids = None
else:
# added cursor.execute again to see if with this all records will be available
# 15.11.2024 added - see https://stackoverflow.com/questions/58101874/cursor-fetchall-or-other-method-fetchone-is-not-working
cursor.execute(
f"SELECT ov.fk_overflow_to FROM qgep_od.wastewater_node wn LEFT JOIN qgep_od.overflow ov ON wn.obj_id = ov.fk_wastewater_node WHERE wn.obj_id IN ({subset_text}) AND NOT ov.fk_overflow_to isNULL;"
)
records = cursor.fetchall()

# 15.11.2024 - does not get all records, but only n-1
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])
if strrow is not None:
connected_overflow_to_wn_ids.append(strrow)
# logger.debug(f" building up '{connected_overflow_to_wn_ids}' ...")
logger.info(f" connected_overflow_to_wn_ids: '{connected_overflow_to_wn_ids}'")
return connected_overflow_to_wn_ids


def get_ws_wn_ids(classname):
Expand Down

0 comments on commit 2d71f2b

Please sign in to comment.