首页 > 解决方案 > Django 的 GEOS API 中的几何交集不再起作用

问题描述

我使用 Django 的 GEOS API 已经有一段时间了,效果很好。我在我的项目中升级了一些东西,并且我拥有的一些代码确定了多边形内的点是否不再起作用。我把它提炼成这个,这证明了这个问题。

我设置了这个模型:

class TestPoint(models.Model):
    name = models.CharField(max_length=64)
    pickup_location = models.PointField(srid=4326)

然后我运行:

>>> from django.contrib.gis.geos import Point, Polygon
>>> from mytest.models import TestPoint
>>> p1 = TestPoint(name="p1", pickup_location=Point(-118.4, 33.9))
>>> p2 = TestPoint(name="p2", pickup_location=Point(-118.45, 34))
>>> p3 = TestPoint(name="p3", pickup_location=Point(-118.3, 34.02))
>>> p4 = TestPoint(name="p4", pickup_location=Point(-118.46, 34.01))
>>> bbox = (-118.5, 34, -118, 34.5)
>>> poly = Polygon().from_bbox(bbox)
>>> poly.srid = 4326
>>> hits = TestPoints.objects.filter(pickup_location__within=poly)
>>> hits
<QuerySet []>

我希望hits包含 3 分,但如您所见,它是空的。

我想知道他们是否改变了坐标的顺序from_bbox(),但文档说它仍然是(xmin,ymin,xmax,ymax)。

这是怎么回事?

标签: pythondjangogeometrygeos

解决方案


您没有将TestPoints 保存到数据库中。你保存这些:

p1 = TestPoint.objects.create(name='p1', pickup_location=Point(-118.4, 33.9))

通过使用:

p1 = TestPoint(name="p1", pickup_location=Point(-118.4, 33.9))

您只需在 Django/Python 层创建一个TestPoint 对象,但您从未将其放入数据库中。


推荐阅读