Hello, I'm a python newbie seeking help.
I'm attempting to create a python script that iterates through a file geodatabase with feature classes and feature classes within feature datasets to calculate the field 'NRHP_Type' with the name of the feature class.
For example, all points in the feature class crothr_pt would have 'Other' written in the NRHP_Type field.
This is what I have so far:
import arcpy
import arcgis
import os
import sys
def listFcsInGDB(gdb):
#list all Feature Classes in a geodatabase, including inside Feature Datasets '''
arcpy.env.workspace = gdb
print('Processing ', arcpy.env.workspace)
fcs = []
for fds in arcpy.ListDatasets('','feature') + ['']:
for fc in arcpy.ListFeatureClasses('','',fds):
#yield os.path.join(fds, fc)
fcs.append(os.path.join(fds, fc))
return fcs
codeblock = """
def getNRHPType(nrhpfc):
if nrhpfc in ['crothr_ln','crothr_pt','crothr_py']:
return 'Other'
elif nrhpfc in ['crbldg_pt','crbldg_py']:
return 'Building'
elif nrhpfc in ['crobj_ln','crobj_pt','crobj_py']:
return 'Object'
elif nrhpfc in ['crsite_ln','crsite_pt','crsite_py']:
return 'Site'
elif nrhpfc in ['crstru_ln','crstru_pt','crstru_py']:
return 'Structure'
elif nrhpfc in ['crsurv_ln','crsurv_py','crsurv_pt']:
return 'Survey'
elif nrhpfc in ['crdist_py']:
return 'District'
else:
return 'undefined NRHP type'"""
gdb = arcpy.GetParameterAsText(0)
fcsLi = listFcsInGDB(gdb)
for fc in fcsLi:
inField = "NRHP_Type"
expression = getNRHPType(os.path.basename(fc))
arcpy.CalculateField_management(fc, inField, ""+expression+"", "PYTHON", codeblock)
print(fc+" completed")
The error message:
Traceback (most recent call last):
File "C:\junk.tbx#CreateNRHPField1_NewToolbox.py", line 41, in <module>
NameError: name 'getNRHPType' is not defined
Here are the guides I've been using:
arcgis desktop - Adding file name to attribute field using Calculate Field? - Geographic Information Systems Stack Exchange
arcpy - Listing all feature classes in File Geodatabase, including within feature datasets? - Geographic Information Systems Stack Exchange
Thank you!