output parameters aren't added to map until after script ends

301
2
Jump to solution
05-23-2014 01:12 PM
RyanClancy
Occasional Contributor
I've written a Python toolbox tool which among other things, creates a Kernel Density raster as output which is added to the map. I want to control the symbology in the script so that each time it runs the output raster will have the same classified symbology applied. The default symbology for the output of the kernel density tool is stretched, not classified, and according to this I can't change from a stretched symbology to a classified symbology unless I have a .lyr file and use the UpdateLayer function:

The symbologyType on the Layer object is a read-only property. In other words, you can't change a raster classified symbology to a raster unique value symbology. You can only change the properties for a specific symbology class on a layer. The only way to change the symbology type is by publishing the desired result to a layer file and using the UpdateLayer function.


However, the output raster is only added to the map after the script finishes, so any attempt to use arcpy.mapping.UpdateLayer fails because the layer is not in the map until after the script finishes. Here's a snippet:

...stuff happens...  density = "C:\MyGDB.gdb\density" tempDens = KernelDensity(datapoints, "NONE","#","#","SQUARE_MAP_UNITS") arcpy.CopyRaster_management(tempDens, density)  # This adds the output raster to the map. It works, except the raster is only added as a layer after the script finishes parameters[7].value = density  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] lyrList = arcpy.mapping.ListLayers(mxd,"",df)  # This indicates there are zero layers at this point, even though the output parameter was set above messages.addMessage("There are {0} layers in the dataframe.".format(len(lyrList))  # This produces nothing, since the layer isn't in the map densLyr = arcpy.mapping.ListLayers(mxd,"density",df)[0] messages.addMessage("Layer name is: {0}".format(densLyr.name)) 


.... so obviously any calls to the arcpy.mapping.UpdateLayer() function do nothing since the layer isn't in the map yet. It's only after the "Completed script...." message appears in the dialog that the output raster gets added.

I've tried this but it doesn't appear to work:
arcpy.mapping.AddLayer(df,density)


So......... how do I control symbology when the output isn't added to the map until after the script finishes?
0 Kudos
1 Solution

Accepted Solutions
RyanClancy
Occasional Contributor
Problem solved. Since this is a  python toolbox and I set up the tool parameters within the getParametersInfo() function, that's the place to set symbology instead of at the end of the execute() function like in my code above. The solution looks like this:

 def getParametersInfo(self):  outDensity = arcpy.Parameter(     displayName = "Kernel Density",     name = "outDensity",     datatype = "DERasterDataset",     parameterType = "Derived",     direction = "Output") outDensity.symbology = "C:\path_to_my_layer_file.lyr" 

View solution in original post

0 Kudos
2 Replies
RyanClancy
Occasional Contributor
...I should also mention that I've tried this and it doesn't work either.

symbology = "C:\symbology.lyr"
parameters[7].value = density
parameters[7].symbology = symbology
0 Kudos
RyanClancy
Occasional Contributor
Problem solved. Since this is a  python toolbox and I set up the tool parameters within the getParametersInfo() function, that's the place to set symbology instead of at the end of the execute() function like in my code above. The solution looks like this:

 def getParametersInfo(self):  outDensity = arcpy.Parameter(     displayName = "Kernel Density",     name = "outDensity",     datatype = "DERasterDataset",     parameterType = "Derived",     direction = "Output") outDensity.symbology = "C:\path_to_my_layer_file.lyr" 
0 Kudos