2015-11-11 21:05:17 +08:00
|
|
|
-- Interfacess of the server extension
|
2015-11-11 20:33:14 +08:00
|
|
|
|
2015-11-24 20:30:18 +08:00
|
|
|
---- cdb_geocode_admin1_polygon(admin1_name text)
|
2015-12-03 18:03:10 +08:00
|
|
|
CREATE OR REPLACE FUNCTION cdb_geocoder_server.cdb_geocode_admin1_polygon(username text, orgname text, admin1_name text)
|
2015-11-11 20:33:14 +08:00
|
|
|
RETURNS Geometry AS $$
|
2015-11-24 20:30:18 +08:00
|
|
|
plpy.debug('Entering cdb_geocode_admin1_polygon(admin1_name text)')
|
2015-12-03 01:54:49 +08:00
|
|
|
plpy.debug('user = %s' % username)
|
2015-11-11 20:33:14 +08:00
|
|
|
|
|
|
|
#--TODO: rate limiting check
|
|
|
|
#--TODO: quota check
|
|
|
|
|
|
|
|
#-- Copied from the doc, see http://www.postgresql.org/docs/9.4/static/plpython-database.html
|
2015-11-24 20:30:18 +08:00
|
|
|
plan = plpy.prepare("SELECT cdb_geocoder_server._cdb_geocode_admin1_polygon($1) AS mypolygon", ["text"])
|
2015-11-11 20:33:14 +08:00
|
|
|
rv = plpy.execute(plan, [admin1_name], 1)
|
|
|
|
|
2015-11-24 20:30:18 +08:00
|
|
|
plpy.debug('Returning from Returning from cdb_geocode_admin1_polygons')
|
2015-11-11 20:33:14 +08:00
|
|
|
return rv[0]["mypolygon"]
|
|
|
|
$$ LANGUAGE plpythonu;
|
|
|
|
|
2015-11-24 20:30:18 +08:00
|
|
|
---- cdb_geocode_admin1_polygon(admin1_name text, country_name text)
|
2015-12-03 18:03:10 +08:00
|
|
|
CREATE OR REPLACE FUNCTION cdb_geocoder_server.cdb_geocode_admin1_polygon(username text, orgname text, admin1_name text, country_name text)
|
2015-11-11 21:05:17 +08:00
|
|
|
RETURNS Geometry AS $$
|
2015-11-24 20:30:18 +08:00
|
|
|
plpy.debug('Entering cdb_geocode_admin1_polygon(admin1_name text, country_name text)')
|
2015-12-03 01:54:49 +08:00
|
|
|
plpy.debug('user = %s' % username)
|
2015-11-11 21:05:17 +08:00
|
|
|
|
|
|
|
#--TODO: rate limiting check
|
|
|
|
#--TODO: quota check
|
|
|
|
|
|
|
|
#-- Copied from the doc, see http://www.postgresql.org/docs/9.4/static/plpython-database.html
|
2015-11-24 20:30:18 +08:00
|
|
|
plan = plpy.prepare("SELECT cdb_geocoder_server._cdb_geocode_admin1_polygon($1, $2) AS mypolygon", ["text", "text"])
|
2015-11-11 21:05:17 +08:00
|
|
|
rv = plpy.execute(plan, [admin1_name, country_name], 1)
|
|
|
|
|
2015-11-24 20:30:18 +08:00
|
|
|
plpy.debug('Returning from Returning from cdb_geocode_admin1_polygon(admin1_name text, country_name text)')
|
2015-11-11 21:05:17 +08:00
|
|
|
return rv[0]["mypolygon"]
|
|
|
|
$$ LANGUAGE plpythonu;
|
2015-11-11 20:33:14 +08:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Implementation of the server extension
|
|
|
|
-- Note: these functions depend on the cdb_geocoder extension
|
2015-11-11 21:05:17 +08:00
|
|
|
|
2015-11-24 20:30:18 +08:00
|
|
|
---- cdb_geocode_admin1_polygon(admin1_name text)
|
|
|
|
CREATE OR REPLACE FUNCTION cdb_geocoder_server._cdb_geocode_admin1_polygon(admin1_name text)
|
2015-11-11 20:33:14 +08:00
|
|
|
RETURNS Geometry AS $$
|
|
|
|
DECLARE
|
|
|
|
ret Geometry;
|
|
|
|
BEGIN
|
|
|
|
SELECT geom INTO ret
|
|
|
|
FROM (
|
|
|
|
SELECT q, (
|
2015-11-11 23:30:34 +08:00
|
|
|
SELECT the_geom
|
2015-11-11 20:33:14 +08:00
|
|
|
FROM global_province_polygons
|
2015-11-11 23:30:34 +08:00
|
|
|
WHERE d.c = ANY (synonyms)
|
2015-11-11 20:33:14 +08:00
|
|
|
ORDER BY frequency DESC LIMIT 1
|
|
|
|
) geom
|
|
|
|
FROM (
|
2015-11-11 23:30:34 +08:00
|
|
|
SELECT
|
2015-11-11 20:33:14 +08:00
|
|
|
trim(replace(lower(admin1_name),'.',' ')) c, admin1_name q
|
|
|
|
) d
|
|
|
|
) v;
|
|
|
|
|
|
|
|
RETURN ret;
|
|
|
|
END
|
|
|
|
$$ LANGUAGE plpgsql;
|
2015-11-11 21:05:17 +08:00
|
|
|
|
2015-11-24 20:30:18 +08:00
|
|
|
---- cdb_geocode_admin1_polygon(admin1_name text, country_name text)
|
|
|
|
CREATE OR REPLACE FUNCTION cdb_geocoder_server._cdb_geocode_admin1_polygon(admin1_name text, country_name text)
|
2015-11-11 21:05:17 +08:00
|
|
|
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 (
|
2015-11-11 23:30:34 +08:00
|
|
|
SELECT
|
2015-11-11 21:05:17 +08:00
|
|
|
q, (
|
2015-11-11 23:30:34 +08:00
|
|
|
SELECT the_geom
|
2015-11-11 21:05:17 +08:00
|
|
|
FROM global_province_polygons
|
2015-11-11 23:30:34 +08:00
|
|
|
WHERE p.c = ANY (synonyms)
|
2015-11-11 21:05:17 +08:00
|
|
|
AND iso3 = p.i
|
|
|
|
ORDER BY frequency DESC LIMIT 1
|
|
|
|
) geom
|
|
|
|
FROM p) n;
|
|
|
|
|
|
|
|
RETURN ret;
|
|
|
|
END
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
|