arcpy AddColormap issue

1704
5
03-16-2016 05:43 AM
RandalGreene1
New Contributor III

In 10.3.1, when using arcpy.AddColormap_management in a script tool, the input must be passed from a Raster Dataset with a full path to the dataset, not from a Raster Layer in an ArcMap document! If passed from a Raster Layer, it results in "ERROR 000199: Failed to add Colormap".

0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

Because the script, if run standalone, has no concept of what a layer is, so you either have to add code that using the arcpy mapping module, to retrieve the layer from the mxd or get it from disk.  So sample code of what you are trying to do would help

RandalGreene1
New Contributor III

Thanks Dan for the quick reply. The issue only occurs when the script tool is being run inside ArcMap and I select a raster layer as the input. Here is the code:

# Import arcpy module
import arcpy

# Script arguments
Forest_tif = arcpy.GetParameterAsText(0)
# required arg - comment following in production
if Forest_tif == '#' or not Forest_tif:
    Forest_tif = "C:\\GIS\\KML\\publish\\oberniki\\Forest.tif" # provide a default value if unspecified

Forest_colour_tif = arcpy.GetParameterAsText(1)
# required arg - comment following in production
if Forest_colour_tif == '#' or not Forest_colour_tif:
    Forest_colour_tif = "C:\\GIS\\KML\\publish\\oberniki\\Forest_colour.tif" # provide a default value if unspecified

# Local variables (files in same path as toolbox/script)
Forest_tif_lyr = "Forest.tif.lyr"
Forest_tif_clr = "Forest.tif.clr"

# Set Geoprocessing environments
arcpy.env.outputCoordinateSystem = "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]"
arcpy.env.overwriteOutput = True

# Make Raster Layer
arcpy.MakeRasterLayer_management(Forest_tif, "temp_layer", "", "", "")

# Apply Symbology From Layer
arcpy.ApplySymbologyFromLayer_management("temp_layer", Forest_tif_lyr)

# Add Colormap
arcpy.AddColormap_management("temp_layer", "#", Forest_tif_clr)

# Copy Raster to generate RGB
arcpy.CopyRaster_management("temp_layer", Forest_colour_tif, "", "", "255", "NONE", "ColormapToRGB", "8_BIT_UNSIGNED", "NONE", "NONE")
0 Kudos
DanPatterson_Retired
MVP Emeritus

why don't you set a workspace line  to specify the folder where the layer files reside

arcpy.env.workspace = .....

0 Kudos
RandalGreene1
New Contributor III

That's a good idea, but it's not related to the problem. arcpy.AddColormap_management will still fail with the error above!

0 Kudos
DanPatterson_Retired
MVP Emeritus

if this is a layer file, it would work in arcmap because it is just a layer... but I just saw that your layer file has

Forest.tif.lyr two periods in it.  Can you confirm the actual filename and the name of the layer in arcmap.  and for extra measure specify the actual path to the layer file to check.  In all cases, print the layer name whether it is being read from the project or disk , particularly the latter to ensure paths are correct