-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgoogle-geocode-example.sql
49 lines (39 loc) · 1.79 KB
/
google-geocode-example.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
--*************************************************************************************************
-- Google Geo API Example
-- Tim Fathers 2020
--
-- Uses the API documented here https://developers.google.com/maps/documentation/geocoding/intro
--
--*************************************************************************************************
-- NOTE: As yet untested with a functioning API key!
declare global temporary table TEST_LONG_LAT as (
-- Encode the address as a URL query string as required by the API.
with QUERY_STRING as (
select 'address=' concat
systools.urlencode(CITY, 'UTF-8') concat '+' concat
systools.urlencode(POSTCODE, 'UTF-8') concat '+' concat
systools.urlencode(COUNTRY, 'UTF-8') concat
'&key=[YOUR_API_KEY]' as QUERY_PARMS
-- Test data, replace with the real source data file.
from(values
('Wiesbaden', '65185', 'Germany'),
('Frankfurt', '60311', 'Germany'),
('Mainz', '55122', 'Germany')
) x(CITY, POSTCODE, COUNTRY)
),
-- Call the API for each address query above.
LONG_LAT as (
select systools.httpgetclob(
varchar('https://maps.googleapis.com/maps/api/geocode/json?' concat QUERY_PARMS),
clob('<httpHeader></httpHeader>') -- <--- Insert any required HTTP headers here
) as API_RESPONSE
from QUERY_STRING
)
-- Extract the error, latitude and longitude fields from the result.
select json_value(API_RESPONSE, '$.error_message' returning varchar(256)) as ERROR,
json_value(API_RESPONSE, '$.results.geometry.location.lat' returning varchar(256)) as LATTITUDE,
json_value(API_RESPONSE, '$.results.geometry.location.lng' returning varchar(256)) as LONGITUDE
from LONG_LAT
) with data with replace
;;
select * from QTEMP.TEST_LONG_LAT