2010-02-10 26 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