From 0bc7b3853e03cc6541141b5d81010235934d54c2 Mon Sep 17 00:00:00 2001 From: Sanj Date: Wed, 23 May 2012 16:14:39 +0530 Subject: [PATCH] implement nearby_areas and nearby_stops queries --- chaloBEST/mumbai/models.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/chaloBEST/mumbai/models.py b/chaloBEST/mumbai/models.py index 06dbc70..dbfd8ed 100644 --- a/chaloBEST/mumbai/models.py +++ b/chaloBEST/mumbai/models.py @@ -5,6 +5,7 @@ from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic from django.db import connection import json +from django.contrib.gis.measure import D STOP_CHOICES = ( ('U','Up'), ('D', 'Down'), @@ -104,7 +105,16 @@ class Area(models.Model): def __unicode__(self): return self.name - + + #FIXME: ideally this would be done using the polygon of the area, but right now we take a random stop in the area, find all stops within x kms, and then return unique areas for those stops. + @property + def nearby_areas(self, distance=D(km=5)): + stop = self.stop_set.all()[0] + tup = (stop.point, distance,) + qset = Stop.objects.filter(point__distance_lte=tup).values('area').distinct() + area_ids = [val.area for val in qset] + return Area.objects.filter(pk__in=area_ids) + class Road(models.Model): code = models.IntegerField()#primary_key=True) slug = models.SlugField(null=True) @@ -199,6 +209,10 @@ class Stop(models.Model): self.save() return self.get_geojson(srid=srid) + @property + def nearby_stops(self, dist=D(km=2)): + tup = (self.point, dist,) + return Stop.objects.filter(point__distance_lte=tup) def __unicode__(self): return self.name