# Using geopy.distance.great_circle to calculate distance between some trajectory points

I am new to python for a month and doing an assignment with geopy.
I am asked to define a function named Cal_Distance(self) , which returns the distance of the whole trajectory in meter. Use a for loop to traverse all trajectory points to calculate the great-circle distance between each two consecutive points, then sum them up and return total distance of the whole trajectory.

Here is a part of my code.

``````    def Cal_Distance(self):
import geopy.distance as geo
for x in self.data:

for y in x:
self.data.append(y)
z1 = int(y)
z2 = int(y+1)
point1_lat = self.data[z1]
point2_lat = self.data[z2]
point1_lon = self.data[z1]
point2_lon = self.data[z2]
point_start = (point1_lat, point1_lon)
point_end = (point2_lat, point2_lon)

dis = geo.great_circle(point_start, point_end).meters

return dis
``````

The self.data list is similar to the following one.
[[39.99473, 116.3075133, 236.2, ‘2009-04-14’, ‘07:33:07’], [39.9947383, 116.3075199, 246.1, ‘2009-04-14’, ‘07:33:08’], [39.9947099, 116.3074466, 328.1, ‘2009-04-14’, ‘07:33:11’], [39.9947116, 116.3074633, 347.8, ‘2009-04-14’, ‘07:33:13’]]

The error shows up like this
ValueError: invalid literal for int() with base 10: ‘2009-04-14’

How can I solve it? Thanks a lot!!

Don’t call int() on date strings. Please post the complete traceback between triple backtick lines, the same as you did for the code.

Appending items of members of data to data while iterating through data does not make sense. If you did not get the exception, I would expect an infinite loop.

The expressions inside the loop suggest an intension that seems to be based on the assumption that `for y in x` would give `y` values that are indexes: `0,1,2,...`. Instead, `y` is taking the actual values inside `x`.

The `x` is taking values that are lists, like the `[39.99473, 116.3075133, 236.2, ‘2009-04-14’, ‘07:33:07’]` in your example. Then the `y` is taking the values inside it, like `39.99473`, and eventually the `2009-04-14` that gives the error.

I think you want to be taking consecutive elements of `self.data`. I think it could be something like this

``````import itertools
import geopy.distance as geo

firsts, seconds = itertools.tee(self.data)
next(seconds, None)
total_distance = 0.0
# Here datum1 and datum2 will be ranging over consecutive entries of self.data
for datum1, datum2 in zip(firsts, seconds):
# Here we are taking the first two values of each, latitude and longitude
point_start = datum1[:2]
point_end = datum2[:2]
dis = geo.great_circle(point_start, point_end).meters
total_distance += dis
``````