基于地理位置的即时跟踪分析

项目任务:一个最近的项目中,需要处理地理空间数据。给出(任务)是 gps 追踪 25,000 个左右位置点,需要根据给定的经纬度,重复定位距离最短的点。

解决方案:

1. 翻查(已经实现的)计算已知经纬度两点间距离的代码片段。代码可以在 John D.  Cook 写的这篇code available in the public domain中找得到。

2. 只要写一段 Python 函数,返回与输入坐标距离最短的点索引(25,000 点数组中的索引)即可

代码是:

def closest_distance(lat,lon,data):

d = 100000.0

best = -1

r = data.index #数据索引最大值?

#根据输入的lat和lon,查找其在data数据中的索引位置对应的经度和纬度,传递为两个变量作为输出

for i in r:

lati = data.ix[i,'Lat']

loni = data.ix[i,'Lon']

#distance_on_unit_sphere是John D. Cook's书中的函数

md = distance_on_unit_sphere(lat, lon, lati, loni)#计算两组数之间的地理距离

if d > md#如果计算的距离在允许范围内

best = i#将数据的索引反馈到i通道输出

d= md#将计算的距离发送给d通道作为输出

return best

#输出best通道的产物-----即计算距离在数据中的索引

遍历(迭代)trkpts数组,将迄今为止(距离给定坐标位置)的距离最短的点索引值,保存到本地变量best中。

来自 <http://www.oschina.net/translate/python_is_not_c>

问题:代码写起来快,但执行起来却很慢。

Computing the distance between two locations on Earth from coordinates

结合地理坐标计算两个位置之间的距离

具体代码:代码写起来快,但执行起来却很慢。

import math

def distance_on_unit_sphere(lat1, long1, lat2, long2):

# Convert latitude and longitude to  转化经纬度为球面坐标

degrees_to_radians = math.pi/180.0   计算一个固定值

# phi = 90 - latitude

phi1 = (90.0 - lat1)*degrees_to_radians  #计算两个动态角度

phi2 = (90.0 - lat2)*degrees_to_radians

# theta = longitude

theta1 = long1*degrees_to_radians

theta2 = long2*degrees_to_radians

# Compute spherical distance from spherical coordinates.

# For two locations in spherical coordinates (1, theta, phi) and (1, theta', phi') 

# cosine( arc length ) = sin phi sin phi' cos(theta-theta') + cos phi cos phi'

# distance = rho * arc length cos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) +

math.cos(phi1)*math.cos(phi2))

arc = math.acos( cos )

# Remember to multiply arc by the radius of the earth

# in your favorite set of units to get length.

return arc

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • #1996 AHSME ##1996 AHSME Problems/Problem 1 The addition ...
    abigtreenj阅读 5,353评论 0 0
  • /** * 地图位置计算工具(将GPS坐标转换成百度地图坐标) * 参考文档:http://bbs.lbsyu...
    nick2046阅读 7,706评论 0 1
  • 今天,在一篇报道中发现,“一直特立独行的猫”微博、公众号都很火的那位------80后女作家赵星,原来是老乡,兴...
    清风淡雅11阅读 1,664评论 0 0
  • //www.greatytc.com/p/a290ffb302d6?utm_campaign=haruki...
    懂茶帝老宋阅读 1,397评论 0 0