forked from guiguiabloc/api-domogeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassGeoLocation.py
59 lines (53 loc) · 2.06 KB
/
ClassGeoLocation.py
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
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Gruik coded by GuiguiAbloc
# http://blog.guiguiabloc.fr
# http://api.domogeek.fr
#
import urllib,urllib2
from xml.dom.minidom import parseString
import json
class geolocation:
def geogoogle(self, addr, api_key):
url = "https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s" % (urllib.quote(addr), urllib.quote(api_key))
try:
data = urllib.urlopen(url).read()
datagoogle = json.loads(data)
except:
return False
assert datagoogle['status'] == "OK"
#print "CHECK GOOGLE"
googleresult = json.dumps([s['geometry']['location'] for s in datagoogle['results']], indent=3)
analyse = json.JSONDecoder()
for part in analyse.decode(googleresult):
latitude = part['lat']
longitude = part['lng']
return latitude,longitude
def geobing(self, addr, api_key):
url = "http://dev.virtualearth.net/REST/v1/Locations?q=%s&key=%s" % (urllib.quote(addr), urllib.quote(api_key))
try:
data = urllib2.urlopen(url).read()
databing = json.loads(data)
except:
return False
assert databing['statusCode'] == 200
#print "CHECK BING"
bingresult = databing['resourceSets'][0]['resources']
pointresult = [ city['point'] for city in bingresult ]
coord = [ city['coordinates'] for city in pointresult ]
latitude = coord[0][0]
longitude = coord[0][1]
return latitude,longitude
def geonames(self, addr, api_key):
url = "http://api.geonames.org/search?q=%s&maxRows=1&username=%s" % (urllib.quote(addr), urllib.quote(api_key))
try:
data = urllib2.urlopen(url).read()
except:
return "Error"
dom = parseString(data)
latTag = dom.getElementsByTagName('lat')[0].toxml()
lngTag = dom.getElementsByTagName('lng')[0].toxml()
latitude=latTag.replace('<lat>','').replace('</lat>','')
longitude=lngTag.replace('<lng>','').replace('</lng>','')
return latitude,longitude