AnsweredAssumed Answered

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

Question asked by drc_ on May 23, 2014
Latest reply on May 26, 2014 by drc_
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( 

.... 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:

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