17

使用geoDjango,有什么区别

myObj.objects.filter(point__dwithin(...etc.))   

myObj.objects.filter(point__distance_lt(...etc.))  

?
他们是同一件事,还是他们在做微妙不同的事情?

4

1 回答 1

31

好的,我做了一些研究,但我不知道结果是否有用;)

我已经有一个使用PostgreSQL数据库的地理应用程序。当我执行此查询时__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

当我尝试使用 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.

所以我不得不将查询更改为不使用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
- 将度数值作为距离参数。
- 使用ST_DWithinSQL 函数。

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

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

于 2010-02-10T12:33:16.980 回答