From b810954396fe2b6a1cb2ae3802eddaf57a1e9490 Mon Sep 17 00:00:00 2001 From: Travis Grigsby Date: Wed, 4 May 2022 16:19:59 -0700 Subject: [PATCH] return fclass_iso, fclass_tlc, and label_x, label_y for OSM features with matching NE features (#2082) --- data/functions.sql | 59 +++++++++++++++++++++++++++++++++ queries/planet_osm_point.jinja2 | 2 ++ 2 files changed, 61 insertions(+) diff --git a/data/functions.sql b/data/functions.sql index 4af7d07fd..80dbf16ad 100644 --- a/data/functions.sql +++ b/data/functions.sql @@ -1053,6 +1053,65 @@ BEGIN END; $$ LANGUAGE plpgsql STABLE; +CREATE OR REPLACE FUNCTION tz_get_fclass_and_label_position(wikidata_id TEXT, place_tag TEXT) +RETURNS JSONB AS $$ +DECLARE +fclass_iso TEXT; +fclass_tlc TEXT; +label_x REAL; +label_y REAL; +BEGIN + IF wikidata_id IS NULL THEN + RETURN '{}'::jsonb; +END IF; + + -- 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 + fclass_iso, fclass_tlc, label_x, label_y INTO fclass_iso, fclass_tlc, label_x, label_y + FROM ne_10m_admin_0_countries_iso i + WHERE i.wikidataid = wikidata_id; + + IF NOT FOUND THEN + SELECT + fclass_iso, fclass_tlc, label_x, label_y INTO fclass_iso, fclass_tlc, label_x, label_y + 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; + + -- There is no label_x and label_y for the non-countries (please confirm @nkelso!) + SELECT + fclass_iso, fclass_tlc, NULL, NULL INTO fclass_iso, fclass_tlc, label_x, label_y + FROM ne_10m_admin_1_states_provinces sp + WHERE sp.wikidataid = wikidata_id; + + -- finally, try localities + IF NOT FOUND THEN + SELECT + fclass_iso, fclass_tlc, NULL, NULL INTO fclass_iso, fclass_tlc, label_x, label_y + FROM ne_10m_populated_places pp + WHERE pp.wikidataid = wikidata_id;END IF; + + -- return an empty JSONB rather than null, so that it can be safely + -- concatenated with whatever other JSONB rather than needing a check for + -- null. + IF NOT FOUND THEN + RETURN '{}'::jsonb; + END IF; + RETURN jsonb_build_object( + '__ne_fclass_iso', fclass_iso, + '__ne_fclass_tlc', fclass_tlc, + '__ne_label_x', label_x, + '__ne_label_y', label_y + ); +END; +$$ LANGUAGE plpgsql STABLE; + -- returns a JSONB object containing __ne_pop_min and __ne_pop_max CREATE OR REPLACE FUNCTION tz_get_ne_pop_min_max(wikidata_id TEXT) diff --git a/queries/planet_osm_point.jinja2 b/queries/planet_osm_point.jinja2 index 2e9e332ec..4b5e3dc05 100644 --- a/queries/planet_osm_point.jinja2 +++ b/queries/planet_osm_point.jinja2 @@ -30,6 +30,8 @@ SELECT tz_get_ne_min_max_zoom(tags->'wikidata', tags->'place') || tz_get_ne_pop_min_max(tags->'wikidata') + || + tz_get_fclass_and_label_position(tags->'wikidata', tags->'place') END AS __places_properties__, CASE WHEN mz_poi_min_zoom IS NOT NULL