Populate attribute field within geodatabase using feature class name

4466
4
03-22-2012 09:05 AM
DanBignell
New Contributor II
Do you have anywhere I can start my search on finding a python script that autopopulates an attribute field based on feature class name for feature classes contained in the TOC of an MXD or even better, a geodatabase? I can???t seem to get a handle on this and have spent ages in the python forum. My start was something like this :-

import arcpy
mxd = arcpy.mapping.MapDocument(r"CURRENT")
fcs = arcpy.ListFeatureClasses(mxd)
fcs.reset()
fc = fcs.Next()
while fc:
arcpy.CalculateField_management (fc, "FILENAME", '"' + fc + '"')
fc = fcs.Next()
print "done
Tags (2)
0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Dan,

Here is an example on how to do this.  The below code will list all the feature classes in a geodatabase (including feature classes within feature datasets), insert a record into a dbf table, and update an existing field within the table called 'Names' with the feature class name.

import arcpy
from arcpy import env
env.workspace = r"C:\TEMP\Python\Test.gdb"

table = r"C:\TEMP\Python\FieldNames.dbf"

list = []

lstFCs = arcpy.ListFeatureClasses("*", "")
for fc in lstFCs:
    list.append(fc)

del fc, lstFCs
    
lstDatasets = arcpy.ListDatasets("*")
for dataset in lstDatasets:
    lstFCs = arcpy.ListFeatureClasses("*", "", dataset)
    for fc in lstFCs:
        list.append(fc)

for item in list:
    rows = arcpy.InsertCursor(table)
    row = rows.newRow()
    row.Fields = item
    rows.insertRow(row)
    print "Successfully inserted"

del row, rows
0 Kudos
DanBignell
New Contributor II
JSkinn,

Thanks very much for your reply. It was very informative. I have not managed to get it to work however, as I don't understand two points.

1) Do you have to specify the specific table within the GDB, or can you run it on all of the feature classes within the geodatabase?

2) I can't find where you specify the attribute field name within the code. YOu say mean to update an existing field called "Names" but I can't see where this is in the code.

Thakn you again for your help on this.

Dna.B
0 Kudos
JakeSkinner
Esri Esteemed Contributor
1) Do you have to specify the specific table within the GDB, or can you run it on all of the feature classes within the geodatabase?

The table I'm writing all of the feature class names to is specified here:

table = r"C:\TEMP\Python\FieldNames.dbf"


If the table exists within the geodatabase (not as a DBF), I can simply specify the name of that table.  Ex:

table = "FieldNames"



2) I can't find where you specify the attribute field name within the code. YOu say mean to update an existing field called "Names" but I can't see where this is in the code.

That was a type-o on my part.  The field name in the above example is called 'Fields', not 'Names'.  I'm calling this field using 'row.Fields = item'.

for item in list:
    rows = arcpy.InsertCursor(table)
    row = rows.newRow()
    row.Fields = item
    rows.insertRow(row)
    print "Successfully inserted"
0 Kudos
DanBignell
New Contributor II
I actually got what I wanted from a colleague, but your thread gave us a lot of inspiration, so thank you very much.

def FieldExist(featureclass, fieldname):
    fieldList = arcpy.ListFields(featureclass, fieldname)
    fieldCount = len(fieldList)
    if (fieldCount == 1):
        return True
    else:
        return False

myField = "MAP"

import arcpy
from arcpy import env
env.workspace = "C:\Users\Danb\Desktop\PG30M.mdb"
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
    if (FieldExist(fc, myField) == False):
        arcpy.AddField_management(fc, myField, "TEXT")
    arcpy.CalculateField_management(fc, myField, "'" + fc + "'", "PYTHON")


Thank you very much JSkinn!

Cheers