2013-06-03 38 views
0

所以我有一个代码给予每个数据集的名称

print 
latOne = dL[1][3] 
lonOne = dL [1][4] 
x = [calculateDistance(latOne, lonOne, latTwo, lonTwo) for latTwo, lonTwo in zip(latitude, longitude)] 
print x 

这产生列表中的距离值与所述输出

[0.0, 3043.004178666758, 21558.2996208357, 40246.748450913066, 40908.82213277263, 43786.0579097594, 67781.1426515405, 79693.11338661514, 65046.35819797423, 92804.01912347642] 

现在每个距离是基于从一个单独的点坐标(其中有一个定义的名称) 即

Sydney (-20.7, 100) 
Melbourne (-20, 120) 

所以我的代码需要的距离确定并产生距离列表。我想要做的是设置结构,以便使用相关的点名打印距离。即采取LAT和悉尼多头计算距离,然后让输出中是

Distance to Syndey is output ..., Distance to Melbourne is output, and so on. 

如果u需要我更多的代码,以帮助请让我知道。

编辑:

这里是完整的脚本代码。

import math 
import csv 
def calculateDistance(latOne, lonOne, latTwo, lonTwo): 


from decimal import Decimal 
latOne, lonOne, latTwo, lonTwo = [Decimal(x) for x in (latOne, lonOne, latTwo,lonTwo)] 
DISTANCE_CONSTANT = 111120.0 
coLat = math.fabs(lonOne - lonTwo) 
alpha = 90 - latTwo 
beta = 90 - latOne 

cosAlpha = math.cos(math.radians(alpha)) 
cosBeta = math.cos(math.radians(beta)) 
sinAlpha = math.sin(math.radians(alpha)) 
sinBeta = math.sin(math.radians(beta)) 
cosC  = math.cos(math.radians(coLat)) 

cos_of_angle_a = (cosAlpha * cosBeta) 
cos_of_angle_b = (sinAlpha * sinBeta * cosC) 
cos_of_angle_c = cos_of_angle_a + cos_of_angle_b 
angle   = math.degrees(math.acos(cos_of_angle_c)) 
distance  = angle * DISTANCE_CONSTANT 
return distance 

stations = [] 
latitude = [] 
longitude = [] 
with open('data.csv', 'rU') as input: 
     dL= list(csv.reader(input)) 
     sL = [row[4] for row in dL[1:]] 
     longitude.extend(sL) 
     sL1 = [row[3] for row in dL[1:]] 
     latitude.extend(sL1) 
     sL2 = [row[1] for row in dL[1:]] 
     stations.extend (sL2) 

data = [] 
print "Station Coordinates" 
for i in range(0, len(latitude)): 
    print str(stations[i]) + "(" + str(latitude[i]) + "," + str(longitude[i]) + ")" 
    ab = str(stations[i]) + "(" + str(latitude[i]) + "," + str(longitude[i]) + ")" 
    data.append(ab) 

print stations   

print 
latOne = dL[1][3] 
lonOne = dL [1][4] 
x = [calculateDistance(latOne, lonOne, latTwo, lonTwo) for latTwo, lonTwo in zip(latitude, longitude)] 
print x 


print 
lessthan, greaterthan = [], [] 
knowndistance = float(raw_input("Please type your radius")) 
for v in x: 
if v <= knowndistance: 
    lessthan.append(v) 
else: 
    greaterthan.append(v) 

print lessthan 
print greaterthan 
+0

那你到底需要什么帮助?目前还不清楚......我用这种东西处理了很多东西,但是我无法真正知道你需要什么帮助...... –

+0

你能举一个例子来说明dL的外观吗?它可以像在dL中循环项目一样简单,并将字典中的距离和相应的城市存储在一个字典 –

+0

@RyanSaxe以及我想要帮助的是使用代码。这将获得距离,我想打印距离加上距离所属的电台名称。这更清楚吗? – user2430623

回答

0

首先,你真的应该考虑geopy.distance以及很多它的其他的内置功能。是的,你已经写了他们的算法之一,但它可能会有其他的东西,你会发现你需要。

所以你打算在这里使用字典。如果您可以保存连接到车站的车站的经度和纬度,它会让您的生活变得非常轻松!

points = [[row[3],row[4]] for row in dL] 
stations = [row[1] for row in dL] 
my_dict = dict(zip(stations,points)) 

现在你有一个字典,你可以通过站名打电话给你的纬度和经度并将其存储在这样一种方式,你可以只是后来回忆。我认为这就是完成你的代码所需要的。如果不是,请评论,以便我可以对其进行相应编辑。

相关问题