I have a polyline shapefile that has only one polyline.
How do I calculate the length of that polyline using ArcPy?
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.
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]
Code:
import arcpy
print([f[0] for f in arcpy.da.SearchCursor(r"C:\GIS\Lines.shp", 'SHAPE@LENGTH')][0])
North_America_Lambert_Conformal_Conic WKID: 102009in ArcGIS. More links: https://gis.stackexchange.com/questions/78141/coordinate-system-for-calculating-distance-at-the-nation-scale and https://gis.stackexchange.com/questions/91007/geodesic-measurements-for-short-distances-throughout-us – Alex Tereshenkov Nov 20 '17 at 17:40