calculate geometry values for specific fc type

2034
5
05-12-2016 03:08 PM
JustinWolff
Occasional Contributor

Wasn't sure how to name this post...

I have a geodatabase with multiple feature datasets, each containing multiple feature classes of various polygon, line and point geometry type.  Every polygon fc has a 'areaSize' field that I need to populate with square feet (all of the fds are in State Plane feet).

I think I'm getting close here, but still getting an error.  (Eventually I will expand this to perform a similar calculation for length of line fc types as well.

The error I receive is attached.  Any help is greatly appreciated.

Thanks.

#*********************************
#v1.0
#*********************************
import arcpy
from arcpy import env
import os

#Set variables
inputgdb = sys.argv[1]

#Set workspace
arcpy.env.workspace = inputgdb

#populate areaSize fields in polygon feature classes
datasetList = arcpy.ListDatasets(feature_type = 'feature')
datasetList = [''] + datasetList if datasetList is not None else []
print "Getting Feature Datasets..."
arcpy.AddMessage ("Getting Feature Datasets...")
print datasetList
arcpy.AddMessage (datasetList)

for dataset in datasetList:
    env.workspace = inputgdb + "\\" + dataset
    dataset = dataset + "\\"
    fcList = arcpy.ListFeatureClasses('*', 'polygon', '*')
    print "Getting Feature Classes..."
    arcpy.AddMessage ("Getting Feature Classes...")
    print "Feature Class list..."
    arcpy.AddMessage (fcList)
    
    for fc in fcList:
        fc = fc +"\\"
        fieldList = [f.name for f in arcpy.ListFields(fc, "areaSize")]
        with arcpy.da.UpdateCursor (fc, fieldList) as cursor:
            for row in cursor:
                row[1] = row[0].area
                cursor.updateRow(row)
                print "Updating 'areaSize' field in: " + fc
                arcpy.AddMessage ("Updating 'areaSize' field in: " + fc)
0 Kudos
5 Replies
DanPatterson_Retired
MVP Esteemed Contributor

you need more print statements, your script is returning an empty list, so you will have to back up a few lines and check your inputs.

and check your syntax and case ... etc Feature not feature

ListDatasets—Help | ArcGIS for Desktop

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I see a couple of larger, structural issues with the code, but I don't have time to elaborate at the moment.  Regarding the specific error message, you can make it go away by changing Line 25 to:

fcList = arcpy.ListFeatureClasses('*', 'polygon', '')

or

fcList = arcpy.ListFeatureClasses('*', 'polygon')

The ListFeatureClasses documentation covers the syntax of the tool.  There is no asterisk wildcard for the feature_dataset parameter.

0 Kudos
JustinWolff
Occasional Contributor

Dan and Josh - thank you very much for the nudge in the right direction.  It's still far from elegant, but it is now working:

#*********************************
#v1.0
#*********************************
import arcpy
from arcpy import env
import os
#Set variables
inputgdb = sys.argv[1]
#Set workspace
arcpy.env.workspace = inputgdb
#populate areaSize fields in polygon feature classes
datasetList = arcpy.ListDatasets(feature_type = 'Feature')
datasetList = [''] + datasetList if datasetList is not None else []
print "Getting Feature Datasets..."
arcpy.AddMessage ("Getting Feature Datasets...")
print datasetList
arcpy.AddMessage (datasetList)
for dataset in datasetList:
    env.workspace = inputgdb + "\\" + dataset
    dataset = dataset + "\\"
    fcList = arcpy.ListFeatureClasses('*', 'polygon', '')
    print "Getting Feature Classes..."
    arcpy.AddMessage ("Getting Feature Classes...")
    print "Feature Class list..."
    arcpy.AddMessage (fcList)

    for fc in fcList:
        fc = fc +"\\"
        #fieldList = [f.name for f in arcpy.ListFields(fc, "areaSize")]
        with arcpy.da.UpdateCursor (fc, ['SHAPE@AREA', 'areaSize']) as cursor:
            for row in cursor:
                row[1] = row[0]
                cursor.updateRow(row)
                print "Updating 'areaSize' field in: " + fc
                arcpy.AddMessage ("Updating 'areaSize' field in: " + fc)

Changing lines 30 and 32 is really all it took.  I still have some cleanup to do, then expand it to populate other fields, etc.  But this is a great start.

Thanks again!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Just for future reference, questions should be flagged as questions so they can be marked Correct if someone provides you an answer.  In this case with this question, you flagged it as a discussion.

0 Kudos
XanderBakker
Esri Esteemed Contributor

I would probably do something like this to avoid showing a message for each record in the featureclass and add the field in case it doesn't exist:

import arcpy
import os

gdb = arcpy.GetParameterAsText(0)
arcpy.env.workspace = gdb
fld_area = 'areaSize'

fdss = arcpy.ListDatasets('*', 'Feature')
fdss.append('')

for fds in fdss:
    arcpy.AddMessage('Feature Datasets: {0}'.format(fds))
    fcs = arcpy.ListFeatureClasses('*', 'Polygon', fds)

    for fc_name in fcs:
        fc = os.path.join(gdb, fds, fc_name)
        arcpy.AddMessage(' - Updating areaSize in Featureclass: {0}'.format(fc_name))

        # check if field exists, if not add it
        if len(arcpy.ListFields(fc, fld_area)) != 1:
            arcpy.AddField_management(fc, fld_area, 'DOUBLE')

        with arcpy.da.UpdateCursor (fc, ('SHAPE@AREA', fld_area)) as cursor:
            i = 0
            for row in cursor:
                i += 1
                if i % 100 == 0:
                    arcpy.AddMessage('  - Updating record: {0}'.format(i))
                row[1] = row[0]
                cursor.updateRow(row)

BTW, I didn't test the code