2018-04-09 20:41:44 +08:00
- - DO NOT MODIFY THIS FILE , IT IS GENERATED AUTOMATICALLY FROM SOURCES
-- Complain if script is sourced in psql, rather than via CREATE EXTENSION
\ echo Use " ALTER EXTENSION cdb_dataservices_server UPDATE TO '0.31.0' " to load this file . \ quit
-- HERE goes your code to upgrade/downgrade
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _cdb_tomtom_route_with_waypoints (
username TEXT ,
orgname TEXT ,
waypoints geometry ( Point , 4326 ) [ ] ,
mode TEXT )
RETURNS cdb_dataservices_server . simple_route AS $ $
from cartodb_services . tools import ServiceManager
from cartodb_services . tomtom import TomTomRouting
from cartodb_services . tomtom . types import TRANSPORT_MODE_TO_TOMTOM
from cartodb_services . tools import Coordinate
from cartodb_services . tools . polyline import polyline_to_linestring
from cartodb_services . refactor . service . tomtom_routing_config import TomTomRoutingConfigBuilder
import cartodb_services
cartodb_services . init ( plpy , GD )
service_manager = ServiceManager ( ' routing ' , TomTomRoutingConfigBuilder , username , orgname , GD )
service_manager . assert_within_limits ( )
try :
client = TomTomRouting ( service_manager . config . tomtom_api_key , service_manager . logger , service_manager . config . service_params )
if not waypoints or len ( waypoints ) < 2 :
service_manager . logger . info ( " Empty origin or destination " )
service_manager . quota_service . increment_empty_service_use ( )
return [ None , None , None ]
if len ( waypoints ) > 25 :
service_manager . logger . info ( " Too many waypoints (max 25) " )
service_manager . quota_service . increment_empty_service_use ( )
return [ None , None , None ]
waypoint_coords = [ ]
for waypoint in waypoints :
lat = plpy . execute ( " SELECT ST_Y('%s') AS lat " % waypoint ) [ 0 ] [ ' lat ' ]
lon = plpy . execute ( " SELECT ST_X('%s') AS lon " % waypoint ) [ 0 ] [ ' lon ' ]
waypoint_coords . append ( Coordinate ( lon , lat ) )
profile = TRANSPORT_MODE_TO_TOMTOM . get ( mode )
resp = client . directions ( waypoint_coords , profile )
if resp and resp . shape :
shape_linestring = polyline_to_linestring ( resp . shape )
if shape_linestring :
service_manager . quota_service . increment_success_service_use ( )
return [ shape_linestring , resp . length , int ( round ( resp . duration ) ) ]
else :
service_manager . quota_service . increment_empty_service_use ( )
return [ None , None , None ]
else :
service_manager . quota_service . increment_empty_service_use ( )
return [ None , None , None ]
except BaseException as e :
import sys
service_manager . quota_service . increment_failed_service_use ( )
service_manager . logger . error ( ' Error trying to calculate TomTom routing ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to calculate TomTom routing ' )
finally :
service_manager . quota_service . increment_total_service_use ( )
2020-03-10 22:45:04 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ SECURITY DEFINER STABLE PARALLEL RESTRICTED ;
2018-04-09 20:41:44 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . cdb_route_point_to_point (
username TEXT ,
orgname TEXT ,
origin geometry ( Point , 4326 ) ,
destination geometry ( Point , 4326 ) ,
mode TEXT ,
options text [ ] DEFAULT ARRAY [ ] : : text [ ] ,
units text DEFAULT ' kilometers ' )
RETURNS cdb_dataservices_server . simple_route AS $ $
from cartodb_services . metrics import metrics
from cartodb_services . tools import Logger
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_routing_config({0}, {1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
user_routing_config = GD [ " user_routing_config_{0} " . format ( username ) ]
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
logger = Logger ( logger_config )
2018-04-27 18:43:29 +08:00
params = { ' origin ' : origin , ' destination ' : destination , ' mode ' : mode , ' options ' : options , ' units ' : units }
with metrics ( ' cdb_route_with_point ' , user_routing_config , logger , params ) :
2018-04-09 20:41:44 +08:00
waypoints = [ origin , destination ]
if user_routing_config . mapzen_provider :
mapzen_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server._cdb_mapzen_route_with_waypoints($1, $2, $3, $4) as route; " , [ " text " , " text " , " geometry(Point, 4326)[] " , " text " ] )
result = plpy . execute ( mapzen_plan , [ username , orgname , waypoints , mode ] )
return [ result [ 0 ] [ ' shape ' ] , result [ 0 ] [ ' length ' ] , result [ 0 ] [ ' duration ' ] ]
elif user_routing_config . mapbox_provider :
mapbox_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server._cdb_mapbox_route_with_waypoints($1, $2, $3, $4) as route; " , [ " text " , " text " , " geometry(Point, 4326)[] " , " text " ] )
result = plpy . execute ( mapbox_plan , [ username , orgname , waypoints , mode ] )
return [ result [ 0 ] [ ' shape ' ] , result [ 0 ] [ ' length ' ] , result [ 0 ] [ ' duration ' ] ]
elif user_routing_config . tomtom_provider :
tomtom_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server._cdb_tomtom_route_with_waypoints($1, $2, $3, $4) as route; " , [ " text " , " text " , " geometry(Point, 4326)[] " , " text " ] )
result = plpy . execute ( tomtom_plan , [ username , orgname , waypoints , mode ] )
return [ result [ 0 ] [ ' shape ' ] , result [ 0 ] [ ' length ' ] , result [ 0 ] [ ' duration ' ] ]
else :
raise Exception ( ' Requested routing method is not available ' )
2020-03-10 22:45:04 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ STABLE PARALLEL RESTRICTED ;
2018-04-09 20:41:44 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . cdb_route_with_waypoints (
username TEXT ,
orgname TEXT ,
waypoints geometry ( Point , 4326 ) [ ] ,
mode TEXT ,
options text [ ] DEFAULT ARRAY [ ] : : text [ ] ,
units text DEFAULT ' kilometers ' )
RETURNS cdb_dataservices_server . simple_route AS $ $
from cartodb_services . metrics import metrics
from cartodb_services . tools import Logger
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_routing_config({0}, {1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
user_routing_config = GD [ " user_routing_config_{0} " . format ( username ) ]
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
logger = Logger ( logger_config )
2018-04-27 18:43:29 +08:00
params = { ' waypoints ' : waypoints , ' mode ' : mode , ' options ' : options , ' units ' : units }
with metrics ( ' cdb_route_with_waypoints ' , user_routing_config , logger , params ) :
2018-04-09 20:41:44 +08:00
if user_routing_config . mapzen_provider :
mapzen_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server._cdb_mapzen_route_with_waypoints($1, $2, $3, $4) as route; " , [ " text " , " text " , " geometry(Point, 4326)[] " , " text " ] )
result = plpy . execute ( mapzen_plan , [ username , orgname , waypoints , mode ] )
return [ result [ 0 ] [ ' shape ' ] , result [ 0 ] [ ' length ' ] , result [ 0 ] [ ' duration ' ] ]
elif user_routing_config . mapbox_provider :
mapbox_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server._cdb_mapbox_route_with_waypoints($1, $2, $3, $4) as route; " , [ " text " , " text " , " geometry(Point, 4326)[] " , " text " ] )
result = plpy . execute ( mapbox_plan , [ username , orgname , waypoints , mode ] )
return [ result [ 0 ] [ ' shape ' ] , result [ 0 ] [ ' length ' ] , result [ 0 ] [ ' duration ' ] ]
elif user_routing_config . tomtom_provider :
tomtom_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server._cdb_tomtom_route_with_waypoints($1, $2, $3, $4) as route; " , [ " text " , " text " , " geometry(Point, 4326)[] " , " text " ] )
result = plpy . execute ( tomtom_plan , [ username , orgname , waypoints , mode ] )
return [ result [ 0 ] [ ' shape ' ] , result [ 0 ] [ ' length ' ] , result [ 0 ] [ ' duration ' ] ]
else :
raise Exception ( ' Requested routing method is not available ' )
2020-03-10 22:45:04 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ STABLE PARALLEL RESTRICTED ;
2018-04-09 20:41:44 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . cdb_geocode_street_point ( username TEXT , orgname TEXT , searchtext TEXT , city TEXT DEFAULT NULL , state_province TEXT DEFAULT NULL , country TEXT DEFAULT NULL )
RETURNS Geometry AS $ $
from cartodb_services . metrics import metrics
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_geocoder_config({0}, {1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
user_geocoder_config = GD [ " user_geocoder_config_{0} " . format ( username ) ]
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
logger = Logger ( logger_config )
2018-04-11 18:56:55 +08:00
params = { ' searchtext ' : searchtext , ' city ' : city , ' state_province ' : state_province , ' country ' : country }
with metrics ( ' cdb_geocode_street_point ' , user_geocoder_config , logger , params ) :
2018-04-09 20:41:44 +08:00
if user_geocoder_config . heremaps_geocoder :
here_plan = plpy . prepare ( " SELECT cdb_dataservices_server._cdb_here_geocode_street_point($1, $2, $3, $4, $5, $6) as point; " , [ " text " , " text " , " text " , " text " , " text " , " text " ] )
return plpy . execute ( here_plan , [ username , orgname , searchtext , city , state_province , country ] , 1 ) [ 0 ] [ ' point ' ]
elif user_geocoder_config . google_geocoder :
google_plan = plpy . prepare ( " SELECT cdb_dataservices_server._cdb_google_geocode_street_point($1, $2, $3, $4, $5, $6) as point; " , [ " text " , " text " , " text " , " text " , " text " , " text " ] )
return plpy . execute ( google_plan , [ username , orgname , searchtext , city , state_province , country ] , 1 ) [ 0 ] [ ' point ' ]
elif user_geocoder_config . mapzen_geocoder :
mapzen_plan = plpy . prepare ( " SELECT cdb_dataservices_server._cdb_mapzen_geocode_street_point($1, $2, $3, $4, $5, $6) as point; " , [ " text " , " text " , " text " , " text " , " text " , " text " ] )
return plpy . execute ( mapzen_plan , [ username , orgname , searchtext , city , state_province , country ] , 1 ) [ 0 ] [ ' point ' ]
elif user_geocoder_config . mapbox_geocoder :
mapbox_plan = plpy . prepare ( " SELECT cdb_dataservices_server._cdb_mapbox_geocode_street_point($1, $2, $3, $4, $5, $6) as point; " , [ " text " , " text " , " text " , " text " , " text " , " text " ] )
return plpy . execute ( mapbox_plan , [ username , orgname , searchtext , city , state_province , country ] , 1 ) [ 0 ] [ ' point ' ]
elif user_geocoder_config . tomtom_geocoder :
tomtom_plan = plpy . prepare ( " SELECT cdb_dataservices_server._cdb_tomtom_geocode_street_point($1, $2, $3, $4, $5, $6) as point; " , [ " text " , " text " , " text " , " text " , " text " , " text " ] )
return plpy . execute ( tomtom_plan , [ username , orgname , searchtext , city , state_province , country ] , 1 ) [ 0 ] [ ' point ' ]
else :
raise Exception ( ' Requested geocoder is not available ' )
2020-03-10 22:45:04 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ STABLE PARALLEL RESTRICTED ;
2018-04-09 20:41:44 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . cdb_tomtom_geocode_street_point ( username TEXT , orgname TEXT , searchtext TEXT , city TEXT DEFAULT NULL , state_province TEXT DEFAULT NULL , country TEXT DEFAULT NULL )
RETURNS Geometry AS $ $
# The configuration is retrieved but no checks are performed on it
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_geocoder_config({0}, {1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
user_geocoder_config = GD [ " user_geocoder_config_{0} " . format ( username ) ]
tomtom_plan = plpy . prepare ( " SELECT cdb_dataservices_server._cdb_tomtom_geocode_street_point($1, $2, $3, $4, $5, $6) as point; " , [ " text " , " text " , " text " , " text " , " text " , " text " ] )
return plpy . execute ( tomtom_plan , [ username , orgname , searchtext , city , state_province , country ] , 1 ) [ 0 ] [ ' point ' ]
2020-03-10 22:45:04 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ STABLE PARALLEL RESTRICTED ;
2018-04-09 20:41:44 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _cdb_tomtom_geocode_street_point ( username TEXT , orgname TEXT , searchtext TEXT , city TEXT DEFAULT NULL , state_province TEXT DEFAULT NULL , country TEXT DEFAULT NULL )
RETURNS Geometry AS $ $
from iso3166 import countries
from cartodb_services . tools import ServiceManager , QuotaExceededException
from cartodb_services . tomtom import TomTomGeocoder
from cartodb_services . tools . country import country_to_iso3
from cartodb_services . refactor . service . tomtom_geocoder_config import TomTomGeocoderConfigBuilder
import cartodb_services
cartodb_services . init ( plpy , GD )
service_manager = ServiceManager ( ' geocoder ' , TomTomGeocoderConfigBuilder , username , orgname , GD )
try :
service_manager . assert_within_limits ( )
geocoder = TomTomGeocoder ( service_manager . config . tomtom_api_key , service_manager . logger , service_manager . config . service_params )
country_iso3166 = None
if country :
country_iso3 = country_to_iso3 ( country )
if country_iso3 :
country_iso3166 = countries . get ( country_iso3 ) . alpha2 . lower ( )
coordinates = geocoder . geocode ( searchtext = searchtext , city = city ,
state_province = state_province ,
country = country_iso3166 )
if coordinates :
service_manager . quota_service . increment_success_service_use ( )
plan = plpy . prepare ( " SELECT ST_SetSRID(ST_MakePoint($1, $2), 4326); " , [ " double precision " , " double precision " ] )
point = plpy . execute ( plan , [ coordinates [ 0 ] , coordinates [ 1 ] ] , 1 ) [ 0 ]
return point [ ' st_setsrid ' ]
else :
service_manager . quota_service . increment_empty_service_use ( )
return None
except QuotaExceededException as qe :
service_manager . quota_service . increment_failed_service_use ( )
return None
except BaseException as e :
import sys
service_manager . quota_service . increment_failed_service_use ( )
service_manager . logger . error ( ' Error trying to geocode street point using TomTom ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to geocode street point using TomTom ' )
finally :
service_manager . quota_service . increment_total_service_use ( )
2020-03-10 22:45:04 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ STABLE PARALLEL RESTRICTED ;
2018-04-09 20:41:44 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _cdb_tomtom_isodistance (
username TEXT ,
orgname TEXT ,
source geometry ( Geometry , 4326 ) ,
mode TEXT ,
data_range integer [ ] ,
options text [ ] )
RETURNS SETOF cdb_dataservices_server . isoline AS $ $
from cartodb_services . tools import ServiceManager
from cartodb_services . tomtom import TomTomIsolines
from cartodb_services . tomtom . types import TRANSPORT_MODE_TO_TOMTOM
from cartodb_services . tools import Coordinate
from cartodb_services . refactor . service . tomtom_isolines_config import TomTomIsolinesConfigBuilder
import cartodb_services
cartodb_services . init ( plpy , GD )
service_manager = ServiceManager ( ' isolines ' , TomTomIsolinesConfigBuilder , username , orgname , GD )
service_manager . assert_within_limits ( )
try :
tomtom_isolines = TomTomIsolines ( service_manager . config . tomtom_api_key , service_manager . logger , service_manager . config . service_params )
if source :
lat = plpy . execute ( " SELECT ST_Y('%s') AS lat " % source ) [ 0 ] [ ' lat ' ]
lon = plpy . execute ( " SELECT ST_X('%s') AS lon " % source ) [ 0 ] [ ' lon ' ]
origin = Coordinate ( lon , lat )
else :
raise Exception ( ' source is NULL ' )
profile = TRANSPORT_MODE_TO_TOMTOM . get ( mode )
# -- TODO Support options properly
isolines = { }
for r in data_range :
isoline = tomtom_isolines . calculate_isodistance ( origin , r , profile )
isolines [ r ] = isoline
result = [ ]
for r in data_range :
if len ( isolines [ r ] ) > = 3 :
# -- TODO encapsulate this block into a func/method
locations = isolines [ r ] + [ isolines [ r ] [ 0 ] ] # close the polygon repeating the first point
wkt_coordinates = ' , ' . join ( [ " %f %f " % ( l . longitude , l . latitude ) for l in locations ] )
sql = " SELECT ST_CollectionExtract(ST_MakeValid(ST_MPolyFromText('MULTIPOLYGON((({0})))', 4326)),3) as geom " . format ( wkt_coordinates )
multipolygon = plpy . execute ( sql , 1 ) [ 0 ] [ ' geom ' ]
else :
multipolygon = None
result . append ( [ source , r , multipolygon ] )
service_manager . quota_service . increment_success_service_use ( )
service_manager . quota_service . increment_isolines_service_use ( len ( isolines ) )
return result
except BaseException as e :
import sys
service_manager . quota_service . increment_failed_service_use ( )
service_manager . logger . error ( ' Error trying to get TomTom isolines ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to get TomTom isolines ' )
finally :
service_manager . quota_service . increment_total_service_use ( )
2020-03-10 22:45:04 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ SECURITY DEFINER STABLE PARALLEL RESTRICTED ;
2018-04-09 20:41:44 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . _cdb_tomtom_isochrones (
username TEXT ,
orgname TEXT ,
source geometry ( Geometry , 4326 ) ,
mode TEXT ,
data_range integer [ ] ,
options text [ ] )
RETURNS SETOF cdb_dataservices_server . isoline AS $ $
from cartodb_services . tools import ServiceManager
from cartodb_services . tomtom import TomTomIsolines
from cartodb_services . tomtom . types import TRANSPORT_MODE_TO_TOMTOM
from cartodb_services . tools import Coordinate
from cartodb_services . tools . coordinates import coordinates_to_polygon
from cartodb_services . refactor . service . tomtom_isolines_config import TomTomIsolinesConfigBuilder
import cartodb_services
cartodb_services . init ( plpy , GD )
service_manager = ServiceManager ( ' isolines ' , TomTomIsolinesConfigBuilder , username , orgname , GD )
service_manager . assert_within_limits ( )
try :
tomtom_isolines = TomTomIsolines ( service_manager . config . tomtom_api_key , service_manager . logger , service_manager . config . service_params )
if source :
lat = plpy . execute ( " SELECT ST_Y('%s') AS lat " % source ) [ 0 ] [ ' lat ' ]
lon = plpy . execute ( " SELECT ST_X('%s') AS lon " % source ) [ 0 ] [ ' lon ' ]
origin = Coordinate ( lon , lat )
else :
raise Exception ( ' source is NULL ' )
profile = TRANSPORT_MODE_TO_TOMTOM . get ( mode )
resp = tomtom_isolines . calculate_isochrone ( origin , data_range , profile )
if resp :
result = [ ]
for isochrone in resp :
result_polygon = coordinates_to_polygon ( isochrone . coordinates )
if result_polygon :
service_manager . quota_service . increment_success_service_use ( )
result . append ( [ source , isochrone . duration , result_polygon ] )
else :
service_manager . quota_service . increment_empty_service_use ( )
result . append ( [ source , isochrone . duration , None ] )
service_manager . quota_service . increment_success_service_use ( )
service_manager . quota_service . increment_isolines_service_use ( len ( result ) )
return result
else :
service_manager . quota_service . increment_empty_service_use ( )
return [ ]
except BaseException as e :
import sys
service_manager . quota_service . increment_failed_service_use ( )
service_manager . logger . error ( ' Error trying to get TomTom isochrones ' , sys . exc_info ( ) , data = { " username " : username , " orgname " : orgname } )
raise Exception ( ' Error trying to get TomTom isochrones ' )
finally :
service_manager . quota_service . increment_total_service_use ( )
2020-03-10 22:45:04 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ SECURITY DEFINER STABLE PARALLEL RESTRICTED ;
2018-04-09 20:41:44 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . cdb_isodistance ( username TEXT , orgname TEXT , source geometry ( Geometry , 4326 ) , mode TEXT , range integer [ ] , options text [ ] DEFAULT array [ ] : : text [ ] )
RETURNS SETOF cdb_dataservices_server . isoline AS $ $
from cartodb_services . metrics import metrics
from cartodb_services . tools import Logger
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_isolines_routing_config({0}, {1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
user_isolines_config = GD [ " user_isolines_routing_config_{0} " . format ( username ) ]
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
logger = Logger ( logger_config )
if user_isolines_config . google_services_user :
raise Exception ( ' This service is not available for google service users. ' )
2018-04-11 18:56:55 +08:00
params = { ' source ' : source , ' mode ' : mode , ' range ' : range , ' options ' : options }
with metrics ( ' cdb_isodistance ' , user_isolines_config , logger , params ) :
2018-04-09 20:41:44 +08:00
if user_isolines_config . heremaps_provider :
here_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server.cdb_here_isodistance($1, $2, $3, $4, $5, $6) as isoline; " , [ " text " , " text " , " geometry(geometry, 4326) " , " text " , " integer[] " , " text[] " ] )
return plpy . execute ( here_plan , [ username , orgname , source , mode , range , options ] )
elif user_isolines_config . mapzen_provider :
mapzen_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server.cdb_mapzen_isodistance($1, $2, $3, $4, $5, $6) as isoline; " , [ " text " , " text " , " geometry(geometry, 4326) " , " text " , " integer[] " , " text[] " ] )
return plpy . execute ( mapzen_plan , [ username , orgname , source , mode , range , options ] )
elif user_isolines_config . mapbox_provider :
mapbox_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server.cdb_mapbox_isodistance($1, $2, $3, $4, $5, $6) as isoline; " , [ " text " , " text " , " geometry(geometry, 4326) " , " text " , " integer[] " , " text[] " ] )
return plpy . execute ( mapbox_plan , [ username , orgname , source , mode , range , options ] )
elif user_isolines_config . tomtom_provider :
tomtom_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server.cdb_tomtom_isodistance($1, $2, $3, $4, $5, $6) as isoline; " , [ " text " , " text " , " geometry(geometry, 4326) " , " text " , " integer[] " , " text[] " ] )
return plpy . execute ( tomtom_plan , [ username , orgname , source , mode , range , options ] )
else :
raise Exception ( ' Requested isolines provider is not available ' )
2020-03-10 22:45:04 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ STABLE PARALLEL RESTRICTED ;
2018-04-09 20:41:44 +08:00
-- tomtom isodistance
CREATE OR REPLACE FUNCTION cdb_dataservices_server . cdb_tomtom_isodistance ( username TEXT , orgname TEXT , source geometry ( Geometry , 4326 ) , mode TEXT , range integer [ ] , options text [ ] DEFAULT array [ ] : : text [ ] )
RETURNS SETOF cdb_dataservices_server . isoline AS $ $
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_isolines_routing_config({0}, {1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
user_isolines_config = GD [ " user_isolines_routing_config_{0} " . format ( username ) ]
tomtom_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server._cdb_tomtom_isodistance($1, $2, $3, $4, $5, $6) as isoline; " , [ " text " , " text " , " geometry(geometry, 4326) " , " text " , " integer[] " , " text[] " ] )
result = plpy . execute ( tomtom_plan , [ username , orgname , source , mode , range , options ] )
return result
2020-03-10 22:45:04 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ STABLE PARALLEL RESTRICTED ;
2018-04-09 20:41:44 +08:00
CREATE OR REPLACE FUNCTION cdb_dataservices_server . cdb_isochrone ( username TEXT , orgname TEXT , source geometry ( Geometry , 4326 ) , mode TEXT , range integer [ ] , options text [ ] DEFAULT array [ ] : : text [ ] )
RETURNS SETOF cdb_dataservices_server . isoline AS $ $
from cartodb_services . metrics import metrics
from cartodb_services . tools import Logger
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_isolines_routing_config({0}, {1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
user_isolines_config = GD [ " user_isolines_routing_config_{0} " . format ( username ) ]
plpy . execute ( " SELECT cdb_dataservices_server._get_logger_config() " )
logger_config = GD [ " logger_config " ]
logger = Logger ( logger_config )
if user_isolines_config . google_services_user :
raise Exception ( ' This service is not available for google service users. ' )
2018-04-11 18:56:55 +08:00
params = { ' source ' : source , ' mode ' : mode , ' range ' : range , ' options ' : options }
with metrics ( ' cdb_isochrone ' , user_isolines_config , logger , params ) :
2018-04-09 20:41:44 +08:00
if user_isolines_config . heremaps_provider :
here_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server.cdb_here_isochrone($1, $2, $3, $4, $5, $6) as isoline; " , [ " text " , " text " , " geometry(geometry, 4326) " , " text " , " integer[] " , " text[] " ] )
return plpy . execute ( here_plan , [ username , orgname , source , mode , range , options ] )
elif user_isolines_config . mapzen_provider :
mapzen_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server.cdb_mapzen_isochrone($1, $2, $3, $4, $5, $6) as isoline; " , [ " text " , " text " , " geometry(geometry, 4326) " , " text " , " integer[] " , " text[] " ] )
return plpy . execute ( mapzen_plan , [ username , orgname , source , mode , range , options ] )
elif user_isolines_config . mapbox_provider :
mapbox_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server.cdb_mapbox_isochrone($1, $2, $3, $4, $5, $6) as isoline; " , [ " text " , " text " , " geometry(geometry, 4326) " , " text " , " integer[] " , " text[] " ] )
return plpy . execute ( mapbox_plan , [ username , orgname , source , mode , range , options ] )
elif user_isolines_config . tomtom_provider :
tomtom_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server.cdb_tomtom_isochrone($1, $2, $3, $4, $5, $6) as isoline; " , [ " text " , " text " , " geometry(geometry, 4326) " , " text " , " integer[] " , " text[] " ] )
return plpy . execute ( tomtom_plan , [ username , orgname , source , mode , range , options ] )
else :
raise Exception ( ' Requested isolines provider is not available ' )
2020-03-10 22:45:04 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ STABLE PARALLEL RESTRICTED ;
2018-04-09 20:41:44 +08:00
-- tomtom isochrone
CREATE OR REPLACE FUNCTION cdb_dataservices_server . cdb_tomtom_isochrone ( username TEXT , orgname TEXT , source geometry ( Geometry , 4326 ) , mode TEXT , range integer [ ] , options text [ ] DEFAULT array [ ] : : text [ ] )
RETURNS SETOF cdb_dataservices_server . isoline AS $ $
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_isolines_routing_config({0}, {1}) " . format ( plpy . quote_nullable ( username ) , plpy . quote_nullable ( orgname ) ) )
user_isolines_config = GD [ " user_isolines_routing_config_{0} " . format ( username ) ]
tomtom_plan = plpy . prepare ( " SELECT * FROM cdb_dataservices_server._cdb_tomtom_isochrones($1, $2, $3, $4, $5, $6) as isoline; " , [ " text " , " text " , " geometry(geometry, 4326) " , " text " , " integer[] " , " text[] " ] )
result = plpy . execute ( tomtom_plan , [ username , orgname , source , mode , range , options ] )
return result
2020-03-10 22:45:04 +08:00
$ $ LANGUAGE @ @ plpythonu @ @ STABLE PARALLEL RESTRICTED ;