Skip to content

Instantly share code, notes, and snippets.

@andresmachado
Last active April 7, 2017 12:38
Show Gist options
  • Save andresmachado/dd17003f578fc2955482761a0bcc130c to your computer and use it in GitHub Desktop.
Save andresmachado/dd17003f578fc2955482761a0bcc130c to your computer and use it in GitHub Desktop.
class OccurencesInRadiusDistance(generics.ListAPIView):
"""API endpoint that returns all objects in a radius given a set of coordinates in url_params."""
serializer_class = MyCustomSerializer
permissions_classes = (permissions.IsAuthenticatedOrReadOnly, )
def get_queryset(self):
"""This queryset returns all objects in a specified radius distance. We use PostGIS to create POINT objects and create the query.
How to use postGIS on Django: https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/postgis/
Obs: The model must have a models.PointField field with point from its latitude and longitude
"""
radius_distance = 5
lon = self.request.query_params.get('lon', None)
lat = self.request.query_params.get('lat', None)
queryset = MyCustomModel.objects.all()
if lon and lat:
point = 'POINT({} {})'.format(lon, lat)
pnt = GEOSGeometry(point)
queryset = MyCustomModel.objects.filter(address_point__distance_lte=(pnt, D(km=radius_distance)))
return queryset
return queryset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment