From 9e5855db6a35459a52dc0fb7bdbda990cf4badd7 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Sun, 19 Nov 2023 17:33:35 +0545 Subject: [PATCH] Update replication and added sql --- backend/replication | 4 ++-- backend/sql/update_countries.sql | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 backend/sql/update_countries.sql diff --git a/backend/replication b/backend/replication index e731f9f6..1807d592 100644 --- a/backend/replication +++ b/backend/replication @@ -534,8 +534,8 @@ def update_country(conn, timestamp, table_name, c_id=None, boundary=None): with conn.cursor() as cur: update_query = f"""WITH t1 AS (SELECT osm_id, ST_Centroid(geom) AS geom FROM {table_name} WHERE "timestamp" >= '{timestamp}'), t2 AS (SELECT t1.osm_id, CASE - WHEN COUNT(cg.cid) = 0 THEN ARRAY[0]::integer[] - ELSE array_agg(COALESCE(cg.cid, 0)) + WHEN COUNT(cg.cid) = 0 THEN ARRAY[1000]::integer[] + ELSE array_agg(COALESCE(cg.cid, 1000)) END AS aa_fids FROM t1 LEFT JOIN countries cg ON ST_Intersects(t1.geom, cg.geometry) GROUP BY t1.osm_id) UPDATE {table_name} uw SET country = t2.aa_fids FROM t2 WHERE t2.osm_id = uw.osm_id;""" diff --git a/backend/sql/update_countries.sql b/backend/sql/update_countries.sql new file mode 100644 index 00000000..2f55a245 --- /dev/null +++ b/backend/sql/update_countries.sql @@ -0,0 +1,3 @@ +WITH t1 AS (SELECT osm_id, ST_Centroid(geom) AS geom FROM nodes wl WHERE country <@ Array[0]), +t2 AS (SELECT t1.osm_id, CASE WHEN COUNT(cg.cid) = 0 THEN ARRAY[1000]::INTEGER[] ELSE ARRAY_AGG(COALESCE(cg.cid, 1000)) END AS aa_fids FROM t1 LEFT JOIN countries cg ON ST_Intersects(t1.geom, cg.geometry) GROUP BY t1.osm_id) +UPDATE nodes uw SET country = t2.aa_fids FROM t2 WHERE t2.osm_id = uw.osm_id; \ No newline at end of file