-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgisgraphy.py
More file actions
67 lines (59 loc) · 2.1 KB
/
gisgraphy.py
File metadata and controls
67 lines (59 loc) · 2.1 KB
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
import json
import re
from restkit import OAuthFilter, request, Resource, TConnectionManager
from restkit.errors import RequestFailed
from settings import settings
from models import GeonamesPlace
from utils import in_local_box
class GisgraphyResource(Resource):
COORD_RE = re.compile('(-?\d+\.\d+), *(-?\d+\.\d+)')
def __init__(self):
Resource.__init__(self,
settings.gisgraphy_url,
conn_manager = TConnectionManager(),
client_opts={'timeout':30},
)
def fulltextsearch(self, q, headers=None, **kwargs):
#we make the query lower case as workaround for "Portland, OR"
r = self.get('fulltext/fulltextsearch',
headers,
q=q,
format="json",
spellchecking=False,
**kwargs)
return json.loads(r.body_string())["response"]["docs"]
def twitter_loc(self, q):
if not q: return None
# check for "30.639, -96.347" style coordinates
match = self.COORD_RE.search(q)
if match:
return GeonamesPlace(
lat=float(match.group(1)),
lng=float(match.group(2)),
feature_code='COORD',
)
#try gisgraphy
q = q.lower().strip().replace('-','/').replace(',',', ')
q = ''.join(re.split('[|&!]',q))
if not q: return None
results = self.fulltextsearch(q)
# otherwise, return the first result
for res in results:
if res['name']=='Sugar Land' and 'sugar' not in q:
break
return GeonamesPlace(res)
# try splitting q in half
found = None
for splitter in ('and','or','/'):
parts = q.split(splitter)
if len(parts)==2:
for part in parts:
res = self.twitter_loc(part)
if res and in_local_box(res.to_d()):
return res
if res:
found = res
return found
if __name__ == '__main__':
res = GisgraphyResource()
f = res.fulltextsearch('Austin TX')