How to filter locations by custom area in django?

Viewed 206

I’m to allow user to draw his custom area on frontend (flutter) and pass it to backend (django +postgis) which must return list of PointFields thats are lies inside the curve. So, in which format should I pass the curve to the backen and how do I properly filter the places queryset?

1 Answers

Assuming you've got a model defined as

class Place(models.Model):
    location = PointField()

You should be able to use the within lookup to retrieve all places with a location contained in the user provided geometry

Place.objects.filter(location__within=geometry)

I suggest you export the user selection as GeoJSON from the frontend and POST it to the backend. On the Django side you should be able to create a GEOSGeometry from the provided data

geometry = GEOSGeometry(request.POST['selection'])
Place.objects.filter(location__within=geometry)

You'll want to validate the selection is valid GeoJSON but this should at least get you started.

Related