<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: ApplySymbologyFromLayer only works after 2nd usage in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/applysymbologyfromlayer-only-works-after-2nd-usage/m-p/1269016#M67147</link>
    <description>&lt;P&gt;The updated code with the save project lines.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import numpy as np

outRasterName = arcpy.GetParameterAsText(0)   # raster output to FGDB
histlyrx = arcpy.GetParameter(1)    # layer file input for raster symbology
histlayout = arcpy.GetParameter(2)   # layout file input

# create NumPy array to be dummy output raster for demo 
hist2D_arr = np.zeros((256,256), dtype='uint32')

# NumPyArrayToRaster(); write out to file geodatabase
outRaster = arcpy.NumPyArrayToRaster(hist2D_arr)
outRaster.save(outRasterName)

# import layout file if needed; also creates map 'Histogram' which is in layout 
currProject = arcpy.mp.ArcGISProject("CURRENT")
layoutList = currProject.listLayouts("HistogramLayout")
if not(layoutList):
    currProject.importDocument(histlayout)
    currProject.save()

# confirm if map named 'Histogram' is present in the current Project. If not,
# issue error message.
histMapList = currProject.listMaps("Histogram")
if histMapList:
    arcpy.AddMessage("Histogram map is present.\n")
    histMap = histMapList[0]
    histlyr = histMap.addDataFromPath(outRasterName)
    currProject.save()
    histlyr = arcpy.management.ApplySymbologyFromLayer(histlyr, histlyrx)
    arcpy.AddMessage(histlyr)
else:
    arcpy.AddError("No map named Histogram found.") 

# Use SetParameter to associate the derived layer required by
# ApplySymbologyFromlayer to the corresponding item in the parameter list
arcpy.SetParameter(3,histlyr)

currProject.save()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 17 Mar 2023 15:59:52 GMT</pubDate>
    <dc:creator>PeterDowty</dc:creator>
    <dc:date>2023-03-17T15:59:52Z</dc:date>
    <item>
      <title>ApplySymbologyFromLayer only works after 2nd usage</title>
      <link>https://community.esri.com/t5/python-questions/applysymbologyfromlayer-only-works-after-2nd-usage/m-p/1268828#M67136</link>
      <description>&lt;P&gt;Hi. Thanks for any help. Having issue with ApplySymbologyFromLayer in my script tool.&lt;/P&gt;&lt;P&gt;Goal of tool is to take two bands from an image and produce a 2D histogram as a new raster layer with just a row/col coordinate system. The histogram is a 256x256 raster with row/col positions indicating the 8bit brightness values in the two bands. Pixel values represent counts of pixels in the original image with the given brightness value pair.&lt;/P&gt;&lt;P&gt;I want to provide a layer file to symbolize the histogram raster and a layout file that adds a layout with grid and legend. The first time the tool is used in a Pro project, the layout file is imported that contains a map named 'Histogram' which is also created upon import of the layout.&lt;/P&gt;&lt;P&gt;I'm finding that on first usage in a new project, the symbology from the layer is not applied to the histogram raster. &amp;nbsp;Any subsequent usage of the tool in that same project does successfully apply the symbology.&lt;/P&gt;&lt;P&gt;Simplified code that just creates a dummy 256x256 output raster, but still replicates the problem:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import numpy as np

outRasterName = arcpy.GetParameterAsText(0)   # raster output to FGDB
histlyrx = arcpy.GetParameter(1)    # layer file input for raster symbology
histlayout = arcpy.GetParameter(2)   # layout file input

# create NumPy array to be dummy output raster for demo 
hist2D_arr = np.zeros((256,256), dtype='uint32')

# NumPyArrayToRaster(); write out to file geodatabase
outRaster = arcpy.NumPyArrayToRaster(hist2D_arr)
outRaster.save(outRasterName)

# import layout file if needed; also creates map 'Histogram' which is in layout 
currProject = arcpy.mp.ArcGISProject("CURRENT")
layoutList = currProject.listLayouts("HistogramLayout")
if not(layoutList):
    currProject.importDocument(histlayout)

# confirm if map named 'Histogram' is present in the current Project. If not,
# issue error message.
histMapList = currProject.listMaps("Histogram")
if histMapList:
    arcpy.AddMessage("Histogram map is present.\n")
    histMap = histMapList[0]
    histlyr = histMap.addDataFromPath(outRasterName)
    histlyr = arcpy.management.ApplySymbologyFromLayer(histlyr, histlyrx)
    arcpy.AddMessage(histlyr)
else:
    arcpy.AddError("No map named Histogram found.") 

# Use SetParameter to associate the derived layer required by
# ApplySymbologyFromlayer to the corresponding item in the parameter list
arcpy.SetParameter(3,histlyr)&lt;/LI-CODE&gt;&lt;P&gt;The tool parameters are set up as:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PeterDowty_0-1679011403817.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/65528i1B5173C7A41071AF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PeterDowty_0-1679011403817.png" alt="PeterDowty_0-1679011403817.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;A clue - right after the line that invokes ApplySymbologyFromLayer, the code writes out the layer name. &amp;nbsp;On first usage of the tool, this writes something like 'GPLYR_{EC07329E-BF4A-872F-A9F0DC9DB737}'. &amp;nbsp;On subsequent uses, this actually writes out the name that I specified in the UI, e.g. "demo1".&lt;/P&gt;&lt;P&gt;Thank you for any ideas.&lt;/P&gt;&lt;P&gt;Pete&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2023 00:11:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/applysymbologyfromlayer-only-works-after-2nd-usage/m-p/1268828#M67136</guid>
      <dc:creator>PeterDowty</dc:creator>
      <dc:date>2023-03-17T00:11:13Z</dc:date>
    </item>
    <item>
      <title>Re: ApplySymbologyFromLayer only works after 2nd usage</title>
      <link>https://community.esri.com/t5/python-questions/applysymbologyfromlayer-only-works-after-2nd-usage/m-p/1268850#M67138</link>
      <description>&lt;P&gt;I am not seeing an project&amp;nbsp;&lt;SPAN&gt;save () at the end&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Saves changes to an&amp;nbsp;ArcGISProject&amp;nbsp;(.aprx). The project is saved and the project variable continues to reference the original&amp;nbsp;ArcGISProjectobject.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;The last sentence suggests that nothing will persist until the project is referenced/opened again after the script runs.&lt;/P&gt;&lt;P&gt;you might need the file path from current&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/arcgisproject-class.htm" target="_blank" rel="noopener"&gt;ArcGISProject—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2023 03:00:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/applysymbologyfromlayer-only-works-after-2nd-usage/m-p/1268850#M67138</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2023-03-17T03:00:29Z</dc:date>
    </item>
    <item>
      <title>Re: ApplySymbologyFromLayer only works after 2nd usage</title>
      <link>https://community.esri.com/t5/python-questions/applysymbologyfromlayer-only-works-after-2nd-usage/m-p/1268984#M67145</link>
      <description>&lt;P&gt;Thank you for that idea&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/215600"&gt;@DanPatterson&lt;/a&gt;.&lt;/P&gt;&lt;P&gt;I just added the following line to the end of that code, but I found the same behavior:&lt;/P&gt;&lt;P&gt;currProject.save()&lt;/P&gt;&lt;P&gt;Another clue - I've also noticed on first usage of the tool in a project, that the histogram raster is successfully added to the map Histogram, it just doesn't get symbolized. &amp;nbsp;But I don't get any warning about the lack of a coordinate system on the histogram raster.&lt;/P&gt;&lt;P&gt;On subsequent uses of the tool in the project, I get this popup warning:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PeterDowty_0-1679066004603.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/65571i6479C01C6BB28950/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PeterDowty_0-1679066004603.png" alt="PeterDowty_0-1679066004603.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2023 15:14:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/applysymbologyfromlayer-only-works-after-2nd-usage/m-p/1268984#M67145</guid>
      <dc:creator>PeterDowty</dc:creator>
      <dc:date>2023-03-17T15:14:11Z</dc:date>
    </item>
    <item>
      <title>Re: ApplySymbologyFromLayer only works after 2nd usage</title>
      <link>https://community.esri.com/t5/python-questions/applysymbologyfromlayer-only-works-after-2nd-usage/m-p/1269015#M67146</link>
      <description>&lt;P&gt;Your suggestion&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/215600"&gt;@DanPatterson&lt;/a&gt;&amp;nbsp;made me think maybe the project needs to be saved earlier in the code, prior to the use of ApplySymbologyFromLayer. &amp;nbsp;I added a couple earlier saves just to check this.&lt;/P&gt;&lt;P&gt;But the problem persisted - symbology is not applied to the raster on the first usage of the tool, but it is on subsequent uses.&lt;/P&gt;&lt;P&gt;Then I tried the following. &amp;nbsp;Prior to the first usage of the tool in a project, I imported the layout manually using the Import Layout tool on the ribbon. &amp;nbsp;Then I used the tool so that the import layout code was not executed.&lt;/P&gt;&lt;P&gt;Then the symbology was correctly applied on the first usage of the tool. &amp;nbsp;So the problem is a consequence of the use of .importDocument in the code.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2023 15:58:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/applysymbologyfromlayer-only-works-after-2nd-usage/m-p/1269015#M67146</guid>
      <dc:creator>PeterDowty</dc:creator>
      <dc:date>2023-03-17T15:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: ApplySymbologyFromLayer only works after 2nd usage</title>
      <link>https://community.esri.com/t5/python-questions/applysymbologyfromlayer-only-works-after-2nd-usage/m-p/1269016#M67147</link>
      <description>&lt;P&gt;The updated code with the save project lines.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import numpy as np

outRasterName = arcpy.GetParameterAsText(0)   # raster output to FGDB
histlyrx = arcpy.GetParameter(1)    # layer file input for raster symbology
histlayout = arcpy.GetParameter(2)   # layout file input

# create NumPy array to be dummy output raster for demo 
hist2D_arr = np.zeros((256,256), dtype='uint32')

# NumPyArrayToRaster(); write out to file geodatabase
outRaster = arcpy.NumPyArrayToRaster(hist2D_arr)
outRaster.save(outRasterName)

# import layout file if needed; also creates map 'Histogram' which is in layout 
currProject = arcpy.mp.ArcGISProject("CURRENT")
layoutList = currProject.listLayouts("HistogramLayout")
if not(layoutList):
    currProject.importDocument(histlayout)
    currProject.save()

# confirm if map named 'Histogram' is present in the current Project. If not,
# issue error message.
histMapList = currProject.listMaps("Histogram")
if histMapList:
    arcpy.AddMessage("Histogram map is present.\n")
    histMap = histMapList[0]
    histlyr = histMap.addDataFromPath(outRasterName)
    currProject.save()
    histlyr = arcpy.management.ApplySymbologyFromLayer(histlyr, histlyrx)
    arcpy.AddMessage(histlyr)
else:
    arcpy.AddError("No map named Histogram found.") 

# Use SetParameter to associate the derived layer required by
# ApplySymbologyFromlayer to the corresponding item in the parameter list
arcpy.SetParameter(3,histlyr)

currProject.save()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2023 15:59:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/applysymbologyfromlayer-only-works-after-2nd-usage/m-p/1269016#M67147</guid>
      <dc:creator>PeterDowty</dc:creator>
      <dc:date>2023-03-17T15:59:52Z</dc:date>
    </item>
  </channel>
</rss>

