Adding a new function
This commit is contained in:
36
server/extension/expected/40_admin1_test.out
Normal file
36
server/extension/expected/40_admin1_test.out
Normal file
@@ -0,0 +1,36 @@
|
||||
-- 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)
|
||||
|
||||
SELECT cdb_geocoder_server.geocode_admin1_polygons(session_user, txid_current(), 'California', 'United States');
|
||||
geocode_admin1_polygons
|
||||
-------------------------
|
||||
|
||||
(1 row)
|
||||
|
||||
-- Insert dummy data into country decoder table
|
||||
INSERT INTO country_decoder (synonyms, iso3) VALUES (Array['united states'], 'USA');
|
||||
-- Insert some dummy data and geometry to return
|
||||
INSERT INTO global_province_polygons (synonyms, iso3, the_geom) VALUES (Array['california'], 'USA', 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
|
||||
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
0103000020E61000000100000004000000D0EA37A29AC651C00FD603035B284540FEFCFB379AC651C0C0503E9F5B284540FFDDDD4D96C651C033AC3B284F284540D0EA37A29AC651C00FD603035B284540
|
||||
(1 row)
|
||||
|
||||
SELECT cdb_geocoder_server.geocode_admin1_polygons(session_user, txid_current(), 'California', 'United States');
|
||||
geocode_admin1_polygons
|
||||
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
0103000020E61000000100000004000000D0EA37A29AC651C00FD603035B284540FEFCFB379AC651C0C0503E9F5B284540FFDDDD4D96C651C033AC3B284F284540D0EA37A29AC651C00FD603035B284540
|
||||
(1 row)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
-- Interface of the server extension
|
||||
-- Interfacess of the server extension
|
||||
|
||||
---- geocode_admin1_polygons(admin1_name text)
|
||||
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('Entering geocode_admin1_polygons(admin1_name text)')
|
||||
plpy.debug('user_id = %s' % user_id)
|
||||
|
||||
#-- Access control
|
||||
@@ -21,11 +22,34 @@ RETURNS Geometry AS $$
|
||||
return rv[0]["mypolygon"]
|
||||
$$ LANGUAGE plpythonu;
|
||||
|
||||
---- geocode_admin1_polygons(admin1_name text, country_name text)
|
||||
CREATE OR REPLACE FUNCTION geocode_admin1_polygons(user_id name, tx_id bigint, admin1_name text, country_name text)
|
||||
RETURNS Geometry AS $$
|
||||
plpy.debug('Entering geocode_admin1_polygons(admin1_name text, country_name text)')
|
||||
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, $2) AS mypolygon", ["text", "text"])
|
||||
rv = plpy.execute(plan, [admin1_name, country_name], 1)
|
||||
|
||||
plpy.debug('Returning from Returning from geocode_admin1_polygons(admin1_name text, country_name text)')
|
||||
return rv[0]["mypolygon"]
|
||||
$$ LANGUAGE plpythonu;
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
-- Implementation of the server extension
|
||||
-- Note: these functions depend on the cdb_geocoder extension
|
||||
|
||||
---- geocode_admin1_polygons(admin1_name text)
|
||||
CREATE OR REPLACE FUNCTION _geocode_admin1_polygons(admin1_name text)
|
||||
RETURNS Geometry AS $$
|
||||
DECLARE
|
||||
@@ -48,3 +72,28 @@ RETURNS Geometry AS $$
|
||||
RETURN ret;
|
||||
END
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
---- geocode_admin1_polygons(admin1_name text, country_name text)
|
||||
CREATE OR REPLACE FUNCTION _geocode_admin1_polygons(admin1_name text, country_name text)
|
||||
RETURNS Geometry AS $$
|
||||
DECLARE
|
||||
ret Geometry;
|
||||
BEGIN
|
||||
WITH p AS (SELECT r.c, r.q, (SELECT iso3 FROM country_decoder WHERE lower(country_name) = ANY (synonyms)) i FROM (SELECT trim(replace(lower(admin1_name),'.',' ')) c, country_name q) r)
|
||||
SELECT
|
||||
geom INTO ret
|
||||
FROM (
|
||||
SELECT
|
||||
q, (
|
||||
SELECT the_geom
|
||||
FROM global_province_polygons
|
||||
WHERE p.c = ANY (synonyms)
|
||||
AND iso3 = p.i
|
||||
ORDER BY frequency DESC LIMIT 1
|
||||
) geom
|
||||
FROM p) n;
|
||||
|
||||
RETURN ret;
|
||||
END
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
-- 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');
|
||||
SELECT cdb_geocoder_server.geocode_admin1_polygons(session_user, txid_current(), 'California', 'United States');
|
||||
|
||||
-- Insert dummy data into country decoder table
|
||||
INSERT INTO country_decoder (synonyms, iso3) VALUES (Array['united states'], 'USA');
|
||||
|
||||
-- Insert some dummy data and geometry to return
|
||||
INSERT INTO global_province_polygons (synonyms, the_geom) VALUES (Array['Califonia'], ST_GeomFromText(
|
||||
INSERT INTO global_province_polygons (synonyms, iso3, the_geom) VALUES (Array['california'], 'USA', ST_GeomFromText(
|
||||
'POLYGON((-71.1031880899493 42.3152774590236,
|
||||
-71.1031627617667 42.3152960829043,
|
||||
-71.102923838298 42.3149156848307,
|
||||
@@ -12,3 +16,9 @@ INSERT INTO global_province_polygons (synonyms, the_geom) VALUES (Array['Califon
|
||||
|
||||
-- This should return the polygon inserted above
|
||||
SELECT cdb_geocoder_server.geocode_admin1_polygons(session_user, txid_current(), 'California');
|
||||
SELECT cdb_geocoder_server.geocode_admin1_polygons(session_user, txid_current(), 'California', 'United States');
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user