Geodjango距离查询未检索正确的结果 [英] Geodjango distance query not retrieving correct results

查看:87
本文介绍了Geodjango距离查询未检索正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据地理位置的相关性来检索一些帖子.
正如您在代码中看到的那样,我正在使用GeoDjango,并且该代码在视图中执行.
问题在于距离过滤器似乎被完全忽略了.

I am trying to retrieve some posts depending on their proximity geographically.
As you can see in the code I am using GeoDjango and the code is executed within a view.
The issue is that the distance filter seems to be completely disregarded.

当我检查查询集上的距离时,我得到了预期的距离(1m和18km),但不应检索18km的帖子.

When I check the distances on the queryset I get the expected distances (1m and 18km) but the 18km post should not have been retrieved.

def get(self, request, format=None):
    latlon=request.query_params
    pnt = GEOSGeometry('SRID=28992;POINT(%s %s)'%(latlon['lat'],latlon['lon']))
    posts = Post.objects.filter(point__distance_lte=(pnt, D(km=1)))
    serialized = PostSerializer(posts, many=True)
    for post in posts:
        distance = pnt.distance(post.point)
        print("Km distance:",distance * 100)
    return JsonResponse(serialized.data, safe=False)

结果:

Km distance: 0.00015231546206626192
Km distance: 18.317378752081577
[18/Jul/2017 13:24:35] "GET /api/posts/?lon=5.8372264&lat=51.8125626 HTTP/1.1" 200 144

推荐答案

我相信您的问题是由按相反的顺序引起的在创建点时latlon .

I believe that your problem arises from the reverse order of lat and lon in your point creation.

作为旁注,我更喜欢使用 Point() 用于创建点的方法:

As a side note, I prefer to use the Point() method for point creation:

ptn = Point(x=latlon['lon'], y=latlon['lat'], srid=28992)


由于以下评论而进行

我尝试了该初始化程序,对我来说似乎是正确的,谢谢.但是,它似乎并没有解决问题.默认距离单位几乎是十亿分之一,因为它可以正常工作. posts = Post.objects.filter(point__distance_lte=(pnt, 0.05))此搜索范围在5公里以内–埃文(Evan)

I tried that initializer, it seems correct to me thank you. However it doesn't seem to have solved the issue. It almost looks as the default distance unit is decameters because this works correctly. posts = Post.objects.filter(point__distance_lte=(pnt, 0.05)) this searches within a range of 5km – Evan

由于上述方法不能解决您的问题,因此我假设

Since the above didn't solve your problem, I will assume that the dwithin's know problem with Distance objects apply to the distance queries as well.

如上面链接的解决方案所述,几何字段默认为WGS84,其中degrees为默认测量单位.

As stated in the solution linked above, geometry fields default to WGS84 which have degrees as a default measurement unit.

0.05 degrees ~= 5 km,这就是您的解决方案正常运行的原因.

0.05 degrees ~= 5 km, and that is why your solution worked correctly.

也许应该通知geodjango团队?

Maybe the geodjango team should be informed of this?

这篇关于Geodjango距离查询未检索正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆