-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
331 lines (237 loc) · 10.3 KB
/
model.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import code
import math
import logging
from datetime import datetime, timedelta
from sqlalchemy import create_engine, event
from sqlalchemy import Column, DateTime, Float, Integer, MetaData, UnicodeText
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.declarative import declared_attr, declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
import geoalchemy
from geoalchemy.postgis import PGComparator
# Edit to reflect the db name, user and password to connect to your
# postgis database.
DB_SETTINGS = dict(
db_name = '...',
db_user = '...',
db_password = '...',
db_host = 'localhost',
db_port = 5432
)
def engine_factory():
"""Creates the database engine."""
settings = DB_SETTINGS
postgresql_path = 'postgresql://%s:%s@%s:%s/%s' % (
settings['db_user'],
settings['db_password'],
settings['db_host'],
settings['db_port'],
settings['db_name']
)
return create_engine(postgresql_path)
engine = engine_factory()
Session = scoped_session(sessionmaker(bind=engine))
SQLModel = declarative_base()
max_radius_of_earth = 6500 * 1000 # metres
max_sqrt_distance = math.sqrt(max_radius_of_earth * 0.95)
min_sqrt_distance = math.sqrt(100)
class Geography(geoalchemy.Geometry):
"""Subclass of ``Geometry`` that stores a `Geography Type`_.
Defaults to storing a point. Call with ``specific=False`` if you don't
want to define the geography type it stores, or specify using
``geography_type='POLYGON'``, etc.
_`Geography Type`: http://postgis.refractions.net/docs/ch04.html#PostGIS_Geography
"""
@property
def name(self):
if not self.kwargs.get('specific', True):
return 'GEOGRAPHY'
geography_type = self.kwargs.get('geography_type', 'POINT')
srid = self.kwargs.get('srid', 4326)
return 'GEOGRAPHY(%s,%d)' % (geography_type, srid)
class BaseMixin(object):
"""Provides ``id``, ``v`` for version, ``c`` for created and ``m`` for
modified columns and a scoped ``self.query`` property.
"""
id = Column(Integer, primary_key=True)
v = Column(Integer, default=1)
c = Column(DateTime, default=datetime.utcnow)
m = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
query = Session.query_property()
class LocationMixin(object):
"""Provides ``self.latitude`` and ``self.longitude`` attributes and a
``self.update_location()`` method which updates ``self.location``,
which is stored as a geography type in latlng projection.
You can keep self.location uptodate automatically by binding to
``before_insert`` and ``before_update`` events using, e.g.::
class MyGeoModel(SQLModel, LocationMixin):
pass
handler = lambda mapper, connection, target: target.update_location()
for event_name in 'before_insert', 'before_update':
event.listen(MyGeoModel, event_name, handler)
Also provides classmethods that return clauses to filter by ``within``,
``within_area`` and order by ``nearest``. So, for example, to filter
by within 10km of a latlng point, you can use::
class MyGeoModel(SQLModel, BaseMixin, LocationMixin):
pass
latitude = 51.51333
longitude = -0.0889469999
within = MyGeoModel.within(latitude, longitude, 10 * 1000)
query = MyGeoModel.query.filter(within)
And to order the results nearest to the location::
nearest = MyGeoModel.within(latitude, longitude)
query.order_by(nearest)
"""
latitude = Column(Float, nullable=False)
longitude = Column(Float, nullable=False)
@declared_attr
def location(self):
return geoalchemy.GeometryColumn(
Geography(),
comparator=PGComparator
)
def update_location(self):
"""Update ``self.location`` with a point value derived from
``self.latitude`` and ``self.longitude``. Note that the point will
be `autocast`_ to geography type on saving:
> Standard geometry type data will autocast to geography if it is of
SRID 4326.
_`autocast`: http://postgis.refractions.net/docs/ch04.html#Geography_Basics
"""
self.location = "POINT(%0.8f %0.8f)" % (self.longitude, self.latitude)
@classmethod
def within(cls, latitude, longitude, distance):
"""Return a within clause that explicitly casts the ``latitude`` and
``longitude`` provided to geography type. Note that `ST_DWithin`_
will use a spatial index to filter out rows that are not within the
boundary box before doing a sequential scan of the remaining rows.
> This function call will automatically include a bounding box comparison
that will make use of any indexes that are available on the geometries.
_`ST_DWithin`: http://postgis.refractions.net/docs/ST_DWithin.html
"""
attr = '%s.location' % cls.__tablename__
point = 'POINT(%0.8f %0.8f)' % (longitude, latitude)
location = "ST_GeographyFromText(E'SRID=4326;%s')" % point
return 'ST_DWithin(%s, %s, %d)' % (attr, location, distance)
@classmethod
def within_area(cls, area):
"""Return a within clause that explicitly casts the ``area`` polygon
provided to geography type.
"""
attr = '%s.location' % cls.__tablename__
location = "ST_GeographyFromText(E'SRID=4326;%s')" % area
return 'ST_DWithin(%s, %s, %d)' % (attr, location, 1)
@classmethod
def nearest(cls, latitude, longitude):
"""Return an order by `ST_Distance`_ clause to sort results by proximity.
"""
attr = '%s.location' % cls.__tablename__
point = 'POINT(%0.8f %0.8f)' % (longitude, latitude)
location = "ST_GeographyFromText(E'SRID=4326;%s')" % point
return 'ST_Distance(%s, %s)' % (attr, location)
@classmethod
def get_distance(
cls,
base_query,
latitude,
longitude,
target_range=None,
too_few_results=45,
too_many_results=75,
too_close=6.0
):
"""Recursive left-node-right algorithm for finding a distance that
yields an acceptable number of results.
Stops looking when it either finds an acceptable number of results
or when it gets to ``min_sqrt_distance`` or ``max_sqrt_distance``.
"""
if target_range is None:
target_range = (0, math.sqrt(max_radius_of_earth))
#logging.info('get_distance(%s, %s)' % (target_range[0], target_range[1]))
# Get the mid point in the range and expand into the actual distance
# in metres (the range is in sqrt because we're dealing with an area).
mid = (target_range[0] + target_range[1]) / 2.0
distance = mid * mid
# If the range is too narrow, let's stop wasting our own resources.
diff = target_range[1] - target_range[0]
if diff < 0: diff = 0 - diff # should never happen!
if diff < too_close or diff/mid*100 < too_close:
#logging.info('too close')
return distance
# Count how many results are within that distance.
within = cls.within(latitude, longitude, distance)
distance_query = base_query.filter(within)
count = distance_query.count()
logging.info('count = %s' % count)
# If there are too many results, try again with the bottom half of the range.
# If too few, try again with the top half of the range.
new_range = None
if count > too_many_results and mid > min_sqrt_distance:
#logging.info('too many')
new_range = (target_range[0], mid)
elif count < too_few_results and mid < max_sqrt_distance:
#logging.info('too few')
new_range = (mid, target_range[1])
if new_range:
return cls.get_distance(
base_query,
latitude,
longitude,
target_range=new_range,
too_few_results=too_few_results,
too_many_results=too_many_results,
too_close=too_close
)
# Otherwise we've found an acceptable distance.
return distance
class Message(SQLModel, BaseMixin, LocationMixin):
"""Encapsulates a message."""
__tablename__ = 'example_messages'
content = Column(UnicodeText)
def __repr__(self):
return u'<Message id="%s" content="%s">' % (
self.id,
self.snip('content', 20)
)
# Bind locatable class before insert or update events to ``target.update_location()``.
handler = lambda mapper, connection, target: target.update_location()
for event_name in 'before_insert', 'before_update':
event.listen(Message, event_name, handler)
SQLModel.metadata.create_all(engine)
def reset_db():
""" Drop all and create afresh.
"""
SQLModel.metadata.drop_all(engine)
SQLModel.metadata.create_all(engine)
def populate_db():
""" Populate the database.
"""
import random
session = Session()
messages = []
t1 = datetime.now()
for i in range(1, 500):
latitude = random.randrange(-900,900) * 0.1
longitude = random.randrange(-1800,1800) * 0.1
message = Message(
content=u'I am message %d' % i,
c=t1-timedelta(500 - i),
latitude=latitude,
longitude=longitude
)
messages.append(message)
session.add_all(messages)
try:
session.commit()
except IntegrityError, err:
logging.error(err)
session.rollback()
finally:
session.close()
def bootstrap():
"""Populate the database from scratch."""
reset_db()
populate_db()
if __name__ == '__main__':
bootstrap()
# code.interact(local=locals())