Django GIS'由查询覆盖返回错误的结果

时间:2021-02-09 15:41:15

There is an GIS coveredby query problem, the query returns a list of items that have a coordinates outside an area I have searched in, what's going on?

有一个由查询问题覆盖的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)

1 个解决方案

#1


0  

The solution is simple:

解决方案很简单:

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. Cover this code with test to be sure :)

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

#1


0  

The solution is simple:

解决方案很简单:

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. Cover this code with test to be sure :)

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