2012-03-06 30 views
4

有一個覆蓋查詢問題的GIS,查詢返回一個項目列表,該項目的座標位於我搜索的區域之外,發生了什麼?Django GIS的覆蓋查詢返回錯誤結果

from django.contrib.gis.geos import Polygon 
from deals.models import Deal 

x1, y1 = 37.446899, 55.693455 
x2, y2 = 37.666626, 55.551165 
area = Polygon(((x1, y1), (x2, y1), (x2, y2), (x1, y2), (x1, y1))) 
qs = Deal.objects.filter(locations__coords__coveredby=area) 

def count(): 
    ok, failed = 0, 0 
    for item in qs.filter(locations__coords__isnull=False)[:20]: 
     for loc in item.locations.all(): 
      lon = loc.longitude 
      lat = loc.latitude 
      if x1 <= lon <= x2 and y1 <= lat <= y2: 
       ok += 1 
      else: 
       failed += 1 
    return ok, failed 

>>> ok, failed 
Out[18]: (0, 11) 

回答

0

的解決方案是簡單的:

x1, y1 = 'left bottom corner of rectangle area' 
x2, y2 = 'top right corner of rectangle area' 
area = Polygon.from_bbox((x1, y1, x2, y2)) 

P.S.用測試覆蓋此代碼以確保:)