开发者

Geography mode distancies in GeoDjango - PostGIS 1.5

I st开发者_StackOverflow社区ore a PointField field named "coordinates" in a model.

Then, I consult nearest instances from a given one, in the command interpreter, and print its name and the distance in km.

[(p.name, p.distance.m) for p in Model_Name.objects.filter(
  coordinates__distance_lte=(pnt, 1000000)).distance(pnt)]

The thing is, when the field "coordinates" is of kind geometry, it works well. But if I include the option "geography=True", to get better precision, it returns a much smaller value, even that I am indicating to print it in km as before.

How can I get correct geography calculations?

Thanks


Appending .distance() to the end of the query set would result in each object in the geoqueryset being annotated with a distance object. And you can use that distance object to get distances in different units.

Because the distance attribute is a Distance object, you can easily express the value in the units of your choice. For example, city.distance.mi is the distance value in miles and city.distance.km is the distance value in kilometers

However in django 1.9 they moved the goal posts, while appending .distance() still works, now the recommended way is to do

from django.contrib.gis.db.models.functions import Distance
[  (p.name, p.distance.m) for p in Model_Name.objects.filter(
   coordinates__distance_lte=(pnt, 1000000)
   ).annotate(distance=Distance('point', pnt))]

Finally, instead of using coordinates__distance_lte there is a much faster method available in postgis and mysql 5.7: dwithin

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜