-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodels.py
More file actions
228 lines (186 loc) · 8.17 KB
/
models.py
File metadata and controls
228 lines (186 loc) · 8.17 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# -*- coding: utf-8 -*-
# Copyright (c) 2008 Alberto García Hierro <fiam@rm-fr.net>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from django.db import models
from django.conf import settings
from django.utils import simplejson
from django.utils.translation import ugettext as _
from geonames.models import Geoname, Country
from datetime import datetime
from urllib2 import urlopen, quote
from decimal import Decimal, InvalidOperation
GOOGLE_GEOCODE_URI = 'http://maps.google.com/maps/geo?key=%(key)s&' \
'oe=utf8&output=json&q=%(q)s'
GOOGLE_REVERSE_GEOCODE_URI = 'http://maps.google.com/maps/geo?output=json' \
'&oe=utf-8&ll=%(latitude)s%%2C%(longitude)s&key=%(key)s'
class BigIntegerField(models.IntegerField):
empty_strings_allowed = False
def get_internal_type(self):
return 'BigIntegerField'
def db_type(self):
if settings.DATABASE_ENGINE == 'oracle':
return 'NUMBER(19)'
return 'BIGINT'
class GeocodedPoint(models.Model):
hash = BigIntegerField(primary_key=True)
status = models.IntegerField(null=True)
accuracy = models.IntegerField(null=True)
latitude = models.DecimalField(max_digits=20, decimal_places=17, null=True)
longitude = models.DecimalField(max_digits=20, decimal_places=17, null=True)
altitude = models.DecimalField(max_digits=10, decimal_places=5, null=True)
address = models.CharField(max_length=300, null=True)
thoroughfare_name = models.CharField(max_length=200, null=True)
locality_name = models.CharField(max_length=200, null=True)
dependent_locality_name = models.CharField(max_length=200, null=True)
country = models.ForeignKey(Country, null=True)
near = models.ForeignKey(Geoname, null=True, related_name='near_points')
location = models.ForeignKey(Geoname, null=True,
related_name='located_points')
created = models.DateTimeField(default=datetime.now)
def __unicode__(self):
return (self.thoroughfare_name or 'near ' + unicode(self.near)) \
+ ' (%s, %s)' % (self.latitude, self.longitude)
def success(self):
return int(self.status) == 200
def match(self):
if self.latitude and self.longitude:
for max_distance in (1, 3, 5, 10, 50, 100):
nears = Geoname.near_point(self.latitude, self.longitude,
kms=max_distance)
if not nears:
continue
self.near = nears[0][0]
self.country_id = nears[0][0].country_id
if self.dependent_locality_name:
for near in nears:
if near[0].name == self.dependent_locality_name:
self.location = near[0]
self.save()
return self
if self.locality_name:
for near in nears:
if near[0].name == self.locality_name:
self.location = near[0]
self.save()
return self
for near in nears:
if near[0].population:
self.location = near[0]
self.save()
return self
self.save()
return self
def read_data(self, data):
if data['Status']['code'] == 200 and 'Placemark' in data \
and len(data['Placemark']) > 0:
self.address = data['Placemark'][0]['address']
self.country_id = data['Placemark'][0]['AddressDetails']['Country']['CountryNameCode']
self.accuracy = data['Placemark'][0]['AddressDetails']['Accuracy']
self.longitude, self.latitude, self.altitude = [Decimal(str(x)) for x in data['Placemark'][0]['Point']['coordinates']]
try:
locality = data['Placemark'][0]['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']
except KeyError:
# No more interesting data
self.save()
return
self.locality_name = locality.get('LocalityName')
self.dependent_locality_name = locality.get('DependentLocalityName')
if 'Thoroughfare' in locality:
self.thoroughfare_name = locality['Thoroughfare'].get('ThoroughfareName')
self.save()
@property
def near_name(self):
try:
return self.near.i18n_name
except AttributeError:
return u''
@property
def location_name(self):
try:
return self.location.i18n_name
except AttributeError:
return u''
@property
def parent_name(self):
try:
return self.near.parent.i18n_name
except AttributeError:
return u''
@property
def country_name(self):
try:
return self.country.geoname.i18n_name
except AttributeError:
return u''
@property
def request_id(self):
return str(self.hash)
@property
def display_name(self):
if self.near:
return _('%(name)s near %(near_name)s in %(location_name)s,' \
' %(country_name)s') % \
{
'name': self.thoroughfare_name or _('Somewhere'),
'near_name': self.near_name,
'location_name': self.location_name or self.parent_name,
'country_name': self.country_name,
}
return _('Somewhere')
@property
def tz_dst(self):
try:
return self.near.timezone.dst_offset
except AttributeError:
return None
def direct_geocode(address):
h = hash(address)
point, created = GeocodedPoint.objects.get_or_create(hash=h)
if not created:
return point
fp = urlopen(GOOGLE_GEOCODE_URI % { 'key': settings.GOOGLE_JS_API_KEY,
'q': quote(address.encode('utf8')) })
data = simplejson.load(fp)
fp.close()
point.read_data(data)
return point.match()
def reverse_geocode(latitude, longitude):
if not (-85 < latitude < 85) or not (-180 < longitude < 180):
return GeocodedPoint(status=400)
h = hash('%s,%s' % (latitude, longitude))
point, created = GeocodedPoint.objects.get_or_create(hash=h)
if not created:
return point
fp = urlopen(GOOGLE_REVERSE_GEOCODE_URI % { 'key': settings.GOOGLE_JS_API_KEY, 'latitude': latitude, 'longitude': longitude })
data = simplejson.load(fp)
fp.close()
point.status = data['Status']['code']
if point.status != 200:
if point.status == 604:
point.status = 200
point.latitude, point.longitude = [Decimal(str(x)) for x in (latitude, longitude)]
return point.match()
point.read_data(data)
# Override coordinates with the ones passed to this function
point.latitude, point.longitude = [Decimal(str(x)) for x in (latitude, longitude)]
return point.match()
def geocode(query):
try:
latitude, longitude = [Decimal(x) for x in query.split(',')]
return reverse_geocode(latitude, longitude)
except (ValueError, TypeError, InvalidOperation):
return direct_geocode(query)