2015-01-08 58 views
3

我有一個MultiPolygon字段preferences.locations和一個Point字段rental.location。當查詢preferences.locations是否包含租賃的位置時,只有包含在preferences.locations MultiPolygon中的第一個多邊形中,查詢纔會成功。檢查multipolygon是否包含GeoDjango中的點

例如,對於這些幾何形狀:

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(無論哪個多邊形他們通過包含)包含。

回答

7

檢查MultiPolygon是否包含點的正確方法是使用point.intersects(multipolygon)

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

謝謝!我對這個區別感到困惑,爲什麼這些點不是「被包含」而是「相交」? – mccc

+0

看看這個答案的詳細解釋:http://gis.stackexchange.com/a/108536 – yndolok