TransWikia.com

Calculating length of polyline defined by list of coordinates using Python

Geographic Information Systems Asked on September 5, 2020

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?

One Answer

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

Answered by G M on September 5, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP