Python 为什么测地线返回0.0?

Python 为什么测地线返回0.0?,python,python-3.x,geopy,Python,Python 3.x,Geopy,我已经学了4个小时Python了,所以这可能是一个非常愚蠢的问题 我不太明白为什么测地线返回0.0表示我提供的两个坐标之间的距离 import csv from geopy.distance import geodesic with open('sample_data.csv', mode='r') as csv_file: csv_reader = csv.DictReader(csv_file) line_count = 0 coordinate_set = 1 coordinat

我已经学了4个小时Python了,所以这可能是一个非常愚蠢的问题

我不太明白为什么测地线返回0.0表示我提供的两个坐标之间的距离

import csv
from geopy.distance import geodesic

with open('sample_data.csv', mode='r') as csv_file:

csv_reader = csv.DictReader(csv_file)

line_count = 0

coordinate_set = 1

coordinates_1 = 0
coordinates_2 = 0

for row in csv_reader:

    if line_count == 0:
        line_count += 1
        continue

    else:

        print("------------------------------------")
        print(line_count)

        if coordinate_set == 1:
            # added cast to float as per recommendation from IftahP
            coordinates_1 = (float(row['Lat']),float(row['Long']))
            # coordinate_set_1 = (41.49008, -71.312796)
            print("Set 1: ")
            print(coordinate_set_1)
            coordinate_set = 2

        if coordinate_set == 2:
            # added cast to float as per recommendation from IftahP
            coordinates_2 = (float(row['Lat']),float(row['Long']))
            #coordinate_set_2 = (41.499498, -81.695391)
            print("Set 2: ")
            print(coordinates_2)

            ## Distance between the 2 coordinates
            print(coordinates_1,coordinates_2)
            print(geodesic(coordinates_1,coordinates_2).miles)

            coordinate_set = 1  

    line_count += 1
这是输出

------------------------------------
1
Set 1: 
('26.0595233', '-80.1332766')
Set 2: 
(25.458595, -80.20754)
(25.458595, -80.20754) (25.458595, -80.20754)
0.0
------------------------------------
2
Set 1: 
('26.0595233', '-80.1332766')
Set 2: 
(25.4586116, -80.207505)
(25.4586116, -80.207505) (25.4586116, -80.207505)
0.0
------------------------------------
3
Set 1: 
('26.0595233', '-80.1332766')
Set 2: 
(25.4586083, -80.2075033)
(25.4586083, -80.2075033) (25.4586083, -80.2075033)
0.0
------------------------------------
4
Set 1: 
('26.0595233', '-80.1332766')
Set 2: 
(25.4585966, -80.2075216)
(25.4585966, -80.2075216) (25.4585966, -80.2075216)
0.0
------------------------------------
5
Set 1: 
('26.0595233', '-80.1332766')
Set 2: 
(25.4585883, -80.20755)
(25.4585883, -80.20755) (25.4585883, -80.20755)
0.0
------------------------------------
6
Set 1: 
('26.0595233', '-80.1332766')
Set 2: 
(25.4585833, -80.2075316)
(25.4585833, -80.2075316) (25.4585833, -80.2075316)
0.0
------------------------------------
7
Set 1: 
('26.0595233', '-80.1332766')
Set 2: 
(25.4585833, -80.207495)
(25.4585833, -80.207495) (25.4585833, -80.207495)
0.0
------------------------------------
8
Set 1: 
('26.0595233', '-80.1332766')
Set 2: 
(25.4585783, -80.2074866)
(25.4585783, -80.2074866) (25.4585783, -80.2074866)
0.0
------------------------------------

我“硬编码”了上面提供的坐标,当我使用它们时,它会显示距离。它只是不喜欢我输入的坐标。没有显示任何错误消息

问题:
坐标1
坐标2
都是从同一行
读取的(因此距离始终等于0)

解决方案是将开关
坐标设置
-值从
移动到
-循环:

for row in csv_reader:
    if coordinate_set == 1:
        coordinates_1 = (float(row['Lat']),float(row['Long']))

        # coordinate_set = 2 ## 1st source of error

    if coordinate_set == 2:
        coordinates_2 = (float(row['Lat']),float(row['Long'])) # the same row is readen twice
        print(geodesic(coordinates_1,coordinates_2).miles)
        # coordinate_set = 1  ## 2nd error

    coordinate_set = 2 if (coordinate_set==1) else 1 # Correct switcher
    line_count += 1

坐标不是数字,而是字符串。使用浮动(行['Lat']),行['Long']使用相同的浮动。另请参见@IftahP实际也接受字符串:
print(测地线('26.0','-80.1'),('25.4','-80.2')).miles,测地线((26.0,-80.1),(25.4,-80.2)).miles)
如果添加:
print(坐标1,坐标2)
@AlexYu输出它:-------------------------------------1集1:('26.0595233','-80.1332766')第二组:(25.458595,-80.20754)(25.458595,-80.20754)(25.458595,-80.20754)0.0-----------------------------------------------------------------啊哈!
(25.458595,-80.20754)(25.458595,-80.20754)
-所以它实际上是相同的坐标。你从同一行中读取相同的坐标两次,比较它们,然后用下一行重复一遍。谢谢。那是个愚蠢的错误……我怎么会错过这个?啊。谢谢!@InsuredApple你可以用更优雅和更通俗的方式来做这件事:
enumerate
csv中的所有行,在一个列表中合并奇数行和非奇数行,计算它们之间的距离。