dataservices-api/server/extension/sql/0.0.1/20_geocode_street.sql

23 lines
983 B
MySQL
Raw Normal View History

2015-11-10 17:23:47 +08:00
-- Geocodes a street address given a searchtext and a state and/or country
CREATE OR REPLACE FUNCTION geocode_street(searchtext TEXT, city TEXT DEFAULT NULL, state_province TEXT DEFAULT NULL, country TEXT DEFAULT NULL)
2015-11-07 01:13:57 +08:00
RETURNS Geometry
AS $$
import json
2015-11-07 01:13:57 +08:00
from heremaps import heremapsgeocoder
heremaps_conf = json.loads(plpy.execute("SELECT cdb_geocoder_server._get_conf('heremaps')", 1)[0]['get_conf'])
app_id = heremaps_conf['geocoder']['app_id']
app_code = heremaps_conf['geocoder']['app_code']
2015-11-07 01:13:57 +08:00
geocoder = heremapsgeocoder.Geocoder(app_id, app_code)
results = geocoder.geocode_address(searchtext=searchtext, city=city, state=state_province, country=country)
2015-11-07 01:13:57 +08:00
coordinates = geocoder.extract_lng_lat_from_result(results[0])
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']
2015-11-10 18:36:48 +08:00
$$ LANGUAGE plpythonu;