arcpy in ArcGIS Pro 2.9.1 - Setting a CIM definition on a query layer causes the layer to become disconnected

1442
4
Jump to solution
02-09-2022 10:27 AM
HarshaPerera
New Contributor III

I am trying to use the CIM definition on a query layer to set it to use its own metadata. This causes the layer to get disconnected. The code is along the following lines

arcpy.management.MakeQueryLayer(..paras..)
arcpy.management.SaveToLayerFile(layername, layerfile, "ABSOLUTE")

layer = map.addLayer(arcpy.mp.LayerFile(layerfile), "BOTTOM")[0]

l_cim = layer.getDefinition('V2')
l_cim.useSourceMetadata = False
# This causes the layer to become disconnected, regardless of whether any properties were set on the

#CIM definition
layer.setDefinition(l_cim)

Can anyone offer any suggestions?

Thanks,

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
MarcoBoeringa
MVP Regular Contributor

I already detected this same issue of disconnection for Query Layers in ArcGIS Pro 2.4... seems nothing changed...

For the particular case I had, I worked around the issue by dumping the layer to a Pro layer file (*.lyrx) by saving it out, and editing the JSON directly using the Python 'json' module and "json.load" / "json.dump" methods.

Note that this is wholly outside the realm of safe edits that ESRI probably supports, but if you are careful, and understand the CIM and know how to mentally translate it to the respective JSON structure, you can in fact edit the json directly by modifying the hierarchical dictionary structure that "json.load" will give you access to, and then dumping it back to the layer file, and subsequently re-load the layer file in ArcGIS.

Note that the *.lyrx layer files are in fact stored in text format, not binary, so you can luckily inspect them and the CIM structure in a text editor, which helps getting it right.

I know..., it makes you grind your teeth... knowing you should be able to do this no problem using "layer.getDefinition/setDefinition"...

View solution in original post

4 Replies
DanPatterson
MVP Esteemed Contributor

Python CIM access—ArcGIS Pro | Documentation

Did you read the documentation and the first caution, before you proceed with what can be done?


... sort of retired...
0 Kudos
MarcoBoeringa
MVP Regular Contributor

I already detected this same issue of disconnection for Query Layers in ArcGIS Pro 2.4... seems nothing changed...

For the particular case I had, I worked around the issue by dumping the layer to a Pro layer file (*.lyrx) by saving it out, and editing the JSON directly using the Python 'json' module and "json.load" / "json.dump" methods.

Note that this is wholly outside the realm of safe edits that ESRI probably supports, but if you are careful, and understand the CIM and know how to mentally translate it to the respective JSON structure, you can in fact edit the json directly by modifying the hierarchical dictionary structure that "json.load" will give you access to, and then dumping it back to the layer file, and subsequently re-load the layer file in ArcGIS.

Note that the *.lyrx layer files are in fact stored in text format, not binary, so you can luckily inspect them and the CIM structure in a text editor, which helps getting it right.

I know..., it makes you grind your teeth... knowing you should be able to do this no problem using "layer.getDefinition/setDefinition"...

HarshaPerera
New Contributor III

Thanks Marco. My aim is to set the description on a layer. This was previously available as a property on the layer. I will have a look at the json load approach as I am already creating a layer file.

0 Kudos
HarshaPerera
New Contributor III

Thanks I worked around this issue using json load and dump.