-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeonames.py
92 lines (80 loc) · 2.91 KB
/
geonames.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import settings
import httplib
import simplejson
import sys
from urllib import urlencode
from xml.etree import ElementTree
class GeoNames():
"""
Accessor class for the online GeoNames geographical database.
"""
def __init__(self, server=settings.DEFAULT_GEONAMES_SERVER):
"""
Create a GeoNames object.
server - address of the server which provides REST webservice interface.
"""
self.server = server
def _api_call(self, method, resource, **kwargs):
"""
Makes a generic API call to geonames webservice.
"""
uri = "/%s?%s" %(resource, urlencode(kwargs))
c = self.get_connection()
c.request(method, uri)
response = c.getresponse()
if not 200 == response.status:
raise GeoNameException("Expected a 200 reponse but got %s." %(response.status))
return response.read()
def get_connection(self):
"""
Return a connection object to the webservice.
"""
c = httplib.HTTPConnection(self.server)
return c
def search(self, name, country):
"""
Perform a search for a country's information.
"""
# we only want exact matches, and we only want one possible match.
xml = self._api_call('GET', 'search', name_equals=name, country=country, maxRows=1)
root_element = ElementTree.XML(xml)
results = root_element.find('totalResultsCount').text
if not results:
raise GeoNameResultException("No results returned for query.")
return GeoResult(
name = root_element.find('geoname/name').text,
country_name = root_element.find('geoname/countryName').text,
country_code = root_element.find('geoname/countryCode').text,
latitude = root_element.find('geoname/lat').text,
longitude = root_element.find('geoname/lng').text,
)
class GeoResult(object):
"""
Result object stores data returned from GeoNames api accessor object.
"""
def __init__(self, name=None, country_name=None, country_code=None, latitude=None, longitude=None):
self.name = name
self.country_name = country_name
self.country_code = country_code
self.latitude = latitude
self.longitude = longitude
def is_complete(self):
complete = True
for key, val in self.__dict__.items():
if not val:
complete = False
break
return complete
class GeoNameException(Exception):
"""
Error initializing GeoNames accessor.
"""
def __init__(self, value):
self.message = value
def __str__(self):
return repr(self.message)
class GeoNameResultException(GeoNameException):
"""
Error getting results from GeoName webservice.
"""
pass