public
Description: A generic, easy-to-use location-awareness application built for use in Django projects.
Homepage: http://django-geo.googlecode.com/
Clone URL: git://github.com/obeattie/django-geo.git
Search Repo:
Moved managers to separate file (preparation for generic relations for 
tidiness)
oliver@obeattie.com (author)
Mon Jan 28 14:51:40 -0800 2008
commit  fb17bf7456eacf5f827c1d1889f61f3e9466f66e
tree    d7161c2e6017de4262f2d78176251539ce83609e
parent  a8075a229803411c31c8f16ff8a9609f7626d4c0
...
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
...
77
78
79
80
 
81
82
83
...
1
2
3
4
5
6
7
 
 
8
9
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
12
13
...
21
22
23
 
24
25
26
27
0
@@ -1,69 +1,13 @@
0
 import datetime
0
 from geopy import distance as geopy_distance
0
+
0
 from django.db import models
0
 from django.conf import settings
0
 from django.utils.translation import ugettext_lazy as _
0
 
0
-from geo import fields as custom_fields
0
-from geo import geocoding, misc
0
+from geo import geocoding, managers, fields as custom_fields
0
 from geo.dateutil.relativedelta import relativedelta
0
 
0
-class LocationManager(models.Manager):
0
- def by_proximity_to_location(self, origin_location, radius_miles=None):
0
- """Returns a list of all self.model objects (excluding the origin_location)
0
- within radius_miles miles of the passed location if specified (otherwise
0
- returns all other objects), ordered by ascending proximity to it."""
0
-
0
- if radius_miles is not None:
0
- # It is assumed that 1 degree of latitude and longitude is equal to
0
- # 75 miles (this is actually overestimated)
0
- coord_set = {
0
- 'latitude': {
0
- 'minimum': origin_location.latitude - (radius_miles / 75),
0
- 'maximum': origin_location.latitude + (radius_miles / 75),
0
- },
0
- 'longitude': {
0
- 'minimum': origin_location.longitude - (radius_miles / 75),
0
- 'maximum': origin_location.longitude + (radius_miles / 75),
0
- },
0
- }
0
-
0
- results = self.model.objects.filter(latitude__range=(coord_set['latitude']['minimum'], coord_set['latitude']['maximum'])).filter(longitude__range=(coord_set['longitude']['minimum'], coord_set['longitude']['maximum']))
0
- else:
0
- results = self.model.objects.all()
0
-
0
- # Exclude any locations with exactly the same co-ordinates (GeoPy doesn't play nice with these)
0
- results = list(results.exclude(latitude__exact=origin_location.latitude).exclude(longitude__exact=origin_location.longitude))
0
- # And any that don't actually fall within the radius (accurately calculated)
0
- if radius_miles is not None:
0
- for result in results:
0
- if geopy_distance.distance((result.latitude, result.longitude), (origin_location.latitude, origin_location.longitude)).miles > radius_miles:
0
- results.remove(result)
0
-
0
- def proximity_cmp(current, previous, location=origin_location):
0
- return misc.base_cmp_by_proximity(current, previous, location.coords_tuple)
0
-
0
- results.sort(proximity_cmp)
0
- return results
0
-
0
- # Backwards-compatibility
0
- by_prox = by_proximity_to_location
0
-
0
- @property
0
- def public(self):
0
- """Returns all self.model objects which have is_public set as True (convenience function)."""
0
- return self.model.objects.filter(is_public=True)
0
-
0
- @property
0
- def expired(self):
0
- """Returns all self.model objects which have expired (convenience function)."""
0
- return self.model.objects.filter(refreshed__lte=(datetime.datetime.now() - relativedelta.relativedelta(**settings.MAX_LOCATION_CACHE_AGE)))
0
-
0
- def within_bounds(self, north_west, south_east):
0
- """Returns a QuerySet of self.models within the supplied lat/long two-tuples (the northwest
0
- and southwest-most corners bounding the segment of the earth in which to search)."""
0
- return self.model.objects.filter(latitude__range=(north_west[0], south_east[0])).filter(longitude__range=(north_west[1], south_east[1]))
0
-
0
 class Location(models.Model):
0
   """A Location on the earth."""
0
   query = models.CharField(_('Location'), max_length=250, blank=False, null=False, unique=True)
0
@@ -77,7 +21,7 @@ class Location(models.Model):
0
   created = models.DateTimeField(editable=False, blank=True, null=True, default=datetime.datetime.now())
0
   is_public = models.BooleanField(default=True)
0
   # Manager
0
- objects = LocationManager()
0
+ objects = managers.LocationManager()
0
   
0
   class Admin:
0
     list_display = ('__str__', 'latitude', 'longitude', 'created', 'refreshed')

Comments

    No one has commented yet.