GeoDjango, difference between dwithin and distance_lt?

ぐ巨炮叔叔 提交于 2019-12-31 19:28:17

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/2235043/geodjango-difference-between-dwithin-and-distance-lt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!