【问题标题】:Django GIS' coveredby query returns wrong resultDjango GIS'coveredby 查询返回错误结果
【发布时间】:2012-03-06 07:39:39
【问题描述】:

有一个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)

【问题讨论】:

    标签: django gis


    【解决方案1】:

    解决方法很简单:

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

    附:用测试覆盖这段代码以确保:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多