Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter a queryset field of PointField for items within a specific distance range is incorrect

I would like to filter a queryset for items within a specific range. This is what my model looks like

class modelEmployee(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    location = models.PointField(srid=4326,max_length=40, blank=True,null=True) 
    objects = GeoManager()

Now this is how I am running a filter command. To return items within a specific range of 90 miles.

qset = modelEmployee.objects.filter(location__distance_lte=(someLocation, D(mi=90)))

The result returns an item whose distance is actually 223.732 miles which it shouldnt return.

These are the locations of the two items

location A - lat: 47.628641 and long: -117.402997

location B - lat: 47.618337 and long: -122.205341

The distance b/w the two is actually 223.732 miles. I must be filtering it wrong. Any suggestions on where I might be going wrong ?

like image 804
MistyD Avatar asked Oct 02 '18 10:10

MistyD


1 Answers

From docs geo spatial query you should use dwithin

Your example should use it like this:

qset = modelEmployee.objects.filter(location__dwithin=(someLocation, D(mi=90)))

like image 137
Gabriel Muj Avatar answered Oct 14 '22 22:10

Gabriel Muj