2010-02-10 27 views

回答

25

好吧,我做了一些研究,但我不知道结果的使用;)

  • 我看着他们用它来测试数据库查询unit tests但他们没有给我真正的提示。

  • 我试图比较生成的SQL:

我已经使用PostgreSQL的 DATABSE地理应用程序。当我执行此查询与__distance_lt

Place.objects.filter(location__distance_lt=(p.location, D(km=1))).query.as_sql() 

我得到这个生成的SQL:

SELECT (some_fields_here) 
FROM "places_place" 
WHERE ST_distance_sphere("places_place"."location", %s) < 1000.0 

当我尝试使用做同样与__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. 

所以我不得不改变查询没有用D对象:

Place.objects.filter(location__dwithin=(p.location, 1)).query.as_sql() 

这导致

SELECT (some fields here) 
FROM "places_place" 
WHERE ST_DWithin("places_place"."location", %s, 1) 

总结:

__dwithin
- 取度值作为distanc e参数。
- 使用ST_DWithin SQL函数。

__distance_lt
- 可采取其它的距离值)。
- 使用ST_distance_sphere SQL函数。

顺便说一句,我得到不同的结果与两个查询,但我想这主要是由于我不知道使用哪个“度”值的事实。

+0

嗨! :)这是一个非常有用的答案 – djq 2013-04-30 13:08:54

+2

注意** dwithin **是最快的,因为它充分利用空间索引[请参阅此处讨论](http://stackoverflow.com/questions/7845133/how-cani-i-查询所有我的数据中之间距离的-5米)。 – s29 2013-05-27 04:17:33

+1

@FelixKling dwellin的价值是在**米**,每[postgis文档](http://postgis.org/docs/ST_DWithin.html) – s29 2013-05-27 04:19:03

相关问题