2018-09-10 18:54:37 +08:00
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_point(username text, orgname text, code text)
|
2016-03-07 20:03:09 +08:00
|
|
|
RETURNS Geometry AS $$
|
2016-10-29 00:12:04 +08:00
|
|
|
from cartodb_services.metrics import metrics
|
2016-08-03 23:37:40 +08:00
|
|
|
from cartodb_services.metrics import QuotaService
|
|
|
|
from cartodb_services.metrics import InternalGeocoderConfig
|
|
|
|
from cartodb_services.tools import Logger,LoggerConfig
|
2016-03-07 20:03:09 +08:00
|
|
|
|
2016-08-03 23:37:40 +08:00
|
|
|
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)]
|
2016-03-07 20:03:09 +08:00
|
|
|
|
2016-08-03 23:37:40 +08:00
|
|
|
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)
|
2018-09-06 19:47:50 +08:00
|
|
|
|
2018-09-10 18:54:37 +08:00
|
|
|
params = {'username': username, 'orgname': orgname, 'code': code}
|
2018-09-06 19:47:50 +08:00
|
|
|
|
|
|
|
with metrics('cdb_geocode_postalcode_point', user_geocoder_config, logger, params):
|
2016-10-29 00:12:04 +08:00
|
|
|
try:
|
|
|
|
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_postalcode_point(trim($1)) AS mypoint", ["text"])
|
|
|
|
rv = plpy.execute(plan, [code], 1)
|
|
|
|
result = rv[0]["mypoint"]
|
|
|
|
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 postal code point', sys.exc_info(), data={"username": username, "orgname": orgname})
|
|
|
|
raise Exception('Error trying to geocode postal code point')
|
|
|
|
finally:
|
|
|
|
quota_service.increment_total_service_use()
|
2017-11-08 00:23:05 +08:00
|
|
|
$$ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED;
|
2016-03-07 20:03:09 +08:00
|
|
|
|
2018-09-10 18:54:37 +08:00
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_point(username text, orgname text, code double precision)
|
2017-07-06 00:29:49 +08:00
|
|
|
RETURNS Geometry AS $$
|
2018-09-10 18:54:37 +08:00
|
|
|
SELECT cdb_dataservices_server.cdb_geocode_postalcode_point(username, orgname, code::text);
|
2017-11-08 00:23:05 +08:00
|
|
|
$$ LANGUAGE SQL STABLE PARALLEL RESTRICTED;
|
2017-07-06 00:29:49 +08:00
|
|
|
|
2017-07-08 01:03:16 +08:00
|
|
|
|
2018-09-10 18:54:37 +08:00
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_point(username text, orgname text, code text, country text)
|
2016-03-07 20:03:09 +08:00
|
|
|
RETURNS Geometry AS $$
|
2016-10-29 00:12:04 +08:00
|
|
|
from cartodb_services.metrics import metrics
|
2016-08-03 23:37:40 +08:00
|
|
|
from cartodb_services.metrics import QuotaService
|
|
|
|
from cartodb_services.metrics import InternalGeocoderConfig
|
|
|
|
from cartodb_services.tools import Logger,LoggerConfig
|
2016-03-07 20:03:09 +08:00
|
|
|
|
2016-08-03 23:37:40 +08:00
|
|
|
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)]
|
2016-03-07 20:03:09 +08:00
|
|
|
|
2016-08-03 23:37:40 +08:00
|
|
|
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)
|
2016-11-02 16:58:35 +08:00
|
|
|
with metrics('cdb_geocode_postalcode_point', user_geocoder_config, logger):
|
2016-10-29 00:12:04 +08:00
|
|
|
try:
|
|
|
|
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_postalcode_point(trim($1), trim($2)) AS mypoint", ["TEXT", "TEXT"])
|
|
|
|
rv = plpy.execute(plan, [code, country], 1)
|
|
|
|
result = rv[0]["mypoint"]
|
|
|
|
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 postal code point', sys.exc_info(), data={"username": username, "orgname": orgname})
|
|
|
|
raise Exception('Error trying to geocode postal code point')
|
|
|
|
finally:
|
|
|
|
quota_service.increment_total_service_use()
|
2017-11-08 00:23:05 +08:00
|
|
|
$$ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED;
|
2016-03-07 20:03:09 +08:00
|
|
|
|
2018-09-10 18:54:37 +08:00
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_point(username text, orgname text, code double precision, country text)
|
2017-07-06 00:29:49 +08:00
|
|
|
RETURNS Geometry AS $$
|
2018-09-10 18:54:37 +08:00
|
|
|
SELECT cdb_dataservices_server.cdb_geocode_postalcode_point(username, orgname, code::text, country);
|
2017-11-08 00:23:05 +08:00
|
|
|
$$ LANGUAGE SQL STABLE PARALLEL RESTRICTED;
|
2017-07-06 00:29:49 +08:00
|
|
|
|
2017-07-08 01:03:16 +08:00
|
|
|
|
2018-09-10 18:54:37 +08:00
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_polygon(username text, orgname text, code text)
|
2016-03-07 20:03:09 +08:00
|
|
|
RETURNS Geometry AS $$
|
2016-10-29 00:12:04 +08:00
|
|
|
from cartodb_services.metrics import metrics
|
2016-08-03 23:37:40 +08:00
|
|
|
from cartodb_services.metrics import QuotaService
|
|
|
|
from cartodb_services.metrics import InternalGeocoderConfig
|
|
|
|
from cartodb_services.tools import Logger,LoggerConfig
|
2016-03-07 20:03:09 +08:00
|
|
|
|
2016-08-03 23:37:40 +08:00
|
|
|
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)]
|
2016-03-07 20:03:09 +08:00
|
|
|
|
2016-08-03 23:37:40 +08:00
|
|
|
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)
|
2016-11-02 16:58:35 +08:00
|
|
|
with metrics('cdb_geocode_postalcode_point', user_geocoder_config, logger):
|
2016-10-29 00:12:04 +08:00
|
|
|
try:
|
|
|
|
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_postalcode_polygon(trim($1)) AS mypolygon", ["text"])
|
|
|
|
rv = plpy.execute(plan, [code], 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 postal code polygon', sys.exc_info(), data={"username": username, "orgname": orgname})
|
|
|
|
raise Exception('Error trying to geocode postal code polygon')
|
|
|
|
finally:
|
|
|
|
quota_service.increment_total_service_use()
|
2017-11-08 00:23:05 +08:00
|
|
|
$$ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED;
|
2016-03-07 20:03:09 +08:00
|
|
|
|
2018-09-10 18:54:37 +08:00
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_polygon(username text, orgname text, code double precision)
|
2017-07-06 00:29:49 +08:00
|
|
|
RETURNS Geometry AS $$
|
2018-09-10 18:54:37 +08:00
|
|
|
SELECT cdb_dataservices_server.cdb_geocode_postalcode_polygon(username, orgname, code::text)
|
2017-11-08 00:23:05 +08:00
|
|
|
$$ LANGUAGE SQL STABLE PARALLEL RESTRICTED;
|
2017-07-06 00:29:49 +08:00
|
|
|
|
2017-07-08 01:03:16 +08:00
|
|
|
|
|
|
|
|
2018-09-10 18:54:37 +08:00
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_polygon(username text, orgname text, code text, country text)
|
2016-03-07 20:03:09 +08:00
|
|
|
RETURNS Geometry AS $$
|
2016-10-29 00:12:04 +08:00
|
|
|
from cartodb_services.metrics import metrics
|
2016-08-03 23:37:40 +08:00
|
|
|
from cartodb_services.metrics import QuotaService
|
|
|
|
from cartodb_services.metrics import InternalGeocoderConfig
|
|
|
|
from cartodb_services.tools import Logger,LoggerConfig
|
2016-03-07 20:03:09 +08:00
|
|
|
|
2016-08-03 23:37:40 +08:00
|
|
|
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)]
|
2016-03-07 20:03:09 +08:00
|
|
|
|
2016-08-03 23:37:40 +08:00
|
|
|
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)
|
2016-11-02 16:58:35 +08:00
|
|
|
with metrics('cdb_geocode_postalcode_point', user_geocoder_config, logger):
|
2016-10-29 00:12:04 +08:00
|
|
|
try:
|
|
|
|
plan = plpy.prepare("SELECT cdb_dataservices_server._cdb_geocode_postalcode_polygon(trim($1), trim($2)) AS mypolygon", ["TEXT", "TEXT"])
|
|
|
|
rv = plpy.execute(plan, [code, country], 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 postal code polygon', sys.exc_info(), data={"username": username, "orgname": orgname})
|
|
|
|
raise Exception('Error trying to geocode postal code polygon')
|
|
|
|
finally:
|
|
|
|
quota_service.increment_total_service_use()
|
2017-11-08 00:23:05 +08:00
|
|
|
$$ LANGUAGE plpythonu STABLE PARALLEL RESTRICTED;
|
2016-03-07 20:03:09 +08:00
|
|
|
|
2018-09-10 18:54:37 +08:00
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_geocode_postalcode_polygon(username text, orgname text, code double precision, country text)
|
2017-07-08 01:03:16 +08:00
|
|
|
RETURNS Geometry AS $$
|
2018-09-10 18:54:37 +08:00
|
|
|
SELECT cdb_dataservices_server.cdb_geocode_postalcode_polygon(username, orgname, code::text, country);
|
2017-11-08 00:23:05 +08:00
|
|
|
$$ LANGUAGE SQL STABLE PARALLEL RESTRICTED;
|
2017-07-08 01:03:16 +08:00
|
|
|
|
2016-03-07 20:03:09 +08:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Implementation of the server extension
|
|
|
|
-- Note: these functions depend on the cdb_geocoder extension
|
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_geocode_postalcode_point(code text)
|
|
|
|
RETURNS Geometry AS $$
|
|
|
|
DECLARE
|
|
|
|
ret Geometry;
|
|
|
|
BEGIN
|
|
|
|
SELECT geom INTO ret
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
q, (
|
|
|
|
SELECT the_geom
|
|
|
|
FROM global_postal_code_points
|
|
|
|
WHERE postal_code = upper(d.q)
|
|
|
|
LIMIT 1
|
|
|
|
) geom
|
|
|
|
FROM (SELECT code q) d
|
|
|
|
) v;
|
|
|
|
|
|
|
|
RETURN ret;
|
|
|
|
END
|
2017-11-08 00:23:05 +08:00
|
|
|
$$ LANGUAGE plpgsql STABLE PARALLEL SAFE;
|
2016-03-07 20:03:09 +08:00
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_geocode_postalcode_point(code text, country text)
|
|
|
|
RETURNS Geometry AS $$
|
|
|
|
DECLARE
|
|
|
|
ret Geometry;
|
|
|
|
BEGIN
|
|
|
|
SELECT geom INTO ret
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
q, (
|
|
|
|
SELECT the_geom
|
|
|
|
FROM global_postal_code_points
|
|
|
|
WHERE postal_code = upper(d.q)
|
|
|
|
AND iso3 = (
|
|
|
|
SELECT iso3 FROM country_decoder WHERE
|
|
|
|
lower(country) = ANY (synonyms) LIMIT 1
|
|
|
|
)
|
|
|
|
LIMIT 1
|
|
|
|
) geom
|
|
|
|
FROM (SELECT code q) d
|
|
|
|
) v;
|
|
|
|
|
|
|
|
RETURN ret;
|
|
|
|
END
|
2017-11-08 00:23:05 +08:00
|
|
|
$$ LANGUAGE plpgsql STABLE PARALLEL SAFE;
|
2016-03-07 20:03:09 +08:00
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_geocode_postalcode_polygon(code text)
|
|
|
|
RETURNS Geometry AS $$
|
|
|
|
DECLARE
|
|
|
|
ret Geometry;
|
|
|
|
BEGIN
|
|
|
|
SELECT geom INTO ret
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
q, (
|
|
|
|
SELECT the_geom
|
|
|
|
FROM global_postal_code_polygons
|
|
|
|
WHERE postal_code = upper(d.q)
|
|
|
|
LIMIT 1
|
|
|
|
) geom
|
|
|
|
FROM (SELECT code q) d
|
|
|
|
) v;
|
|
|
|
|
|
|
|
RETURN ret;
|
|
|
|
END
|
2017-11-08 00:23:05 +08:00
|
|
|
$$ LANGUAGE plpgsql STABLE PARALLEL SAFE;
|
2016-03-07 20:03:09 +08:00
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION cdb_dataservices_server._cdb_geocode_postalcode_polygon(code text, country text)
|
|
|
|
RETURNS Geometry AS $$
|
|
|
|
DECLARE
|
|
|
|
ret Geometry;
|
|
|
|
BEGIN
|
|
|
|
SELECT geom INTO ret
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
q, (
|
|
|
|
SELECT the_geom
|
|
|
|
FROM global_postal_code_polygons
|
|
|
|
WHERE postal_code = upper(d.q)
|
|
|
|
AND iso3 = (
|
|
|
|
SELECT iso3 FROM country_decoder WHERE
|
|
|
|
lower(country) = ANY (synonyms) LIMIT 1
|
|
|
|
)
|
|
|
|
LIMIT 1
|
|
|
|
) geom
|
|
|
|
FROM (SELECT code q) d
|
|
|
|
) v;
|
|
|
|
|
|
|
|
RETURN ret;
|
|
|
|
END
|
2017-11-08 00:23:05 +08:00
|
|
|
$$ LANGUAGE plpgsql STABLE PARALLEL SAFE;
|