Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter locations by custom area in django?

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?

like image 683
Vassily Avatar asked Dec 04 '20 19:12

Vassily


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.

like image 94
Simon Charette Avatar answered Oct 19 '22 16:10

Simon Charette