From 4e23bfaa3aa4e4066a236e75ae30c0a849ea9120 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Tue, 10 Nov 2015 12:36:53 +0100 Subject: [PATCH 1/2] Add server functions for country geocoder #6 --- server/extension/sql/10_admin0.sql | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 server/extension/sql/10_admin0.sql diff --git a/server/extension/sql/10_admin0.sql b/server/extension/sql/10_admin0.sql new file mode 100644 index 0000000..03122e6 --- /dev/null +++ b/server/extension/sql/10_admin0.sql @@ -0,0 +1,41 @@ +-- Interface of the server extension + +CREATE OR REPLACE FUNCTION geocode_admin0_polygons(user_name name, country_name text) +RETURNS Geometry AS $$ + plpy.debug('Entering geocode_admin0_polygons') + plpy.debug('user_name = %s' % user_name) + + #-- Access control + #-- TODO: this should be part of cdb python library + if user_name == 'publicuser': + plpy.error('The api_key must be provided') + + #--TODO: rate limiting check + #--TODO: quota check + + #-- Copied from the doc, see http://www.postgresql.org/docs/9.4/static/plpython-database.html + plan = plpy.prepare("SELECT _geocode_admin0_polygons($1) AS mypolygon", ["text"]) + rv = plpy.execute(plan, [country_name], 1) + + plpy.debug('Returning from Returning from geocode_admin0_polygons') + return rv[0]["mypolygon"] +$$ LANGUAGE plpythonu; + + +-------------------------------------------------------------------------------- + +-- Implementation of the server extension + +CREATE OR REPLACE FUNCTION _geocode_admin0_polygons(country_name text) +RETURNS Geometry AS $$ + DECLARE + ret Geometry; + BEGIN + SELECT n.the_geom as geom INTO ret + FROM (SELECT q, lower(regexp_replace(q, '[^a-zA-Z\u00C0-\u00ff]+', '', 'g'))::text x + FROM (SELECT country_name q) g) d + LEFT OUTER JOIN admin0_synonyms s ON name_ = d.x + LEFT OUTER JOIN ne_admin0_v3 n ON s.adm0_a3 = n.adm0_a3 GROUP BY d.q, n.the_geom, s.adm0_a3; + RETURN ret; + END +$$ LANGUAGE plpgsql; From 08aac5a362326ad98e73918184c91b0635f886cc Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Tue, 10 Nov 2015 15:33:06 +0100 Subject: [PATCH 2/2] Add tx_id (transaction id) param --- server/extension/sql/10_admin0.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/extension/sql/10_admin0.sql b/server/extension/sql/10_admin0.sql index 03122e6..ca6da1f 100644 --- a/server/extension/sql/10_admin0.sql +++ b/server/extension/sql/10_admin0.sql @@ -1,6 +1,6 @@ -- Interface of the server extension -CREATE OR REPLACE FUNCTION geocode_admin0_polygons(user_name name, country_name text) +CREATE OR REPLACE FUNCTION geocode_admin0_polygons(user_id name, rx_id bigint, country_name text) RETURNS Geometry AS $$ plpy.debug('Entering geocode_admin0_polygons') plpy.debug('user_name = %s' % user_name)