Check if multipolygon contains point in GeoDjango
I have MultiPolygon field, preferences.locations
and the Point field rental.location
. When a query to check if the lease is contained in preferences.locations
, the query is only executed if rental.location
contained in the first polygon in the preferences.locations
MultiPolygon.
For example, with these geometries:
point1 = (81.20141954209073, -129.891357421875)
point2 = (40.70875101828792, -73.93179774284363)
preferences.locations = MultiPolygon(
Polygon(((81.14748070499664, -163.289794921875),
point1, # contains the first point
(81.14748070499664, -163.289794921875),
(81.14748070499664, -163.289794921875),)),
Polygon(((40.70718949655447, -73.98123621940613),
point2, # contains the second point
(40.683762276904055, -73.99702906608582),
(40.70718949655447, -73.98123621940613),)),
)
rental1.location = Point(*point1)
rental2.location = Point(*point2)
When you ask which rental locations are located in preferences.locations
, while both rentals must be returned, only the first rental is returned.
>>> Rental.objects.filter(location__contained=preferences.locations)
[<Rental: Rental object>] # rental1
How can I successfully check which lease placements are contained in preferences.locations
(no matter which Polygon they are contained in).
source to share