Select to view content in your preferred language

Creating/Editing Feature Templates via arcpy?

1341
7
08-03-2023 01:22 PM
GB_MattPanunto_GISS
Frequent Contributor

Part of my workflow is to create feature templates, which can be a tedious process. Is it possible to create/edit templates via arcpy? Having a hard time finding any resources on how to do this.

 

GB_MattPanunto_GISS_0-1691093980101.png

 

7 Replies
AntEsk
by
Emerging Contributor

I am trying to do this as well, also cannot find any documentation.

i have found that the lyrx file is a json file and there is a featureTemplates section which seems to define default values

so we should be able to modify it,

i just had a go but it didn't work, (i couldn't complete the sketch when editing so i did something wrong) 

its Friday arvo, sounds like a job for next week 😁

DavidSolari
Frequent Contributor

Editing the "x" files directly is possible but very fragile, there's nothing stopping ESRI from redefining how these files are laid out and breaking your code. The preferred method is to go through the CIM Interfaces as they have explicit versions that are guaranteed to remain stable across Pro versions. Note that CIM editing is very different from your usual arcpy workflows, you have to work with the various types as they're defined and instantiate new instances using the provided factory functions.

AntEsk
by
Emerging Contributor

Hi, 

yes it certainly is a dirty method, i will look into the CIM.

for now, i made a lyrx file with a feature template / symbology using the two fields i wanted, populating one with a temp string, then i just do i bulk replace of the temp string in the lyrx using json 

0 Kudos
BenCapellGGT
New Contributor

Hi all,

I'm looking into generating Feature Templates with a specific field value for users/editors to access when generating features in feature classes of a geodatabase. I need to be able to set various fields to a specific configuration of  values that will change periodically, so I want to script the update in Python.

@AntEsk

Is there documentation that can help me reproduce your .lyrx update via JSON method where you alter the featureTemplates section in the JSON?

@DavidSolari ,

For the CIM Interfaces method of achieving what I described above the most related documentation I've found are:

CIM FeatureLayerDefinition-3  then EditingTemplate  then it is not clear what I need to work with to edit a field value for a feature template. I'm thinking it may be the CIMBasicRowTemplate but their isn't much detail describing what impact changes to the properties of this will have or what JSON format to use to alter the defaultValues property.  

defaultValues{JSON_object}The default values.

 

Any help is much appreciated!!

0 Kudos
AntEsk
by
Emerging Contributor

to do a global replace on the lyrx file, replacing ##TEMPLATE## with the required values after manually creating a template lyrx file whihc alredy has a feature template using two fields, i am only updating my second field which has a temp value of ##TEMPLATE##

with open(lyrx_file_pth, 'r+') as f:
data = json.load(f)
# replace placeholder with op code
d = json.loads(json.dumps(data).replace('##TEMPLATE##', f'{op_code}'))

# back to top of file
f.seek(0)
# write new file
f.write(json.dumps(d, indent=4))
f.truncate()

i have since gone down the lyr_cim method, getting the featureTemplates as an attribute of the lyr_cim object, the featureTemplate

lyr_cim = lyr.getDefinition('V3')
for ft in lyr_cim.featureTemplates:
# do stuff
0 Kudos
wchowster
New Contributor

@GB_MattPanunto_GISS @AntEsk @AntEsk 

Wondering if any if you guys have any updates since the last post.

I am trying to do pretty much the exact same thing, create a feature template for points with pre defined set attributes for each point that I have already loaded into the appropriate domain/field group/contingent values. Since there is a lot of points and fields it is a tedious process but doesn't seem like as straight forward as I would have hoped. 

0 Kudos
AntDevEsk
Occasional Contributor

@wchowster 

i had a project code field (project_code_fld) and a project code (project_code)  i wanted to populate it with

from memory i had to update lyrx file in three places, the display field, the feature template and the renderer field.  below is cobbled together from parts of a larger function, hope if helps  

 

    for lyr_file_name in layer_lst:
        lyr_pth = os.path.join(temp_lyr_fldr, lyr_file_name)
        lyr_file = arcpy.mp.LayerFile(lyr_pth)
        for lyr in lyr_file.listLayers():
            # Return the layer's CIM definition
            lyr_cim = lyr.getDefinition('V3')

            # update the display field
            try:
                lyr_cim.featureTable.displayField = lyr_cim.renderer.fields[0]
            except:
                lyr_cim.featureTable.displayField = project_code_fld

            # # update the feature template
            for ft in lyr_cim.featureTemplates:
                # assign default value
                ft_default_vals = ft.defaultValues["propertySetItems"]
                ft_default_vals.extend([project_code_fld, project_code])

            # # update the feature renderer field values
            for grp in lyr_cim.renderer.groups:

                for grp_class in grp.classes:
                    # update the second field value to be the project code
                    try:
                        grp_class.values[0].fieldValues.append(project_code)
                    except:
                        msgs.addMessage(f'\t!! cannot update grp_class values for - {lyr.name}')

                # Push the changes back to the layer object
            lyr.setDefinition(lyr_cim)
        lyr_file.save()