Export Symbology as Python

893
4
07-20-2023 09:41 AM
Status: Under Consideration
Labels (1)
EricEagle
Occasional Contributor III

I'd like the ability to export the symbology of a layer in the ArcGIS Pro canvas as a python snippet.

 

When I'm writing a script tool that performs some loading and symbology of layers, I sometimes have to run the script over and over again to ensure that the symbology is what I want.

It would be really cool if I were able to export symbology I'm happy with as a Python snippet that I could then just drop in a script tool with a little refactoring.

Specifically, I see this working by using the hamburger (overflow) menu in the Symbology tab, click the menu, and then maybe an "export as python" (which would save it to a file) or "Copy python snippet" (builds/stashes the snippet in the clipboard for pasting into a script -- this is my preferred method).

This would save a huge amount of time when it comes to the more tedious aspects of script tool construction (and symbology definitely falls in that category!) allowing analysts/devs to focus more on the core geoprocessing and worrying less about coming up with symbology code from scratch.

4 Comments
JakeJacobsAVA

I like this idea. Symbology is really hard to maintain and more tools are always welcome.

I'll also mention that one workaround I've used is to export a lyrx file and then edit the json directly and then re-apply it. For example, I had a unique value renderer and due to a datafix some of the values changed but I wanted to apply the same symbology from the older value to the newer value. So I just manually edited the values in the file, leaving the rest of the symbology the same.

HannesZiegler
Status changed to: Needs Clarification

Hi @EricEagle, this can be done using the CIM specification, specifically the renderer for vector data. When applying symbology to the result in a script tool, you can use the arcpy.SetParameterSymbology method. The Output symbology in scripts—ArcGIS Pro documentation explains how to view the CIM definition of a layer and how to find the renderer definition within it. You can also use cim access properties to programmatically modify the CIM definition. 

I believe this provides what you are looking for?

EricEagle

@HannesZiegler Thanks for the response!

 

In this case I am talking about a specific workflow enhancement.  Yes you can do everything under arcpy - but I am looking more for the following workflow:

  1. User adds content (raster or vector) to the map canvas.
  2. User styles the content in the way that they want using the GUI symbology options, getting everything exactly right.
  3. User exports that symbology state as a python snippet for use in a script.  Obviously certain variables will need to be changed as any python snippet generated would point to a specific data source rather than a variable.

The workflow here differs from what you suggest because instead of anticipating the symbology in the IDE and then tweaking it as iterations are run, the user would start from a finished/approved symbology, lightly editing it into a script.  Additionally, the CIM workflow requires a .lyrx file - what I am suggesting would be done on the fly, and may not need to be expressed in CIM terms (i.e., the current symbology should be returned as python in its simplest form for use in a script).

JeffBarrette
Status changed to: Under Consideration

We have near term plans to help with applying existing symbology to other existing objects.  We will take your scenario into consideration when designing a workflow and may post some follow-up questions as we get closer to development.

Jeff - Layout and arcpy.mp teams