Select to view content in your preferred language

Creating/Editing Feature Templates via arcpy?

923
5
08-03-2023 01:22 PM
GB_MattPanunto_GISS
Occasional Contributor II

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

 

5 Replies
AntEsk
by
New Contributor II

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
Occasional Contributor III

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
New Contributor II

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
New Contributor II

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