I have a polyline shapefile that has only one polyline.

How do I calculate the length of that polyline using ArcPy?


You can use the shape tokens: this is the fastest and arguably the least expensive way to get the length of a feature.

Make sure though that you're accessing the distance values using a proper coordinate system so the values make sense. For instance, if your polyline is located geographically in California, US, and stored using geographic coordinate system WGS84 (EPSG 4326), then you could, for instance, use a projected coordinate system NAD_1983_California_Teale_Albers (EPSG 3310) to re-project the feature on-the-fly getting the proper length value. This is done by supplying a spatial reference object to the cursor.

  1. You can run this code in ArcMap Python window (provided that the name of the shapefile is Lines.).

[f[0] for f in arcpy.da.SearchCursor(r"C:\GIS\Lines.shp", 'SHAPE@LENGTH')][0]

Or in another coordinate system:

[f[0] for f in arcpy.da.SearchCursor(r"C:\GIS\Lines.shp", 'SHAPE@LENGTH', spatial_reference=arcpy.SpatialReference(3310))][0]

  1. Or as a standalone script


import arcpy
print([f[0] for f in arcpy.da.SearchCursor(r"C:\GIS\Lines.shp", 'SHAPE@LENGTH')][0])
| improve this answer | |
  • How do I change the units of the length? – pnkjmndhl Nov 20 '17 at 17:18
  • 1
    @pnkjmndhl, updated the answer! – Alex Tereshenkov Nov 20 '17 at 17:22
  • Thanks for the update, my current coordinate system is WGS1984. However, the polyline can lie anywhere in the US. So, I cant project in a specific state. Can I still project to a fixed projection and still get a reliable distance? – pnkjmndhl Nov 20 '17 at 17:27
  • 1
    I am not the best one to talk to regarding the choice of projections for the US, but I think you should be fine with the North_America_Lambert_Conformal_Conic WKID: 102009 in ArcGIS. More links: gis.stackexchange.com/questions/78141/… and gis.stackexchange.com/questions/91007/… – Alex Tereshenkov Nov 20 '17 at 17:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.