python - 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)。
这是怎么回事?
解决方案
您没有将TestPoint
s 保存到数据库中。你保存这些:
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
对象,但您从未将其放入数据库中。
推荐阅读
- encoding - 是否可以在函数内部设置编码而不是第一行代码?
- jmeter - JMeter - 版本 5.1 中没有响应标头,而版本 3.1 工作正常
- networking - 在 Solaris 9 SPARC OS [QEMU] 中配置 NIC 设备
- ios - 在 NSMapTable 中迭代键会永远保留弱值
- github - 为什么我的 shell 管道不能在我的服务器上使用私有运行器运行?为什么我会收到此错误消息?
- c# - 从站点 c# 获取 html
- entity - 软删除应该在领域层建模吗?
- hbase - HBase 连接是否应该保持活动状态?
- python - 如何处理依赖于另一个变量的变量,例如 Gurobi python 界面中的需求和价格
- python - ValueError:int() 的无效文字,基数为 10:'fee_code'