From b05b25d1a0c4a431436e065e8c4e5192fafad5a1 Mon Sep 17 00:00:00 2001 From: Travis Grigsby Date: Wed, 4 May 2022 16:06:44 -0700 Subject: [PATCH] split lookup depending on whether it's a country (#2075) --- data/functions.sql | 36 +++++++++++++++++---------------- queries/planet_osm_point.jinja2 | 2 +- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/data/functions.sql b/data/functions.sql index 626d0c4fd..4af7d07fd 100644 --- a/data/functions.sql +++ b/data/functions.sql @@ -996,7 +996,7 @@ $$ LANGUAGE plpgsql IMMUTABLE; -- returns a JSONB object containing __ne_min_zoom and __ne_max_zoom set to the -- label min and max zoom of any matching row from the Natural Earth countries, -- map units and states/provinces themes. -CREATE OR REPLACE FUNCTION tz_get_ne_min_max_zoom(wikidata_id TEXT) +CREATE OR REPLACE FUNCTION tz_get_ne_min_max_zoom(wikidata_id TEXT, place_tag TEXT) RETURNS JSONB AS $$ DECLARE min_zoom REAL; @@ -1006,28 +1006,30 @@ BEGIN RETURN '{}'::jsonb; END IF; - -- first, try the countries table - SELECT - min_label, max_label INTO min_zoom, max_zoom - FROM ne_10m_admin_0_countries c - WHERE c.wikidataid = wikidata_id; + -- if it's a country, only look it up in the iso and tlc tables + IF place_tag='country' OR place_tag='unrecognized' THEN + SELECT + min_label, max_label INTO min_zoom, max_zoom + FROM ne_10m_admin_0_countries_iso i + WHERE i.wikidataid = wikidata_id; - -- if that fails, try map_units (which contains some sub-country but super- - -- state level stuff such as England, Scotland and Wales). - IF NOT FOUND THEN - SELECT - min_label, max_label INTO min_zoom, max_zoom - FROM ne_10m_admin_0_map_units mu - WHERE mu.wikidataid = wikidata_id; + IF NOT FOUND THEN + SELECT + min_label, max_label INTO min_zoom, max_zoom + FROM ne_10m_admin_0_countries_tlc t + WHERE t.wikidataid = wikidata_id; + END IF; + + IF NOT FOUND THEN + RETURN '{}'::jsonb; + END IF; END IF; - -- try states and provinces - IF NOT FOUND THEN - SELECT + -- try states and provinces if it's not a country + SELECT min_label, max_label INTO min_zoom, max_zoom FROM ne_10m_admin_1_states_provinces sp WHERE sp.wikidataid = wikidata_id; - END IF; -- finally, try localities -- There is no concept of max_zoom for ne_10m_populated_places diff --git a/queries/planet_osm_point.jinja2 b/queries/planet_osm_point.jinja2 index 5cb48f0c4..2e9e332ec 100644 --- a/queries/planet_osm_point.jinja2 +++ b/queries/planet_osm_point.jinja2 @@ -27,7 +27,7 @@ SELECT THEN jsonb_build_object( 'min_zoom', mz_places_min_zoom ) || - tz_get_ne_min_max_zoom(tags->'wikidata') + tz_get_ne_min_max_zoom(tags->'wikidata', tags->'place') || tz_get_ne_pop_min_max(tags->'wikidata') END AS __places_properties__,