Skip to content

Commit

Permalink
Avoid duplicate GADM entries in lookup table.
Browse files Browse the repository at this point in the history
  • Loading branch information
MattBlissett committed Sep 27, 2023
1 parent f81b011 commit 46c234b
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions database/gadm_indices.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,28 @@ UPDATE gadm3 SET fulltext_search_3 =

CREATE EXTENSION IF NOT EXISTS hstore;

-- DISTINCT ON to avoid duplicate entries
CREATE MATERIALIZED VIEW gadm_region AS
SELECT DISTINCT
SELECT DISTINCT ON(gid_0)
gid_0 AS gid, name_0 AS name, string_to_array(varname_0, '|') AS variant_name, NULL::text[] AS non_latin_name, ARRAY['Country or area'] AS type, ARRAY['Country or area'] AS english_type,
0 AS gadm_level, NULL::text[] AS top_levels, NULL::hstore AS top_levels_map, NULL AS parent_gid, fulltext_search_0 AS fulltext_search FROM gadm3 WHERE gid_0 IS NOT NULL
0 AS gadm_level, NULL::text[] AS top_levels, NULL::hstore AS top_levels_map, NULL AS parent_gid, fulltext_search_0 AS fulltext_search
FROM gadm3
WHERE gid_0 IS NOT NULL
UNION ALL
SELECT DISTINCT
SELECT DISTINCT ON(gid_1)
gid_1 AS gid, name_1 AS name, string_to_array(varname_1, '|') AS variant_name, string_to_array(nl_name_1, '|') AS non_latin_name, string_to_array(type_1, '|') AS type, string_to_array(engtype_1, '|') AS english_type,
1 AS gadm_level, ARRAY[gid_0] AS top_levels, hstore(gid_0, name_0) AS top_levels_map, gid_0 AS parent_gid, fulltext_search_1 AS fulltext_search FROM gadm3 WHERE gid_1 IS NOT NULL
1 AS gadm_level, ARRAY[gid_0] AS top_levels, hstore(gid_0, name_0) AS top_levels_map, gid_0 AS parent_gid, fulltext_search_1 AS fulltext_search
FROM gadm3
WHERE gid_1 IS NOT NULL
UNION ALL
SELECT DISTINCT
SELECT DISTINCT ON(gid_2)
gid_2 AS gid, name_2 AS name, string_to_array(varname_2, '|') AS variant_name, string_to_array(nl_name_2, '|') AS non_latin_name, string_to_array(type_2, '|') AS type, string_to_array(engtype_2, '|') AS english_type,
2 AS gadm_level, ARRAY[gid_0, gid_1] AS top_levels, hstore(gid_0,name_0) || hstore(gid_1, name_1) AS top_levels_map, gid_1 AS parent_gid, fulltext_search_2 AS fulltext_search FROM gadm3 WHERE gid_2 IS NOT NULL
2 AS gadm_level, ARRAY[gid_0, gid_1] AS top_levels, hstore(gid_0,name_0) || hstore(gid_1, name_1) AS top_levels_map, gid_1 AS parent_gid, fulltext_search_2 AS fulltext_search
FROM gadm3
WHERE gid_2 IS NOT NULL
UNION ALL
SELECT DISTINCT
SELECT DISTINCT ON(gid_3)
gid_3 AS gid, name_3 AS name, string_to_array(varname_3, '|') AS variant_name, string_to_array(nl_name_3, '|') AS non_latin_name, string_to_array(type_3, '|') AS type, string_to_array(engtype_3, '|') AS english_type,
3 AS gadm_level, ARRAY[gid_0, gid_1, gid_2] AS top_levels, hstore(gid_0,name_0) || hstore(gid_1, name_1) || hstore(gid_2, name_2) AS top_levels_map, gid_2 AS parent_gid, fulltext_search_3 AS fulltext_search FROM gadm3 WHERE gid_3 IS NOT NULL;
3 AS gadm_level, ARRAY[gid_0, gid_1, gid_2] AS top_levels, hstore(gid_0,name_0) || hstore(gid_1, name_1) || hstore(gid_2, name_2) AS top_levels_map, gid_2 AS parent_gid, fulltext_search_3 AS fulltext_search
FROM gadm3
WHERE gid_3 IS NOT NULL;

0 comments on commit 46c234b

Please sign in to comment.