-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgetLatLonViaYql.py
More file actions
27 lines (22 loc) · 833 Bytes
/
getLatLonViaYql.py
File metadata and controls
27 lines (22 loc) · 833 Bytes
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
#! /usr/bin/env python3
import requests
import re
def getLatLonViaYql(city, countryCode):
# build the YQL query
yqlQuery = ('select centroid.latitude,centroid.longitude '
'from geo.places '
'where text="{}" and country.code="{}" and '
'placeTypeName.code="7"'
).format(city, countryCode)
# build the URL
yqlUrl = 'http://query.yahooapis.com/v1/public/yql'
r = requests.get(yqlUrl, params={'q': yqlQuery})
xmlData = r.text
latStr = re.search('latitude>([0-9.-]+)<', xmlData)
lonStr = re.search('longitude>([0-9.-]+)<', xmlData)
if latStr is None or lonStr is None:
print('Warning, unknown city {}'.format(city))
return (0, 0)
lat = float(latStr.group(1))
lon = float(lonStr.group(1))
return (lat, lon)