# Using geopy.distance.great\_circle to calculate distance between some trajectory points

**URL:** <https://discuss.python.org/t/using-geopy-distance-great-circle-to-calculate-distance-between-some-trajectory-points/23932>\
**Category:** Python Help\
**Tags:** help\
**Created:** [February 16, 2023, 10:03am UTC](https://discuss.python.org/t/using-geopy-distance-great-circle-to-calculate-distance-between-some-trajectory-points/23932 "2023-02-16T10:03:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carmnn](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@Carmnn](https://discuss.python.org/u/Carmnn)\
**Post date:** [February 16, 2023, 10:03am UTC](https://discuss.python.org/t/using-geopy-distance-great-circle-to-calculate-distance-between-some-trajectory-points/23932/1 "2023-02-16T10:03:36Z")

</div>

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.

```auto
    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][0]
                point2_lat = self.data[z2][0]
                point1_lon = self.data[z1][1]
                point2_lon = self.data[z2][1]
                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!!

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [February 16, 2023, 7:13pm UTC](https://discuss.python.org/t/using-geopy-distance-great-circle-to-calculate-distance-between-some-trajectory-points/23932/2 "2023-02-16T19:13:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![franklinvp](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/franklinvp/32/11142_2.png) [@franklinvp](https://discuss.python.org/u/franklinvp)\
**Post date:** [February 16, 2023, 10:31pm UTC](https://discuss.python.org/t/using-geopy-distance-great-circle-to-calculate-distance-between-some-trajectory-points/23932/3 "2023-02-16T22:31:51Z")

</div>

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

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

```
