0

I have a polyline shapefile that has only one polyline.

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

closed as off-topic by PolyGeo Nov 20 '17 at 19:45

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

3

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

Code:

import arcpy
print([f[0] for f in arcpy.da.SearchCursor(r"C:\GIS\Lines.shp", 'SHAPE@LENGTH')][0])
  • 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.