AnsweredAssumed Answered

How to use Python to calculate street sq footage

Question asked by kfewell@eloyaz.gov on Dec 4, 2018
Latest reply on Dec 4, 2018 by kfewell@eloyaz.gov

Hello all,

I have a street centerline feature in a File Geodatabase with a width attribute. Using ArcGIS 10.5.1.

I'm simply trying to calculate the sq footage using a width attribute & the shape length and update "SQFT_LxW" field.

I know I can easily do this in ArcMap using calc field but I want a script so I can run it nightly.

I get the RuntimeError: An edit operation is required.

 

However if I remove the "read only" field SHAPE@LENGTH from the fields list and fudge the length variable (length = 5)

then it works.

So how do I get the Length of a polyline without calling it in the UpdateCursor?

Code below:

import arcpy

fc = r"//GDB/LocalGov.gdb/"
fields = ['Width', 'SQFT_LxW', 'SHAPE@LENGTH'] # This DOESN'T WORK
#fields = ['Width', 'SQFT_LxW']                                 # This WORKS!
layer = 'Centerlines'
workspace = fc[0:-1]
print workspace + "\n"
fc = fc + layer
print fc
try:
   arcpy.MakeFeatureLayer_management(fc, layer)
   with arcpy.da.Editor(workspace) as edit:
      with arcpy.da.UpdateCursor(fc, fields) as cursor:
         for row in cursor:
               if (row[0] is not None): #width field has a value
                     if row[0] > 0:
                        length = row[2]
                        sqft = int(length * row[0])
                        print sqft
                        row[1] = sqft
                     else: row[1] = None
               else: row[1] = None
               cursor.updateRow(row)
   print("updated!")
except arcpy.ExecuteError:
   print (arcpy.GetMessages(2))

Outcomes