如何使用Haversine公式计算csv文件中lat长位置的邻近度

时间:2017-11-30 01:36:51

标签: python-3.x

问题

我有多个gps lat / long坐标。我使用Haversine公式来计算用户输入lat1,lon1,lat2,lon2之间的距离。它目前告诉我两个地点之间的距离。

我希望用户不必手动输入它们。我希望用户输入一个位置id,它将查找lat / lon,然后将其传递给lat1和lon1。

然后我想让用户输入一个搜索半径,然后显示该附近的所有位置。

我显示用户输入的lat和lon的第一个代码

    with open("D:/Python/Data/rttData.csv") as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')

        enbIDs = []
        enbNames = []
        enbLats = []
        enbLons = []

        for row in readCSV:
            enbID = row[0]
            enbName = row[1]
            enbLat = row[2]
            enbLon = row[3]

            enbNames.append(enbName)
            enbIDs.append(enbID)
            enbLats.append(enbLat)
            enbLons.append(enbLon)

    siteSelection = input('\nEnter the eNB number below to search for [ex: 25.806.1.1]\n')
    enbIndex = enbIDs.index(siteSelection)
    theName = enbNames[enbIndex]
    theLat = enbLats[enbIndex]
    theLon = enbLons[enbIndex]
    print('The name of site', siteSelection, 'is:', theName,'the Latitude is:', theLat, 'and the Longitude is:',theLon)
    state = 3

我的第二个代码,用于计算4个用户输入之间的距离,用于定义lat1,lon1,lat2,lon2

def distance(point1, point2):

    lat1 = point1[0]
    long1 = point1[1]
    lat2 = point2[0]
    long2 = point2[1]

    long1, lat1, long2, lat2 = map(radians, [long1, lat1, long2, lat2])

    dlong = long2 - long1
    dlat = lat2 - lat1
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlong/2)**2
    c = 2 * asin(sqrt(a))
    r = 3959 #distance in miles around the earth
    return c * r

def get_location():
    lat = float(input('Enter the Latitude:'))
    long = float(input('Enter the Longitude:'))
    return (lat,long)


def main():

    user_selection = 'y'

    while user_selection == 'y':

        print('\nEnter the coordinates for your starting location below in decimal format.')

        point1 = get_location()
        print('\nEnter the coordinates for your ending location below in decimal format')

        point2 = get_location()
        dist = distance(point1, point2)
        rounded_dist = round(dist,2)

        print('\nThe distance between both locations is: {} miles'.format(rounded_dist))


if __name__ == '__main__':
  main()

最终输出应该如下所示

Hello用户,请输入网站:

  

25.806.1.1

网站25.806.1.1的名称是:ABQ_BEARCANYON纬度为:35.135056,经度为:-106.53475

请输入以英里为单位的邻近搜索:

  

20

20英里范围内的网站是:

Site 1:       5.23 miles

Site 2:      17.64 miles

Site 3:      19.88 miles

Site 4:      19.97 miles

0 个答案:

没有答案