Select to view content in your preferred language

ArcPro ArcPy defined function not resetting symbology reference parameter in subsequent function calls

459
7
03-30-2025 02:39 PM
Labels (1)
JillMasters1
Occasional Contributor

The following code works beautifully the first time the function is called. The second time the function is called, the new second parameter value, Forest_TmbrVol_Source, is ignored while the new first parameter value, Project_TmbrVol_Path, is correctly used.

The result is that in both function calls, the input layer is symbolized by the same Forest_SlopePrc_Source symbology, which in the second call, results in Timber Volume being symbolized with Slope Percentage symbology rather than Timber Volume symbology as coded.

This error is not a result of duplicate names or incorrectly called vars. I coded messages to return the values of what was passed and processed and there are no errors there. The problem appears to be the persistence of the same initial variable used in the ApplySymbologyFromLayer tool for the reference symbology raster.

Reversing the order of the two calls, does not change this behavior. The mismatch in the 2nd call continues, but reversed., such that the input layer is incorrectly symbolized by Forest_TimbVol_Source rather than Forest_SlopePrc_Source symbology as coded.

JillMasters1_1-1743475771471.png

 

Image content:

#function
def apply_symbology(target_raster, symb_raster):
#apply symbology
target = current_map.addDataFromPath(target_raster)
symbLayer = current_map.addDataFromPath(symb_raster)
symb=symbLayer.listLayers()[2]   #returns Raster Image
outlayer = arcpy.management.ApplySymbologyFromLayer(target, symb)
current_map.addLayer(outlayer[0])
 
#var assignments
proj_folder=r'C:\Project\LayerFile\Clipped'
Forest_SlopePrc_Source = r'C:\Project\LayerFile\ImageryAndClassifiedImagery\LiDAR_1m_BareEarth_SlopePct_Color.lyr'
Forest_TmbrVol_Source = r'C:\Project\LayerFile\ImageryAndClassifiedImagery\LiDAR_30m_modeled_Volume_boardft_gt_9in_per_acre.lyr'
Project_SlopePrc_Path = proj_folder + '\\Project_SlopePrc'
Project_TmbrVol_Path = proj_folder + '\\Project_TmbrVol'
 
#function call
apply_symbology(Project_SlopePrc_Path, Forest_SlopePrc_Source)
apply_symbology(Project_TmbrVol_Path, Forest_TmbrVol_Source)

 

 

0 Kudos
7 Replies
DanPatterson
MVP Esteemed Contributor

Apply Symbology From Layer (Data Management)—ArcGIS Pro | Documentation

The "optional" parameter describes the situations where symbology won't be updated.  

Can you confirm that this doesn't apply?

 

Update_symbology
(Optional)
Specifies whether symbology ranges will be updated.

DEFAULT—Symbology ranges will be updated, except in the following situations:
When the input layer is empty
When the symbology layer uses class breaks (for example, graduated colors or graduated symbols) and the classification method is manual or defined interval
When the symbology layer uses unique values and the Show all other values option is checked
UPDATE—Symbology ranges will be updated.
MAINTAIN—Symbology ranges will not be updated; they will be maintained.

As for code formatting to facilitate reading and line number referencing, have a look at...

Code formatting ... the Community Version - Esri Community


... sort of retired...
0 Kudos
JillMasters1
Occasional Contributor

The code works without issue if I run the code for only one call.

The problem occurs when I run the code for multiple calls in a row.

def applysymbology(inlayer, outlayer):

     some code

applysymbology(xy,yy)

applysymbology(ss,tt)

The first call works without issue.

The second call applies symbology from yy instead of tt!

0 Kudos
DavidPike
MVP Notable Contributor

What's tempSymbRaster? it seems to be defined outside of your function.  I think that's the culprit.

0 Kudos
JillMasters1
Occasional Contributor

Miscopy of actual code on my part. I corrected the mis-identified var name in my post. Thanks

0 Kudos
DanPatterson
MVP Esteemed Contributor

something is persisting then between runs.  At this point all I can suggest is adding a Save Project between the two runs to see if it "clears".

If that doesn't work, then Tech Support is your best bet


... sort of retired...
0 Kudos
JillMasters1
Occasional Contributor

As soon as I can get the VDI to run (it's very temperamental today), I will try adding a Save Project. 

0 Kudos
JillMasters1
Occasional Contributor

Update:

If anyone knows of a better workaround. Please advise!!

I could not find a workaround, other than to separate my code into 2 files, and apply the symbology manually in-between. The first file contains the code up to apply symbology and the second file containing the code after I manually apply symbology.  Not happy. Hope ESRI will fix the tool soon.

 

Not the solution!
My workaround was to simply delete the parameter variables at the end of the code and then there was no issue with persistence.

def applysymbology(in, out):
     some code
     del in
     del out

applysymbology(xy,yy)
applysymbology(ss,tt)

0 Kudos