# Calculating length of polyline defined by list of coordinates using Python

I have a list of coordinates, knowing that I am working using EPSG:4326 and using Python I would like to calculate the length in kilometres of the polyline defined by these coordinates.

``````Coords = [(0.0, 50.787944),
(0.0, 50.787944),
(-0.20159865271498856, 50.824569950535725),
(-0.40044683717220364, 50.803694),
(-0.599761967889834, 50.78975316549538)]
``````

I've tried with GeoPandas and shapely but reading the documentation I can't find an easy way to do it. Is there any function that allows me to calculate the length in kilometre knowing Coords and the reference system used?

• stackoverflow.com/questions/29545704/… Commented Aug 20, 2020 at 9:24
• @Taras trying to post the code that I wrote I end up finding the solution, I am not sure it is right but I've posted it as an answer. Thanks for your encouragement.
– G M
Commented Aug 20, 2020 at 9:42

Eventually, I have understood the steps to follow I had to convert the EPSG:4326 which has degrees as for units to a coordinate system in meters as EPSG:32618. So eventually I have end up following the instructions in the shapely documentation for transformations.

``````Coords = [(0.0, 50.787944),
(0.0, 50.787944),
(-0.20159865271498856, 50.824569950535725),
(-0.40044683717220364, 50.803694),
(-0.599761967889834, 50.78975316549538)]

polyline = LineString(Coords)
``````

After converting the list of coordinates to a shapely `LineString` I used `pyproj` for computing the transformation. I had initially a `attributeError : module 'pyproj' has no attribute 'CRS'` because I was using an old version.

``````import pyproj

from shapely.geometry import Point
from shapely.ops import transform

wgs84 = pyproj.CRS('EPSG:4326')
utm = pyproj.CRS('EPSG:32618')
utm_polyline = transform(project, wgs84_pt)
length_km = utm_polyline.length/1000
``````

If using pyproj < 2.1:

``````from functools import partial
import pyproj

from shapely.ops import transform

wgs84 = pyproj.Proj(init='epsg:4326')
utm = pyproj.Proj(init='epsg:32618')

project = partial(
pyproj.transform,
wgs84,
utm)

utm_polyline = transform(project, polyline)
length_km = utm_polyline.length/1000
``````
• You used the wrong UTM zone. UTM zone 18N is documented for "Between 78°W and 72°W, northern hemisphere between equator and 84°N, onshore and offshore. Bahamas. Canada - Nunavut; Ontario; Quebec. Colombia. Cuba. Ecuador. Greenland. Haiti. Jamica. Panama. Turks and Caicos Islands. United States (USA). Venezuela." I wouldn't use UTM for the UK, since it straddles zones 29&30. Commented Aug 20, 2020 at 12:15
• @Vince thanks a lot, I did this for an experiment in my thesis in surface metrology so I am not very familiar with CRS should I use EPSG:27700? Please post an answer if you know a better way. Thanks a lot!
– G M
Commented Aug 20, 2020 at 12:28
• @Vince I was really impressed that you understood it was UK. Very good observation :-)
– G M
Commented Aug 20, 2020 at 12:29
• As a rule, you should probably use the projection the national mapping agency uses. 50E,0N is in the Indian Ocean, so it wasn't hard to figure you were at 50N,0E Commented Aug 20, 2020 at 13:44