cim_lyr.featureTable.fieldDescriptions returns an empty list

6052
14
Jump to solution
04-12-2021 02:29 PM
MarvisKisakye1
Regular Contributor

Why would cim_lyr.featureTable.fieldDescriptions return an empty list when the feature layer clearly contains fields? @JeffBarrette @JeffMoulds 

Tags (2)
14 Replies
AaronDutch
New Contributor

It seems that altering the Shape field alias no longer generates the CIM which is frustrating. Following this workflow all of my CIM objects are still empty when using print(cim_obj.featureTable.fieldDescriptions) even though in the APRX the alias has been changed. 

0 Kudos
julian_svcs
Frequent Contributor

@AaronDutch That is strange. I just tested this using ArcGIS Pro 3.0 and it works as expected. I read the fieldDescriptions before changing the alias and it is empty (as expected). When I use AlterField to change the SHAPE field alias and then call the fieldDescriptions again, I get the field details.

Here is my sample code that worked for me:

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps(aprx.activeMap.name)[0]
for lyr in m.listLayers():
    if lyr.isFeatureLayer:
        print(f"{lyr.longName}")
        arcpy.AlterField_management(lyr, "SHAPE", new_field_alias="Shape")
        cim_lyr = lyr.getDefinition('V2')
        print(cim_lyr.featureTable.fieldDescriptions)
        for fd in cim_lyr.featureTable.fieldDescriptions:
            print(f"Field Name: {fd.fieldName}")
Pascal
by
New Contributor

Hey all,

thanks for the thread which already helped me a lot when I encountered the empty list.

As I aim to build a Python tool that can change the fieldDescription of a selected layer, I needed to modify the layer with arcpy.AlterField_management() from inside the tool.

Unfortunately, the CIM fieldDescription list is still empty after running the tool. In contrast, If I do the AlterField operation from the Python Window in ArcGIS Pro, the fieldDescription list is getting populated.

Does anyone have an idea how that could be and how the execution of a function from either Python Window or Python Toolbox differs?

Using ArcGIS Pro 3.0.3

Thanks!

PhilLarkin1
Frequent Contributor

I experience the same issue when running Pro 3.2.0. When running AlterField in a geoprocessing script tool, fieldDescription is not being populated. When running in the Python Window it is.  @JeffBarrette

JeffBarrette
Esri Regular Contributor

I know this is a really old post but I was going through some old emails, etc and came across this thread.  I've helped people address this issue (differently than AlterField) and want to paste that code here as an option for people to try.

Basically, if there are NO field descriptions (e.g., a layer was just added), then another option is to use MakeFeatureLayer.  The MakeFeatureLayer result has the CIM properties.  You make the changes and than copy those changes back to the original layer.  There are pre3.4 and 3.4 and beyond options.

def updateCIMFields(l, cimLyr):
    fList = ["SQKM", "POP2001", "Shape_Length", "Shape_Area"]

    for fd in cimLyr.featureTable.fieldDescriptions:
        if fd.fieldName in fList:
            fd.numberFormat.roundingOption = "esriRoundNumberOfDecimals"
            fd.numberFormat.roundingValue = 0
            fd.numberFormat.zeroPad = True
            fd.numberFormat.useSeparator = True
    l.setDefinition(cimLyr)
    return cimLyr
    
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps()[0]

lyr = m.listLayers('Provinces')[0]
lyr_cim = lyr.getDefinition('V3')

if len(lyr_cim.featureTable.fieldDescriptions) == 0:  #NO CIM field info present
    print('No CIM Field Info')

    mkLyr = arcpy.management.MakeFeatureLayer(lyr)[0]
    mkLyr_cim = mkLyr.getDefinition('V3')
    mkLyr_cim = updateCIMFields(mkLyr, mkLyr_cim)

    #Copy CIM information and remove temporary layer
    #lyr_cim.featureTable.fieldDescriptions = mkLyr_cim.featureTable.fieldDescriptions  ###for 3.3 and prior
    #lyr.setDefinition(lyr_cim)                                                         ###for 3.3 and prior
    lyr.pasteProperties(mkLyr, 'FIELD_PROPERTIES')                                      ###for 3.4 and prior
    m.removeLayer(mkLyr)

else:                                                 #CIM field info present
    print('CIM Field Info Pre-Exists')
    lyr_cim = updateCIMFields(lyr, lyr_cim)
    lyr.setDefinition(lyr_cim) 

 

Jeff - arcpy.mp Team

0 Kudos