GeoDjango中如何检查多边形是否包含一个点

5

我有一个 MultiPolygon 字段,preferences.locations,和一个 Point 字段,rental.location。当查询以检查租赁位置是否包含在 preferences.locations 中时,只有当 rental.location 包含在 preferences.locations 的第一个多边形中时,查询才会成功。

例如,使用这些几何图形:

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)

在查询检查哪些租赁位置包含在 preferences.locations 中时,虽然应该返回两个租赁,但只返回第一个租赁。

>>> Rental.objects.filter(location__contained=preferences.locations)
[<Rental: Rental object>] # rental1

我该如何成功地检查哪些租赁位置包含在 preferences.locations 中(不管它们包含在哪个多边形中)。

1个回答

10

检查一个点是否包含在多边形中的正确方法是使用point.intersects(multipolygon)

>>> Rental.objects.filter(location__intersects=preferences.locations)
[<Rental: Rental object>, <Rental: Rental object>]

1
谢谢!但我对这个区别感到困惑:为什么点不是“包含”而是“相交”? - mccc
1
请查看此答案以获取详细解释:http://gis.stackexchange.com/a/108536 - yndolok

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接