如何从数据库获取最近的位置条目? [英] How to get the nearest location entries from a database?

查看:140
本文介绍了如何从数据库获取最近的位置条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将坐标作为纬度经度对存储在数据库中.现在,我需要从给定的经纬度坐标中检索在给定半径内的所有条目.例如,如果给定坐标48.796777、2.371140和距离20公里,它将检索到该点20公里以内的所有记录.

I store coordinates as latitude longitude pairs in my database. Now, I need to retrieve all the entries that are within a given radius from a given lat-long coordinate. For example, if given coordinates 48.796777, 2.371140 and distance of 20 kilometers, it would retrieve all the records within 20 kilometers from that point.

如果它提供了任何简化或减少了CPU时间,那么计算两点之间的直线"距离就足够准确了,也就是说,无需考虑地球的曲率.

If it provides any simplification or results in less CPU time, it's accurate enough to calculate the "straight" distance between the points, i.e. there's no need to factor in the curvature of the earth.

如何用Django实现?更具体地说,对于这样的查询,应该如何构造视图(可能还有模型)?

How can this be achieved with Django? More specifically, how should the view (and possibly the model) be constructed for such a query?

推荐答案

您应使用距离查询在您的地理数据库条目上.

You should use GeoDjango. It allows you to perform distance queries on your geographic database entries.

from django.contrib.gis.measure import D
from django.contrib.gis.geos import *
from myapp.models import MyResult

pnt = fromstr('POINT(48.796777 2.371140 )', srid=4326)
qs = MyResult.objects.filter(point__distance_lte=(pnt, D(km=20)))

这篇关于如何从数据库获取最近的位置条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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