dataservices-api/server/lib/python/cartodb_geocoder/example/server_func_example.sql

28 lines
1.1 KiB
MySQL
Raw Normal View History

2015-11-07 00:50:54 +08:00
CREATE OR REPLACE
FUNCTION geocode_admin0(search text, tx_id bigint, user_id name)
RETURNS Geometry AS
$$
import logging
2015-11-10 20:48:52 +08:00
from cartodb_geocoder import quota_service
2015-11-07 00:50:54 +08:00
LOG_FILENAME = '/tmp/plpython.log'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
2015-11-09 23:43:10 +08:00
if user_id in SD and tx_id in SD[user_id] and 'redis_connection' in SD[user_id][tx_id]:
logging.debug("Using redis cached connection...")
2015-11-10 23:54:47 +08:00
qs = quota_service.QuotaService(user_id, tx_id, redis_connection=SD[user_id][tx_id]['redis_connection'])
2015-11-09 23:43:10 +08:00
else:
2015-11-10 23:54:47 +08:00
qs = quota_service.QuotaService(user_id, tx_id, redis_host='localhost', redis_port=6379, redis_db=5)
2015-11-09 23:43:10 +08:00
2015-11-07 00:50:54 +08:00
if qs.check_user_quota():
result = plpy.execute("SELECT geom FROM geocode_admin0_polygons(Array[\'{0}\']::text[])".format(search))
if result.status() == 5 and result.nrows() == 1:
2015-11-10 23:32:53 +08:00
qs.increment_geocoder_use()
2015-11-10 23:49:27 +08:00
SD[user_id] = {tx_id: {'redis_connection': qs.user_service.redis_connection}}
2015-11-07 00:50:54 +08:00
return result[0]["geom"]
else:
raise Exception('Something wrong with the georefence operation')
else:
raise Exception('Not enough quota for this user')
2015-11-09 23:43:10 +08:00
2015-11-07 00:50:54 +08:00
$$ LANGUAGE plpythonu;