diff --git a/server/extension/expected/40_admin1_test.out b/server/extension/expected/40_admin1_test.out new file mode 100644 index 0000000..dc7a839 --- /dev/null +++ b/server/extension/expected/40_admin1_test.out @@ -0,0 +1,22 @@ +-- Check that the public function is callable, even with no data +-- It should return NULL +SELECT cdb_geocoder_server.geocode_admin1_polygons(session_user, txid_current(), 'California'); + geocode_admin1_polygons +------------------------- + +(1 row) + +-- Insert some dummy data and geometry to return +INSERT INTO global_province_polygons (synonyms, the_geom) VALUES (Array['Califonia'], ST_GeomFromText( + 'POLYGON((-71.1031880899493 42.3152774590236, + -71.1031627617667 42.3152960829043, + -71.102923838298 42.3149156848307, + -71.1031880899493 42.3152774590236))',4326) +); +-- This should return the polygon inserted above +SELECT cdb_geocoder_server.geocode_admin1_polygons(session_user, txid_current(), 'California'); + geocode_admin1_polygons +------------------------- + +(1 row) + diff --git a/server/extension/sql/0.0.1/40_admin1.sql b/server/extension/sql/0.0.1/40_admin1.sql new file mode 100644 index 0000000..c8d351d --- /dev/null +++ b/server/extension/sql/0.0.1/40_admin1.sql @@ -0,0 +1,50 @@ +-- Interface of the server extension + +CREATE OR REPLACE FUNCTION geocode_admin1_polygons(user_id name, tx_id bigint, admin1_name text) +RETURNS Geometry AS $$ + plpy.debug('Entering geocode_admin1_polygons') + plpy.debug('user_id = %s' % user_id) + + #-- Access control + #-- TODO: this should be part of cdb python library + if user_id == '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 cdb_geocoder_server._geocode_admin1_polygons($1) AS mypolygon", ["text"]) + rv = plpy.execute(plan, [admin1_name], 1) + + plpy.debug('Returning from Returning from geocode_admin1_polygons') + return rv[0]["mypolygon"] +$$ LANGUAGE plpythonu; + + +-------------------------------------------------------------------------------- + +-- Implementation of the server extension +-- Note: these functions depend on the cdb_geocoder extension +CREATE OR REPLACE FUNCTION _geocode_admin1_polygons(admin1_name text) +RETURNS Geometry AS $$ + DECLARE + ret Geometry; + BEGIN + SELECT geom INTO ret + FROM ( + SELECT q, ( + SELECT the_geom + FROM global_province_polygons + WHERE d.c = ANY (synonyms) + ORDER BY frequency DESC LIMIT 1 + ) geom + FROM ( + SELECT + trim(replace(lower(admin1_name),'.',' ')) c, admin1_name q + ) d + ) v; + + RETURN ret; + END +$$ LANGUAGE plpgsql; diff --git a/server/extension/sql/40_admin1_test.sql b/server/extension/sql/40_admin1_test.sql new file mode 100644 index 0000000..e96cbfc --- /dev/null +++ b/server/extension/sql/40_admin1_test.sql @@ -0,0 +1,14 @@ +-- Check that the public function is callable, even with no data +-- It should return NULL +SELECT cdb_geocoder_server.geocode_admin1_polygons(session_user, txid_current(), 'California'); + +-- Insert some dummy data and geometry to return +INSERT INTO global_province_polygons (synonyms, the_geom) VALUES (Array['Califonia'], ST_GeomFromText( + 'POLYGON((-71.1031880899493 42.3152774590236, + -71.1031627617667 42.3152960829043, + -71.102923838298 42.3149156848307, + -71.1031880899493 42.3152774590236))',4326) +); + +-- This should return the polygon inserted above +SELECT cdb_geocoder_server.geocode_admin1_polygons(session_user, txid_current(), 'California');