2012-03-03 63 views
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" 
+1

你可以張貼CSV幾行? – kaveman 2012-03-03 02:03:11

+0

如果我們不知道你有什麼信息和你沒有什麼信息,那麼你不能期望我們能夠幫助你。 – 2012-03-03 02:11:49

+0

http://pastebin.com/nXA0uFG1 – matture 2012-03-03 02:14:34

回答

0
import sys 
import csv 
from math import radians,sin,cos,asin,sqrt 

def haversine(lat1,lon1,lat2,lon2,radius=3959): 
    lat1,lon1,lat2,lon2 = map(radians, [lat1,lon1,lat2,lon2]) 
    dlat = lat2 - lat1 
    dlon = lon2 - lon1 
    return 2*radius*asin(sqrt(sin(dlat/2)**2 + cos(lat1)*cos(lat2)*sin(dlon/2)**2)) 

class Zipcodes(): 
    def __init__(self, fname="zips.csv"): 
     self.zips = {} 
     with open(fname) as inf: 
      r = csv.reader(inf) 
      header = r.next() # discard header row 
      for row in r: 
       zip = row[0].strip() 
       lat = float(row[2].replace('"','').strip()) 
       lon = float(row[3].replace('"','').strip()) 
       self.zips[zip] = (lat,lon) 
    def dist(self, zip1, zip2): 
     try: 
      lat1, lon1 = self.zips[zip1] 
      lat2, lon2 = self.zips[zip2] 
      return haversine(lat1,lon1,lat2,lon2) 
     except KeyError: 
      return 15000.0 

def main(): 
    z = Zipcodes() 

    # you figure out what goes here! 
    print z.dist('35004','35007') 

if __name__=="__main__": 
    main()