使用python的地理位置

时间:2012-03-03 01:44:19

标签: python

我有一个分配,将csv文件读入字典,然后根据几个给定的zipcodes查询字典中的坐标值。如果邮政编码在“中心”邮政编码的50英里范围内,则返回。我在转换弧度坐标时遇到了很多麻烦。此外,如果有人能指出我正确的方向,以确定他们是否在50英里内。到目前为止,这是我的代码:

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 个答案:

答案 0 :(得分: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()
相关问题