2

I need to identify quickly to which polygons a set of points belong in Django 1.9.

First option is to loop through all polygons and check which points they contain:

for countrypolygon in countrypolygons:
    placesinthecountry = Place.objects.filter(lnglat__intersects=countrypolygon.geom)

This takes a lot of time as I need to loop through a lot of polygons.

Is it possible to do the opposite, i.e. loop through each point and immediately get the polygons in which it is contained?

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
Paul Noon
  • 656
  • 1
  • 8
  • 25

1 Answers1

5

Yes, you can use contains:

for point in my_points:
    polygons = MyModel.objects.filter(geom__contains=point.geom)  
John Moutafis
  • 22,254
  • 11
  • 68
  • 112