Django - 如何找到两个位置之间的距离?
我在 Django 应用程序中注册了一些用户,我希望能够根据邮政编码计算出两个用户之间的地理距离,然后根据该距离对列表进行排序。我想这个功能没有内置到 Django 中。我正在考虑一些选项,并偶然发现了 geodjango,这似乎对于我的需求来说可能有点过分了。
I have some users registered in my Django app and I want to simply be able to figure out the distance, geographically, between two users based on their zip code and then sort a list based on that. I would imagine this functionality isn't built into Django. I was looking at some options and stumbled across geodjango which seems like it might be overkill for what my needs are.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是对 @Sven Marnach 的(当前接受的)答案中发布的代码的一个大注释。
来自zip项目网站的原始代码,由我编辑的缩进:
Sven发布的代码:
问题1:WON'T RUN:需要导入
acos
问题2:错误答案:需要转换
倒数第二行中的经度与弧度的差值
问题 3:变量名称“距离”是一个极端的用词不当。
这个量实际上是两条线之间角度的余弦
从地球中心到输入点。改为“cos_x”
问题4:不需要将角度x 转换为度数。简单地
将 x 乘以所选单位的地球半径(公里、纳米或“法定英里”)
解决所有这些问题后,我们得到:
注意:解决问题 1 和 2 后,这就是通常实施的“余弦球面定律”。
对于诸如“两个美国邮政编码之间的距离”之类的应用程序来说是可以的。
注意事项 1:对于像从前门到街道这样的小距离,它并不精确,以至于如果两个点相同,它可能会给出非零距离或引发异常 (cos_x > 1.0);这种情况可能是特殊情况。
注意事项 2:如果两个点是对映的(直线路径穿过地球中心),则可能会引发异常(cos_x < -1.0)。任何担心这一点的人可以在执行 acos(cos_x) 之前检查 cos_x。
示例:
SFO (37.676, -122.433) 到 NYC (40.733, -73.917)
calcDist -> 2570.7758043869976
calc_dist ->; 5038.599866130089
calc_dist_fixed ->; 2570.9028268899356
美国政府网站 (http://www.nhc.noaa.gov/gccalc.shtml) ->第2569章
这个网站(http://www.timeanddate.com/worldclock/distanceresult.html?p1=179&p2=224),我从中得到了SFO和NYC坐标,-> 2577
This is a big fat comment on the code posted in the (currently-accepted) answer by @Sven Marnach.
Original code from zip project website, with indentation edited by me:
Code posted by Sven:
Problem 1: WON'T RUN: needs to import
acos
Problem 2: WRONG ANSWERS: needs to convert the
longitude difference to radians in the second last line
Problem 3: The variable name "distance" is an extreme misnomer.
That quantity is actually the cos of the angle between the two lines
from the centre of the earth to the input points. Change to "cos_x"
Problem 4: It is not necessary to convert angle x to degrees. Simply
multiply x by earth's radius in chosen units (km, nm, or "statute miles")
After fixing all that, we get:
Note: After fixing problems 1 and 2, this is the "spherical law of cosines" as usually implemented.
It is OK for applications like "distance between two US zipcodes".
Caveat 1: It is not precise for small distances like from your front door to the street, so much so that it can give a non-zero distance or raise an exception (cos_x > 1.0) if the two points are identical; this situation can be special-cased.
Caveat 2: If the two points are antipodal (straight line path passes through the center of the earth), it can raise an exception (cos_x < -1.0). Anyone worried about that can check cos_x before doing acos(cos_x).
Example:
SFO (37.676, -122.433) to NYC (40.733, -73.917)
calcDist -> 2570.7758043869976
calc_dist -> 5038.599866130089
calc_dist_fixed -> 2570.9028268899356
A US government website (http://www.nhc.noaa.gov/gccalc.shtml) -> 2569
This website (http://www.timeanddate.com/worldclock/distanceresult.html?p1=179&p2=224), from which I got the SFO and NYC coordinates, -> 2577
根据 tcarobruce 的建议,以下是我的上述评论作为答案:
邮政编码数据库项目 有一个纬度数据库美国邮政编码的经度(SQL 或 CSV 形式)。他们还提供了以下距离计算代码(由我稍作编辑):
请注意,结果以法定英里为单位给出。
编辑:约翰·梅钦 (John Machin) 的更正。
Following tcarobruce's suggestion, here is my above comment as an answer:
The Zip Code Database Project has a database of the latitudes and longitudes of the US zip codes, either as SQL or as CSV. They also provide the following code for distance calculation (slighlty edited by me):
Note that the result is given in statute miles.
Edit: Corrections due to John Machin.
http://code.google.com/apis/maps/documentation/directions/
您可以为每个地点提供指示。给出了总距离。 API似乎输出JSON;您可以在服务器端解析答案,也可以通过 JavaScript 计算距离。
http://code.google.com/apis/maps/documentation/directions/
You could do directions for each location. The total distance is given. The API seems to output JSON; you could either parse the answer on the server side or have the distance calculated by JavaScript.
另一种简单的方法:
下面的函数在根据邮政编码计算纬度和经度后返回两个位置之间的距离。
lat1
、long1
是第一个位置的纬度和经度。lat2
、long2
是第二个位置的纬度和经度。Another simple way:
Below function returns distance between two location after calculating latitudes and longitudes from zipcode.
lat1
,long1
are the latitudes and longitudes of first location.lat2
,long2
are the latitudes and longitudes of second location.