Select to view content in your preferred language

Vary symbology by rotation using ArcPy

1484
8
12-10-2022 07:13 PM
Status: Under Consideration
Labels (1)
Bud
by
Esteemed Contributor

Edited

When creating feature layers using ArcPy:

There doesn’t seem to be a way to Vary symbology by rotation using ArcPy.

Could that functionality be added?

Related: ArcPy script to convert layer to individual feature layers for each symbology class
https://community.esri.com/t5/arcgis-pro-sdk-questions/customize-pro-ui-checkboxes-in-toc-to-toggle/...

Tags (2)
8 Comments
DanPatterson

Symbol—ArcGIS Pro | Documentation

angle
(Read and Write)
Gets and sets the rotation value of a point symbol.

Double

JohannesLindner

The question is a bit unclear.

Yes, there is symbol.angle, but that sets the angle value of each symbol.

What @Bud means is that there is no way in arcpy to Vary symbology by rotation—ArcGIS Pro | Documentation

 

JeffBarrette
Status changed to: Under Consideration

This is something we can consider but it will be a long time before we can provide this level of detail via the curated API. In the meantime, the CIM may be an option.  Before reviewing the code below it may be worth reviewing the Python CIM Access help topic: 

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/python-cim-access.htm

I started with:

BEFORE_UI.PNG

As suggested in the topic above, I created a before and after layer, saved them to LYRX files and compared the JSON differences.

JSON_Difference.PNG

Here is the code based on what I see above.  The before layer didn't have visual variable applied so I had to create the appropriate CIM objects, set the values to match above and then apply the newly created visual variable to the empty visual variable list associated with the layer.

p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Map')[0]
l = m.listLayers('*BEFORE')[0]
maxLabel = 360 #this could be automated

#Create a rotation visual variable via the CIM
l_cim = l.getDefinition('V3')
visVar = arcpy.cim.CreateCIMObjectFromClassName('CIMRotationVisualVariable', 'V3')
#Xinfo
visVar.visualVariableInfoX.randomMax = maxLabel
visVar.visualVariableInfoX.visualVariableInfoType = "None"
#Yinfo
visVar.visualVariableInfoY.randomMax = maxLabel
visVar.visualVariableInfoY.visualVariableInfoType = "None"
#Zinfo
visVar.visualVariableInfoZ.randomMax = maxLabel
visVar.visualVariableInfoZ.visualVariableInfoType = "Expression"
#Create expression for Xinfo
expInf = arcpy.cim.CreateCIMObjectFromClassName('CIMExpressionInfo', 'V3')
expInf.title = "Custom"
expInf.expression = "$feature.Rotation"  #field called rotation
expInf.returnType = "Default"

visVar.visualVariableInfoZ.valueExpressionInfo = expInf
visVar.rotationTypeZ = "Arithmetic"

l_cim.renderer.visualVariables.append(visVar) #Apply visual variable
l.setDefinition(l_cim) #set back to layer

 

And the final result:

AFTER_UI.PNG

I hope this helps,

Jeff - Layout and arcpy.mp teams

JohannesLindner

Thanks Jeff, that helps alot.

I have done some work with the CIM (especially configuring popups), but I haven't looked too far into it regarding symbology.

 

AndreasB

@JeffBarrette  Hi, I looked at your code and tried  to implement it, but it didn't change my lyrx file as far as I can see. 

Is there a difference between 'V3' and 'V2' or shouldn't that make any difference? 

Thanks in advance

JeffBarrette

@AndreasB V2 is intended for Pro 2x but will continue to work in 3x.  If they both don't work they way you are expecting then nothing was changed in the 2x CIM vs 3x.  I would need to see more of your code to understand what is not working.

Jeff - Layout and arcpy.mp teams

fireapple

@JeffBarrette Thanks to your solution, Through cim, lyrx applied rotation and size was extracted from arcgis pro arcpy. However, I would also like to apply this to arcmap (arcgis desktop), but, on the arcmap arcpy, the getDefinition function does not seem to be available in Layer objects. I wonder if there is a possible way in arcmap arcpy.

JeffBarrette

@fireapple Python CIM access is not available to arcpy.mapping (for ArcMap).  ArcGIS Pro projects and document settings use the CIM specification for persistance where MXDs do not. 

Jeff - arcpy.mp and Layout teams