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:
Thank you!
Solved! Go to Solution.
After working through it, I realized that I could accomplish this task without a calculate field code block. Here's what I came up with!
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
gdb = arcpy.GetParameterAsText(0)
fcsLi = listFcsInGDB(gdb)
for fc in fcsLi:
inField = 'NRHP_Type'
fileName = os.path.basename(fc)
if fileName in ['crothr_ln','crothr_pt','crothr_py']:
fieldInput = 'Other'
elif fileName in ['crbldg_pt','crbldg_py']:
fieldInput = 'Building'
elif fileName in ['crobj_ln','crobj_pt','crobj_py']:
fieldInput = 'Object'
elif fileName in ['crsite_ln','crsite_pt','crsite_py']:
fieldInput = 'Site'
elif fileName in ['crstru_ln','crstru_pt','crstru_py']:
fieldInput = 'Structure'
elif fileName in ['crsurv_ln','crsurv_py','crsurv_pt']:
fieldInput = 'Survey'
elif fileName in ['crdist_py']:
fieldInput = 'District'
else:
fieldInput = 'undefined NRHP type'
arcpy.CalculateField_management(fc, inField, "'"+fieldInput+"'", "PYTHON3")
print(fc+" completed")
Might not be the prettiest but it got the job done. Thank you @DavidPike and @RhettZufelt !
have not looked that the rest of your code or code logic is correct, but at a glance possibly:
for fc in fcsLi:
inField = "NRHP_Type"
expression = "getNRHPType(os.path.basename(fc))"
arcpy.CalculateField_management(fc, inField, "'"+expression+"'", "PYTHON", codeblock)
print(fc+" completed")
Hi @DavidPike ,
The code ran without error, but it didn't accomplish what I was hoping it would do. It now writes: "getNRHPType(os.path.basename(fc))" to the field.
the message says the function getNRHPType is not defined. Probably because it is seeing it inside the """ of the code block and thinks it is comment.
Try removing the codeblock = """ and the matching """, should at least make that error go away.
R_
After working through it, I realized that I could accomplish this task without a calculate field code block. Here's what I came up with!
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
gdb = arcpy.GetParameterAsText(0)
fcsLi = listFcsInGDB(gdb)
for fc in fcsLi:
inField = 'NRHP_Type'
fileName = os.path.basename(fc)
if fileName in ['crothr_ln','crothr_pt','crothr_py']:
fieldInput = 'Other'
elif fileName in ['crbldg_pt','crbldg_py']:
fieldInput = 'Building'
elif fileName in ['crobj_ln','crobj_pt','crobj_py']:
fieldInput = 'Object'
elif fileName in ['crsite_ln','crsite_pt','crsite_py']:
fieldInput = 'Site'
elif fileName in ['crstru_ln','crstru_pt','crstru_py']:
fieldInput = 'Structure'
elif fileName in ['crsurv_ln','crsurv_py','crsurv_pt']:
fieldInput = 'Survey'
elif fileName in ['crdist_py']:
fieldInput = 'District'
else:
fieldInput = 'undefined NRHP type'
arcpy.CalculateField_management(fc, inField, "'"+fieldInput+"'", "PYTHON3")
print(fc+" completed")
Might not be the prettiest but it got the job done. Thank you @DavidPike and @RhettZufelt !