Extract value from a one line shape file arcpy

527
4
Jump to solution
03-17-2021 01:27 AM
Labels (3)
MohamadAchour
New Contributor III

Hello,

I have a shapefile that contains one line, and I need to extract the "Shape Length" value from his attribute table using python.

Any suggestion?

MohamadAchour_0-1615969516175.png

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

a search cursor will do the trick SearchCursor—ArcGIS Pro | Documentation

 

import arcpy

fc = 'c:/data/your_shapefile.shp'
#look at the geometry tokens in the help url
#for more understanding
fields = ['SHAPE@LENGTH']

#open cursor
with arcpy.da.SearchCursor(fc, fields) as cursor:
    #for every row in the shapefiles attribute table
    for row in cursor:
        #row[0] is the first field specified when opening the cursor
        #next field rould be row[1]...etc

        #print the length
        print(row[0])

View solution in original post

4 Replies
JayantaPoddar
MVP Esteemed Contributor

Where do you want to extract it to?

Using Python Parser of Field Calculator?

!shape.length@<units>!

e.g.: For calculating length in yards

!shape.length@yards!

Geometry Calculation 



Think Location
MohamadAchour
New Contributor III

I only need the value of the "shape length" cell. Then, I will use this value in the script.

0 Kudos
MohamadAchour
New Contributor III

if there is a method to calculate the length of the polyline also it can work.

0 Kudos
DavidPike
MVP Frequent Contributor

a search cursor will do the trick SearchCursor—ArcGIS Pro | Documentation

 

import arcpy

fc = 'c:/data/your_shapefile.shp'
#look at the geometry tokens in the help url
#for more understanding
fields = ['SHAPE@LENGTH']

#open cursor
with arcpy.da.SearchCursor(fc, fields) as cursor:
    #for every row in the shapefiles attribute table
    for row in cursor:
        #row[0] is the first field specified when opening the cursor
        #next field rould be row[1]...etc

        #print the length
        print(row[0])