GEODjango: select all objects that are contained in a POST request given polygon

1.9k views Asked by At

my request has coordinates that form a polygon.

plist = []
...
plist.append(fromstr('POINT(%s)' %(value)))
...
p = Polygon(plist)

using the GeoDjango I'd like to select all the objects (places.objects.all()) from a table that are contained in the polygon.

going over the documentation I'm finding ways how to verify if a single point intersect or is contained with the polygon (p.mpoly.contains(pnt))but not something that works on a collection of points

is there a way to achieve that or should I use store procedure for that?

1

There are 1 answers

0
Tom--G On

I think the GeoQuerySet within filter is what you're looking for:

places.objects.filter(geom__within=p)

Assuming that geom is your geometry column.

Alternatively, you could use contained but this only considers the polygon bounding box, which may be sufficient for you depending on your use case.