<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculate Field from Feature Class Name in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/calculate-field-from-feature-class-name/m-p/1371317#M69684</link>
    <description>&lt;P&gt;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!&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Might not be the prettiest but it got the job done. Thank you&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/15530"&gt;@RhettZufelt&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;</description>
    <pubDate>Wed, 17 Jan 2024 21:54:44 GMT</pubDate>
    <dc:creator>Kklitt</dc:creator>
    <dc:date>2024-01-17T21:54:44Z</dc:date>
    <item>
      <title>Calculate Field from Feature Class Name</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-from-feature-class-name/m-p/1371110#M69679</link>
      <description>&lt;P&gt;Hello, I'm a python newbie seeking help.&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;For example, all points in the feature class crothr_pt would have 'Other' written in the NRHP_Type field.&lt;/P&gt;&lt;P&gt;This is what I have so far:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The error message:&lt;BR /&gt;&lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;File "C:\junk.tbx#CreateNRHPField1_NewToolbox.py", line 41, in &amp;lt;module&amp;gt;&lt;BR /&gt;NameError: name 'getNRHPType' is not defined&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are the guides I've been using:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://gis.stackexchange.com/questions/17741/adding-file-name-to-attribute-field-using-calculate-field" target="_blank"&gt;arcgis desktop - Adding file name to attribute field using Calculate Field? - Geographic Information Systems Stack Exchange&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://gis.stackexchange.com/questions/5893/listing-all-feature-classes-in-file-geodatabase-including-within-feature-datase" target="_blank"&gt;arcpy - Listing all feature classes in File Geodatabase, including within feature datasets? - Geographic Information Systems Stack Exchange&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2024 17:46:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-from-feature-class-name/m-p/1371110#M69679</guid>
      <dc:creator>Kklitt</dc:creator>
      <dc:date>2024-01-17T17:46:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field from Feature Class Name</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-from-feature-class-name/m-p/1371256#M69681</link>
      <description>&lt;P&gt;have not looked that the rest of your code or code logic is correct, but at a glance possibly:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for fc in fcsLi:
    inField = "NRHP_Type"
    expression = "getNRHPType(os.path.basename(fc))"
    arcpy.CalculateField_management(fc, inField, "'"+expression+"'", "PYTHON", codeblock)
    print(fc+" completed")&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 17 Jan 2024 20:51:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-from-feature-class-name/m-p/1371256#M69681</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2024-01-17T20:51:10Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field from Feature Class Name</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-from-feature-class-name/m-p/1371291#M69682</link>
      <description>&lt;P&gt;the message says the function getNRHPType is not defined.&amp;nbsp; Probably because it is seeing it inside the """ of the code block and thinks it is comment.&lt;/P&gt;&lt;P&gt;Try removing the codeblock = """ and the matching """, should at least make that error go away.&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2024 21:31:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-from-feature-class-name/m-p/1371291#M69682</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2024-01-17T21:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field from Feature Class Name</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-from-feature-class-name/m-p/1371305#M69683</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Kklitt_0-1705527641686.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/91902i2D2279E692C42F32/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Kklitt_0-1705527641686.png" alt="Kklitt_0-1705527641686.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2024 21:41:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-from-feature-class-name/m-p/1371305#M69683</guid>
      <dc:creator>Kklitt</dc:creator>
      <dc:date>2024-01-17T21:41:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field from Feature Class Name</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-from-feature-class-name/m-p/1371317#M69684</link>
      <description>&lt;P&gt;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!&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Might not be the prettiest but it got the job done. Thank you&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/15530"&gt;@RhettZufelt&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2024 21:54:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-from-feature-class-name/m-p/1371317#M69684</guid>
      <dc:creator>Kklitt</dc:creator>
      <dc:date>2024-01-17T21:54:44Z</dc:date>
    </item>
  </channel>
</rss>

