GeoDjango,dwithin 和 distance_lt 之间的区别?

发布于 08-21 01:41 字数 222 浏览 8 评论 0原文

有什么区别

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

使用geoDjango,和

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


它们是同一件事,还是做着细微不同的事情?

Using geoDjango, what is the difference between

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

and

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

?
Are they the same thing, or are they doing subtly different things?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动听の歌2024-08-28 01:41:02

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

  • 我查看了 单元测试,他们用来测试数据库查询,但他们没有给出真正的提示(对我来说)。

  • 我尝试比较生成的 SQL:

我已经有一个使用 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< /strong>,我收到错误:

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)

Summary:

<强>__dwithin
- 将度数值作为距离参数。
- 使用ST_DWithin SQL 函数。

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

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

Ok, I did some research but I don't know if the results are of any use ;)

  • I looked at the unit tests that they use to test the DB queries but they don't give real hints (to me).

  • I tried to compare the generated SQL:

I have already a geo application using a PostgreSQL databse. When I perform this query with __distance_lt:

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

I get this generated SQL:

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

When I try to use do to the same with __dwithin, I get an error:

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.

So I had to change the query to no use a D object:

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

which results in

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

Summary:

__dwithin
- takes degree values as distance parameter.
- uses ST_DWithin SQL function.

__distance_lt
- can take other distance values ;).
- uses ST_distance_sphere SQL function.

Btw, I get different results with both queries but I guess this is mostly due the fact that I don't know which "degree" value to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文