arcpy.MakeRasterLayer_management() failed in toolbox script

4781
4
Jump to solution
12-20-2013 06:30 PM
DongxiZheng
New Contributor III
Hello,

Recently I have been trying to implement the "Make Raster Layer" tool in a python script. The code I have is as follow, which takes a file path as the major input with a boolean to indicate whether debugging messages should be output to a file.
import arcpy  debugmsg = "" # Receive the parameters from the toolbox dialog # - the path of the input image in_img_path = arcpy.GetParameterAsText(0) debugmsg += "input: " + in_img_path + "\n"  # - the choice of outputting a debug text file debug = arcpy.GetParameter(1)  # set up variables tmp = in_img_path.split("\\") layername = tmp[len(tmp)-1].split(".")[0] debugmsg += "layer name: " + layername + "\n"  try:     # -- generate a raster layer from image     arcpy.MakeRasterLayer_management(imgname, layername, "#")     debugmsg += arcpy.GetMessages()+"\n" except:     debugmsg += "Make Raster Layer failed.\n"     debugmsg += arcpy.GetMessages()+"\n"  print debugmsg if debug:     f = open(in_img_path[0:len(in_img_path)-4]+'_debug.txt', 'w')     f.write(debugmsg)     f.close()


However, when I executed it, the Make Raster Layer function failed as indicated in the debugging output:
input: I:\WIN\Desktop\image\big.png layer name: big Make Raster Layer failed.


I used the same input directly in the Python window of ArcGIS and it succeeded with a layer named "big" shown up in the table of content. The command follows:
arcpy.MakeRasterLayer_management(r"I:\WIN\Desktop\image\big.png","big")


So, I am confused about why the same command executed differently. Does anyone have the same experience and hopefully a solution? Any suggestion is appreciated.

Some screenshots of invoking the script and the execution result for your reference:
[ATTACH=CONFIG]30058[/ATTACH]
[ATTACH=CONFIG]30059[/ATTACH]
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Add the following to the end of your code:

mxd = arcpy.mapping.MapDocument("CURRENT") df  = arcpy.mapping.ListDataFrames(mxd)[0]  lyr = arcpy.mapping.Layer(layername)  arcpy.mapping.AddLayer(df, lyr) arcpy.RefreshTOC() arcpy.RefreshActiveView()

View solution in original post

0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
Hello,

In your MakeRasterLayer function, it doesn't look like you have the 'imgname' variable defined to anything.  Try the following instead:

arcpy.MakeRasterLayer_management(in_img_path, layername, "#")
0 Kudos
by Anonymous User
Not applicable
Original User: zhengdx04

Hello,

In your MakeRasterLayer function, it doesn't look like you have the 'imgname' variable defined to anything.  Try the following instead:

arcpy.MakeRasterLayer_management(in_img_path, layername, "#")


Thanks for catching that Jake! After correction, the code executed MakeRasterLayer_management() function successfully. However, I still couldn't see it add any layer into my map. When I used it in Python window, it did. Any additional thought?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Add the following to the end of your code:

mxd = arcpy.mapping.MapDocument("CURRENT") df  = arcpy.mapping.ListDataFrames(mxd)[0]  lyr = arcpy.mapping.Layer(layername)  arcpy.mapping.AddLayer(df, lyr) arcpy.RefreshTOC() arcpy.RefreshActiveView()
0 Kudos
by Anonymous User
Not applicable
Original User: zhengdx04

Add the following to the end of your code:

mxd = arcpy.mapping.MapDocument("CURRENT")
df  = arcpy.mapping.ListDataFrames(mxd)[0]

lyr = arcpy.mapping.Layer(layername)

arcpy.mapping.AddLayer(df, lyr)
arcpy.RefreshTOC()
arcpy.RefreshActiveView()


A lot thanks! It is the exact solution, although the last two lines of refreshing code are not a must.
0 Kudos