From 008a5e3579e6ae34705386ef46d3cc56d399709f Mon Sep 17 00:00:00 2001 From: "amaury.zarzelli" Date: Tue, 27 Feb 2024 10:43:43 +0100 Subject: [PATCH] change(pivot2pgr): do not use clean_graph() procedure --- changelog.md | 5 +++++ r2gg/__about__.py | 2 +- r2gg/_pivot_to_pgr.py | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index 2d20644..c5d0e45 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,11 @@ ## x.y.z +## 2.2.4 + +CHANGED: +- Pivot to pgr: Not using clean_grpah method anymore, but a SQL query + ## 2.2.3 CHANGED: diff --git a/r2gg/__about__.py b/r2gg/__about__.py index 425afd0..10db08c 100644 --- a/r2gg/__about__.py +++ b/r2gg/__about__.py @@ -34,7 +34,7 @@ __uri_tracker__ = f"{__uri_repository__}issues/" __uri__ = __uri_repository__ -__version__ = "2.2.3" +__version__ = "2.2.4" __version_info__ = tuple( [ int(num) if num.isdigit() else num diff --git a/r2gg/_pivot_to_pgr.py b/r2gg/_pivot_to_pgr.py index ded60ff..2d0686a 100644 --- a/r2gg/_pivot_to_pgr.py +++ b/r2gg/_pivot_to_pgr.py @@ -395,13 +395,43 @@ def pivot_to_pgr(source, cost_calculation_file_path, connection_work, connection for profile_name in profile_names: logger.info("Cleaning isolated edges for profile {}...".format(profile_name)) - clean_graph_query = "SELECT {0}.clean_graph('{1}')".format(schema, profile_name) + clean_graph_query = """ + WITH connected_components AS ( + SELECT * FROM pgr_connectedComponents( + 'SELECT id, source, target, cost_s_{1} as cost, reverse_cost_s_{1} as reverse_cost FROM {0}.ways' + ) + ), + remove_nodes AS ( + SELECT node + FROM + connected_components + WHERE + component = ANY( + SELECT DISTINCT component + FROM + ( + SELECT component, count(*) AS nb + FROM + connected_components + GROUP BY component + ) + AS components + WHERE nb <= 10 + ) + ) + UPDATE {0}.ways + SET cost_s_{1} = -1, + reverse_cost_s_{1} = -1, + cost_m_{1} = -1, + reverse_cost_m_{1} = -1 + WHERE {0}.ways.target = ANY(SELECT * from remove_nodes) OR {0}.ways.source = ANY(SELECT * from remove_nodes); + """.format(schema, profile_name) logger.info("SQL: {}".format(clean_graph_query)) cursor_isolated.execute(clean_graph_query) + connection_out.commit() et_execute = time.time() logger.info("Execution ended. Elapsed time : %s seconds." %(et_execute - st_execute)) - connection_out.commit() cursor_isolated.close() end_time = time.time()