GeoDjango,dwithin和distance_lt之间的区别?

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_DWithinSQL函数.

__distance_lt
- 可以采取其他距离值;).
- 使用ST_distance_sphereSQL函数.

顺便说一下,我对这两个查询都得到了不同的结果,但我想这主要是因为我不知道要使用哪个"度"值.

  • 注意****dwithin最快的是因为它使空间索引的最佳使用[见这里的讨论(http://stackoverflow.com/questions/7845133/how-can-i-query-all-my-data-within -a间距离的-5米). (2认同)
  • @s29:今天查看文档(https://docs.djangoproject.com/en/dev/ref/contrib/gis/geoquerysets/#dwithin)后,它确实看起来像“dwithin”现在接受距离对象。也许给出的错误消息不正确。我只能假设这些年来这种情况发生了变化。我会更新我的答案。谢谢! (2认同)