132 lines
5.5 KiB
PL/PgSQL
132 lines
5.5 KiB
PL/PgSQL
---- cdb_geocode_admin1_polygon(admin1_name text)
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_admin1_polygon(username text, orgname text, admin1_name text)
|
|
RETURNS Geometry AS $$
|
|
from cartodb_services.metrics import metrics
|
|
from cartodb_services.metrics import QuotaService
|
|
from cartodb_services.metrics import InternalGeocoderConfig
|
|
from cartodb_services.tools import Logger,LoggerConfig
|
|
|
|
plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username))
|
|
redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection']
|
|
plpy.execute("SELECT cdb_dataservices_server._get_internal_geocoder_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname)))
|
|
user_geocoder_config = GD["user_internal_geocoder_config_{0}".format(username)]
|
|
|
|
plpy.execute("SELECT cdb_dataservices_server._get_logger_config()")
|
|
logger_config = GD["logger_config"]
|
|
logger = Logger(logger_config)
|
|
quota_service = QuotaService(user_geocoder_config, redis_conn)
|
|
|
|
params = {'username': username, 'orgname': orgname, 'admin1_name': admin1_name}
|
|
|
|
with metrics('cdb_geocode_admin1_polygon', user_geocoder_config, logger, params):
|
|
try:
|
|
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_admin1_polygon(trim($1)) AS mypolygon", ["text"])
|
|
rv = plpy.execute(plan, [admin1_name], 1)
|
|
result = rv[0]["mypolygon"]
|
|
if result:
|
|
quota_service.increment_success_service_use()
|
|
return result
|
|
else:
|
|
quota_service.increment_empty_service_use()
|
|
return None
|
|
except BaseException as e:
|
|
import sys
|
|
quota_service.increment_failed_service_use()
|
|
logger.error('Error trying to geocode admin1 polygon', sys.exc_info(), data={"username": username, "orgname": orgname})
|
|
raise Exception('Error trying to geocode admin1 polygon')
|
|
finally:
|
|
quota_service.increment_total_service_use()
|
|
$$ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED;
|
|
|
|
---- cdb_geocode_admin1_polygon(admin1_name text, country_name text)
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_admin1_polygon(username text, orgname text, admin1_name text, country_name text)
|
|
RETURNS Geometry AS $$
|
|
from cartodb_services.metrics import metrics
|
|
from cartodb_services.metrics import QuotaService
|
|
from cartodb_services.metrics import InternalGeocoderConfig
|
|
from cartodb_services.tools import Logger,LoggerConfig
|
|
|
|
plpy.execute("SELECT cdb_dataservices_server._connect_to_redis('{0}')".format(username))
|
|
redis_conn = GD["redis_connection_{0}".format(username)]['redis_metrics_connection']
|
|
plpy.execute("SELECT cdb_dataservices_server._get_internal_geocoder_config({0}, {1})".format(plpy.quote_nullable(username), plpy.quote_nullable(orgname)))
|
|
user_geocoder_config = GD["user_internal_geocoder_config_{0}".format(username)]
|
|
|
|
plpy.execute("SELECT cdb_dataservices_server._get_logger_config()")
|
|
logger_config = GD["logger_config"]
|
|
logger = Logger(logger_config)
|
|
quota_service = QuotaService(user_geocoder_config, redis_conn)
|
|
|
|
with metrics('cdb_geocode_admin1_polygon', user_geocoder_config, logger):
|
|
try:
|
|
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_admin1_polygon(trim($1), trim($2)) AS mypolygon", ["text", "text"])
|
|
rv = plpy.execute(plan, [admin1_name, country_name], 1)
|
|
result = rv[0]["mypolygon"]
|
|
if result:
|
|
quota_service.increment_success_service_use()
|
|
return result
|
|
else:
|
|
quota_service.increment_empty_service_use()
|
|
return None
|
|
except BaseException as e:
|
|
import sys
|
|
quota_service.increment_failed_service_use()
|
|
logger.error('Error trying to geocode admin1 polygon', sys.exc_info(), data={"username": username, "orgname": orgname})
|
|
raise Exception('Error trying to geocode admin1 polygon')
|
|
finally:
|
|
quota_service.increment_total_service_use()
|
|
$$ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED;
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Implementation of the server extension
|
|
-- Note: these functions depend on the cdb_geocoder extension
|
|
|
|
---- cdb_geocode_admin1_polygon(admin1_name text)
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_geocode_admin1_polygon(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 STABLE PARALLEL SAFE;
|
|
|
|
---- cdb_geocode_admin1_polygon(admin1_name text, country_name text)
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_geocode_admin1_polygon(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 STABLE PARALLEL SAFE;
|
|
|