hen*_*ski 17 django geospatial geodjango
使用geoDjango,有什么区别
myObj.objects.filter(point__dwithin(...etc.))
Run Code Online (Sandbox Code Playgroud)
和
myObj.objects.filter(point__distance_lt(...etc.))
Run Code Online (Sandbox Code Playgroud)
?
它们是同一个东西,还是它们做着微妙不同的事情?
Fel*_*ing 28
好的,我做了一些研究,但我不知道结果是否有用;)
我查看了他们用来测试数据库查询的单元测试,但他们没有提供真正的提示(对我而言).
我试着比较生成的SQL:
我已经使用PostgreSQL数据库进行了地理应用程序.当我执行此查询时__distance_lt
:
Place.objects.filter(location__distance_lt=(p.location, D(km=1))).query.as_sql()
Run Code Online (Sandbox Code Playgroud)
我得到这个生成的SQL:
SELECT (some_fields_here)
FROM "places_place"
WHERE ST_distance_sphere("places_place"."location", %s) < 1000.0
Run Code Online (Sandbox Code Playgroud)
当我尝试使用do相同时__dwithin
,我收到一个错误:
Place.objects.filter(location__dwithin=(p.location, D(km=1))).query.as_sql()
TypeError: Only numeric values of degree units are allowed on geographic DWithin queries.
Run Code Online (Sandbox Code Playgroud)
所以我不得不将查询更改为不使用D
对象:
Place.objects.filter(location__dwithin=(p.location, 1)).query.as_sql()
Run Code Online (Sandbox Code Playgroud)
结果
SELECT (some fields here)
FROM "places_place"
WHERE ST_DWithin("places_place"."location", %s, 1)
Run Code Online (Sandbox Code Playgroud)
摘要:
__dwithin
- 将度数值作为距离参数.
- 使用ST_DWithin
SQL函数.
__distance_lt
- 可以采取其他距离值;).
- 使用ST_distance_sphere
SQL函数.
顺便说一下,我对这两个查询都得到了不同的结果,但我想这主要是因为我不知道要使用哪个"度"值.
归档时间: |
|
查看次数: |
3025 次 |
最近记录: |