美文网首页
基于地理位置的即时跟踪分析

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

作者: vlnk2012 | 来源:发表于2016-02-19 20:35 被阅读71次

项目任务:一个最近的项目中,需要处理地理空间数据。给出(任务)是 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

相关文章

网友评论

      本文标题:基于地理位置的即时跟踪分析

      本文链接:https://www.haomeiwen.com/subject/intvkttx.html