Skip to content

Commit

Permalink
Merge pull request #149 from hotosm/hotfix/outside_intersection
Browse files Browse the repository at this point in the history
Hotfix : Update Country as 0 if not intersected
  • Loading branch information
kshitijrajsharma authored Sep 6, 2023
2 parents 28cd0b6 + c2460c0 commit d2306e0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
10 changes: 4 additions & 6 deletions backend/field_update
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ class Database:
# catch exception for invalid SQL statement

try:

self.cursor.execute(query)
self.conn.commit()
# print(query)
Expand Down Expand Up @@ -180,7 +179,6 @@ class Raw:
):
"""Function that updates column of table"""
if insert_type.lower() == "array":

select_query = f"""WITH
t1 AS (
SELECT
Expand All @@ -194,10 +192,10 @@ class Raw:
t2 AS (
SELECT
t1.osm_id,
array_agg(cg.{source_column}) AS aa_fids
COALESCE(array_agg(cg.{source_column}),ARRAY[0]::integer[]) AS aa_fids
FROM
t1
INNER JOIN
LEFT JOIN
{source_table} cg ON ST_Intersects(t1.geom, cg.{source_geom})
GROUP BY
t1.osm_id
Expand All @@ -223,13 +221,13 @@ class Raw:
update
{target_table} as wp
SET
{target_column} = (
{target_column} = COALESCE((
select
cu.{source_column}
from {source_table} cu
where ST_Intersects(tf.{target_geom},cu.{source_geom})
LIMIT 1
)::int
)::int,0::int)
from table_filtered tf
where
wp.osm_id = tf.osm_id"""
Expand Down
4 changes: 2 additions & 2 deletions backend/replication
Original file line number Diff line number Diff line change
Expand Up @@ -534,15 +534,15 @@ def update_country(conn, timestamp, table_name, c_id=None, boundary=None):
update_query = f"""update
{table_name} as w
set
country = array((
country = COALESCE(array((
select
b.id
from
countries b
where
ST_Intersects( ST_Centroid(w.geom) ,
b.geometry)
))
)),ARRAY[0]::integer[])
where "timestamp" >= '{timestamp}'"""
cur.execute(update_query)
LOG.info(
Expand Down
19 changes: 19 additions & 0 deletions backend/sql/post_indexes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,31 @@ CREATE INDEX IF NOT EXISTS ways_poly_geom_idx ON public.ways_poly USING gist (g

CREATE INDEX IF NOT EXISTS relations_geom_idx ON public.relations USING gist (geom);
CREATE INDEX IF NOT EXISTS relations_country_idx ON public.relations USING gin (country gin__int_ops);
CREATE INDEX IF NOT EXISTS relations_tags_idx ON public.relations USING gin (tags);

-- External Indexes

-- CREATE INDEX IF NOT EXISTS nodes_uid_idx ON public.nodes USING btree (uid);
-- CREATE INDEX IF NOT EXISTS nodes_changeset_idx ON public.nodes USING btree (changeset);

-- CREATE INDEX IF NOT EXISTS ways_line_uid_idx ON public.ways_line USING btree (uid);
-- CREATE INDEX IF NOT EXISTS ways_line_changeset_idx ON public.ways_line USING btree (changeset);

-- CREATE INDEX IF NOT EXISTS ways_poly_uid_idx ON public.ways_poly USING btree (uid);
-- CREATE INDEX IF NOT EXISTS ways_poly_changeset_idx ON public.ways_poly USING btree (changeset);

-- CREATE INDEX IF NOT EXISTS relations_uid_idx ON public.relations USING btree (uid);
-- CREATE INDEX IF NOT EXISTS relations_changeset_idx ON public.relations USING btree (changeset);

-- clustering nodes
CLUSTER nodes USING nodes_geom_idx;
-- clustering ways_line
CLUSTER ways_line USING ways_line_geom_idx;
-- clustering ways_poly
CLUSTER ways_poly USING ways_poly_geom_idx;
-- clustering relations
CLUSTER relations USING relations_geom_idx;



-- VACUUM the table to reclaim disk space
Expand All @@ -58,3 +75,5 @@ ANALYZE nodes;
ANALYZE ways_line;
ANALYZE ways_poly;
ANALYZE relations;


14 changes: 0 additions & 14 deletions backend/sql/pre_indexes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@
-- # 1100 13th Street NW Suite 800 Washington, D.C. 20005
-- # <[email protected]>

-- CREATE INDEX IF NOT EXISTS nodes_uid_idx ON public.nodes USING btree (uid);
-- CREATE INDEX IF NOT EXISTS nodes_changeset_idx ON public.nodes USING btree (changeset);

-- CREATE INDEX IF NOT EXISTS ways_line_uid_idx ON public.ways_line USING btree (uid);
-- CREATE INDEX IF NOT EXISTS ways_line_changeset_idx ON public.ways_line USING btree (changeset);

-- CREATE INDEX IF NOT EXISTS ways_poly_uid_idx ON public.ways_poly USING btree (uid);
-- CREATE INDEX IF NOT EXISTS ways_poly_changeset_idx ON public.ways_poly USING btree (changeset);

-- CREATE INDEX IF NOT EXISTS relations_uid_idx ON public.relations USING btree (uid);
-- CREATE INDEX IF NOT EXISTS relations_changeset_idx ON public.relations USING btree (changeset);


CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE EXTENSION IF NOT EXISTS postgis;
Expand All @@ -53,8 +41,6 @@ CREATE INDEX IF NOT EXISTS ways_line_timestamp_idx ON public.ways_line USING btr

CREATE INDEX IF NOT EXISTS ways_poly_timestamp_idx ON public.ways_poly USING btree ("timestamp");

CREATE INDEX IF NOT EXISTS relations_tags_idx ON public.relations USING gin (tags);

CREATE INDEX IF NOT EXISTS relations_timestamp_idx ON public.relations USING btree ("timestamp");


Expand Down
2 changes: 1 addition & 1 deletion src/query_builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_grid_id_query(geometry_dump):

def get_country_id_query(geom_dump):
base_query = f"""select
b.id::int as fid
COALESCE(b.id::int, 0) as fid
from
countries b
where
Expand Down

0 comments on commit d2306e0

Please sign in to comment.