0

I'm about 4 hours into Python, so this is probably a really silly question.

I don't quite understand why geodesic is returning 0.0 for the distance between the 2 coordinates I provide.

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

Here is the output...

------------------------------------
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
------------------------------------

I "hard coded" the coordinates provided on https://pypi.org/project/geopy/ and when I use them it shows a distance. It just doesn't like the coordinates I feed it. There is no error message that is displayed.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
InsuredApple
  • 63
  • 1
  • 3
  • 12
  • Your coordinates are not numbers, but strings. Use float(row['Lat']) and same for row['Long']. See also [here](https://stackoverflow.com/questions/4643991/python-converting-string-into-decimal-numbe) – LemonPy Feb 25 '19 at 22:08
  • @IftahP Actually strings are accepted too: `print(geodesic(('26.0', '-80.1'), ('25.4', '-80.2')).miles, geodesic((26.0, -80.1), (25.4, -80.2)).miles)` – Alex Yu Feb 25 '19 at 22:21
  • Can you clarify what well became if you add: `print(coordinates_1,coordinates_2)` – Alex Yu Feb 25 '19 at 22:23
  • @AlexYu the output it: ------------------------------------ 1 Set 1: ('26.0595233', '-80.1332766') Set 2: (25.458595, -80.20754) (25.458595, -80.20754) (25.458595, -80.20754) 0.0 ------------------------------------ – InsuredApple Feb 25 '19 at 23:00
  • Aha! `(25.458595, -80.20754) (25.458595, -80.20754)` - so it's really the same coordinates. You read the same coordinates from the same `row` **twice**, compare them and repeat it again with next `row` – Alex Yu Feb 25 '19 at 23:10
  • @InsuredApple Please check my answer. It seems very simple – Alex Yu Feb 25 '19 at 23:23

1 Answers1

1

Problem: coordinate_1 and coordinate_2 both are read from the same row (so distance is always equals 0)

Solution will be to move switch coordinate_set-value from ifs in row-cycle:

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
Alex Yu
  • 3,412
  • 1
  • 25
  • 38
  • Ugh thank you. That was such a stupid mistake... how did I miss this? ugh. Thank you! – InsuredApple Feb 25 '19 at 23:43
  • @InsuredApple You can do this in much more elegant way and more pythonic way: `enumerate` all rows in csv, combine in one list comprehension **twice** odd- and non-odd- rows, calculate distance between them. – Alex Yu Feb 25 '19 at 23:48