我想檢索一些職位,取決於他們的地理位置鄰近。
正如您在代碼中看到的,我正在使用GeoDjango,代碼在視圖中執行。
問題是距離過濾器似乎被完全忽略。Geodjango距離查詢不檢索正確的結果
當我檢查查詢集的距離時,我得到預期的距離(1米和18公里),但18公里的職位不應該被檢索。
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
我試過這個初始化器,對我來說似乎更正確的謝謝你。但它似乎並沒有解決這個問題。它幾乎看起來像默認距離單位是十進制數,因爲這可以正常工作。 'posts = Post.objects.filter(point__distance_lte =(pnt,0。05))'這個搜索範圍在5km以內 – Evan
@Evan正如我在這裏看到的:https://epsg.io/28992,默認單位是米。我編輯了我的答案,包括對該問題的可能解釋。 –