Skip to content

Commit

Permalink
Add coalesce to replication
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Sep 10, 2023
1 parent 1d40dfd commit 4a215a8
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion backend/replication
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def update_country(conn, timestamp, table_name, c_id=None, boundary=None):
"""Updates country column in table , It intersects it's newly inserted data's geometry to the boundaries loaded to underpass and populates info , in order to use multicolumn indexes with geometry"""
# array value 1000 means rest of the world
with conn.cursor() as cur:
update_query = f"""WITH t1 AS (SELECT osm_id, ST_Centroid(geom) AS geom FROM {table_name} WHERE country = '{{}}'), t2 AS (SELECT t1.osm_id, array_agg(cg.id) AS aa_fids FROM t1 INNER 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;"""
update_query = f"""WITH t1 AS (SELECT osm_id, ST_Centroid(geom) AS geom FROM {table_name} WHERE country = '{{}}'), t2 AS (SELECT t1.osm_id, COALESCE(array_agg(cg.id),ARRAY[0]::integer[]) 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;"""

cur.execute(update_query)
conn.commit()
Expand Down

0 comments on commit 4a215a8

Please sign in to comment.