I am trying to retrieve some posts depending on their proximity geographically.
As you can see in the code I am using GeoDjango and the code is executed within a view.
The issue is that the distance filter seems to be completely disregarded.
When I check the distances on the queryset I get the expected distances (1m and 18km) but the 18km post should not have been retrieved.
def get(self, request, format=None):
latlon=request.query_params
pnt = GEOSGeometry('SRID=28992;POINT(%s %s)'%(latlon['lat'],latlon['lon']))
posts = Post.objects.filter(point__distance_lte=(pnt, D(km=1)))
serialized = PostSerializer(posts, many=True)
for post in posts:
distance = pnt.distance(post.point)
print("Km distance:",distance * 100)
return JsonResponse(serialized.data, safe=False)
Result:
Km distance: 0.00015231546206626192
Km distance: 18.317378752081577
[18/Jul/2017 13:24:35] "GET /api/posts/?lon=5.8372264&lat=51.8125626 HTTP/1.1" 200 144