From 4a30ec28b9f5be3851289d1eb0bf1fe5228e5b6b Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Fri, 10 Apr 2020 11:05:11 +0200 Subject: [PATCH 1/1] move helper functions from placex_triggers into utils Also adds documentation for these functions. --- sql/functions/placex_triggers.sql | 40 ---------------------- sql/functions/utils.sql | 57 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 40 deletions(-) diff --git a/sql/functions/placex_triggers.sql b/sql/functions/placex_triggers.sql index c04aef28..19367db4 100644 --- a/sql/functions/placex_triggers.sql +++ b/sql/functions/placex_triggers.sql @@ -1,45 +1,5 @@ -- Trigger functions for the placex table. -CREATE OR REPLACE FUNCTION get_rel_node_members(members TEXT[], memberLabels TEXT[]) - RETURNS SETOF BIGINT - AS $$ -DECLARE - i INTEGER; -BEGIN - FOR i IN 1..ARRAY_UPPER(members,1) BY 2 LOOP - IF members[i+1] = ANY(memberLabels) - AND upper(substring(members[i], 1, 1))::char(1) = 'N' - THEN - RETURN NEXT substring(members[i], 2)::bigint; - END IF; - END LOOP; - - RETURN; -END; -$$ -LANGUAGE plpgsql IMMUTABLE; - --- copy 'name' to or from the default language (if there is a default language) -CREATE OR REPLACE FUNCTION add_default_place_name(country_code VARCHAR(2), - INOUT name HSTORE) - AS $$ -DECLARE - default_language VARCHAR(10); -BEGIN - IF name is not null AND array_upper(akeys(name),1) > 1 THEN - default_language := get_country_language_code(country_code); - IF default_language IS NOT NULL THEN - IF name ? 'name' AND NOT name ? ('name:'||default_language) THEN - name := name || hstore(('name:'||default_language), (name -> 'name')); - ELSEIF name ? ('name:'||default_language) AND NOT name ? 'name' THEN - name := name || hstore('name', (name -> ('name:'||default_language))); - END IF; - END IF; - END IF; -END; -$$ -LANGUAGE plpgsql IMMUTABLE; - -- Find the parent road of a POI. -- -- \returns Place ID of parent object or NULL if none diff --git a/sql/functions/utils.sql b/sql/functions/utils.sql index 6371e9ad..ccb6a203 100644 --- a/sql/functions/utils.sql +++ b/sql/functions/utils.sql @@ -38,6 +38,63 @@ END; $$ LANGUAGE plpgsql IMMUTABLE; +-- Return the node members with a given label from a relation member list +-- as a set. +-- +-- \param members Member list in osm2pgsql middle format. +-- \param memberLabels Array of labels to accept. +-- +-- \returns Set of OSM ids of nodes that are found. +-- +CREATE OR REPLACE FUNCTION get_rel_node_members(members TEXT[], + memberLabels TEXT[]) + RETURNS SETOF BIGINT + AS $$ +DECLARE + i INTEGER; +BEGIN + FOR i IN 1..ARRAY_UPPER(members,1) BY 2 LOOP + IF members[i+1] = ANY(memberLabels) + AND upper(substring(members[i], 1, 1))::char(1) = 'N' + THEN + RETURN NEXT substring(members[i], 2)::bigint; + END IF; + END LOOP; + + RETURN; +END; +$$ +LANGUAGE plpgsql IMMUTABLE; + +-- Copy 'name' to or from the default language. +-- +-- \param country_code Country code of the object being named. +-- \param[inout] name List of names of the object. +-- +-- If the country named by country_code has a single default language, +-- then a `name` tag is copied to `name:` if this tag does +-- not yet exist and vice versa. +CREATE OR REPLACE FUNCTION add_default_place_name(country_code VARCHAR(2), + INOUT name HSTORE) + AS $$ +DECLARE + default_language VARCHAR(10); +BEGIN + IF name is not null AND array_upper(akeys(name),1) > 1 THEN + default_language := get_country_language_code(country_code); + IF default_language IS NOT NULL THEN + IF name ? 'name' AND NOT name ? ('name:'||default_language) THEN + name := name || hstore(('name:'||default_language), (name -> 'name')); + ELSEIF name ? ('name:'||default_language) AND NOT name ? 'name' THEN + name := name || hstore('name', (name -> ('name:'||default_language))); + END IF; + END IF; + END IF; +END; +$$ +LANGUAGE plpgsql IMMUTABLE; + + CREATE OR REPLACE FUNCTION reverse_place_diameter(rank_search SMALLINT) RETURNS FLOAT -- 2.39.5