0
我有一個任務將csv文件讀入字典,然後根據幾個給定的zipcode查詢字典中的座標值。然後如果郵政編碼在「中心」郵編的50英里範圍內則返回。以弧度轉換座標時遇到了很多麻煩。此外,如果任何人都可以指出我正確的方向,以確定它們是否在50英里之內。這是我到目前爲止的代碼:使用python的地理位置
import sys
import csv
import math
dicts = {}
origin =[]
ziplist = ['12481', '10001', '12203', '10303', '12561']
center ='12401'
def getLatRad(latitude):
return float(latitude) * (math.pi/180.0)
def getLongRad(longitude):
return float(longitude) * (math.pi/180.0)
def getnearbylist():
try:
f = open("zips.csv")
csvParser = csv.reader(f)
for row in csvParser:
zipcode= row[0].strip()
latitude= row[2].replace('"', '').strip()
longitude=row[3].replace('"', '').strip()
dicts[zipcode] = {'latitude': latitude, 'longitude':longitude}
matched = {match: dicts[match] for match in ziplist if match in dicts}
for k in matched:
latRad2 = getLatRad(k[1])
longRad2 = getLongRad(k[2])
print latRad2
print longRad2
if center in dicts:
origin ={}
origin = dicts[center]
latRad1 = getLatRad(origin[1])
longRad1 = getLongRad(origin[2])
print latRad1
except ValueError:
pass
getnearbylist()
原點[X]心不是工作,我不具備Python很廣闊的認識,從而會有人能夠幫助我解決如何這些座標轉換。
樣本數據:
"zipcode", "state abbreviation", "latitude", "longitude", "city", "state"
"35004", "AL", " 33.606379", " -86.50249", "Moody", "Alabama"
"35005", "AL", " 33.592585", " -86.95969", "Adamsville", "Alabama"
"35006", "AL", " 33.451714", " -87.23957", "Adger", "Alabama"
"35007", "AL", " 33.232422", " -86.80871", "Alabaster", "Alabama"
"35010", "AL", " 32.903432", " -85.92669", "Alexander City", "Alabama"
"35014", "AL", " 33.355960", " -86.27720", "Alpine", "Alabama"
"35016", "AL", " 34.323715", " -86.49278", "Arab", "Alabama"
"35019", "AL", " 34.292540", " -86.63505", "Baileyton", "Alabama"
你可以張貼CSV幾行? – kaveman 2012-03-03 02:03:11
如果我們不知道你有什麼信息和你沒有什麼信息,那麼你不能期望我們能夠幫助你。 – 2012-03-03 02:11:49
http://pastebin.com/nXA0uFG1 – matture 2012-03-03 02:14:34