Geodjango距离查询未检索正确的结果

编程入门 行业动态 更新时间:2024-10-13 22:22:07
本文介绍了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

推荐答案

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

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

由于上述方法不能解决您的问题,因此我假设 dwithin Distance对象的已知问题也适用于distance查询.

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距离查询未检索正确的结果

本文发布于:2023-10-18 15:03:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   距离   Geodjango

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!