Loading a raster into a dataframe in arcpy

1945
2
Jump to solution
02-04-2011 08:24 AM
MannyGimond
New Contributor III
When attempting to load a raster (created in arcpy) using arcpy.mapping.AddLayer, I get the following error message:

<type 'exceptions.AssertionError'>

Here's a snippet of the code:
env.workspace = arcpy.GetParameterAsText(0)
...
YmapFile      = arcpy.GetParameterAsText(4) # Output filename
...
YMAP = arcpy.NumPyArrayToRaster(YmapArray,lowerLeft,pixSize,pixSize)
YMAP.save(YmapFile)
...
mxd = arcpy.mapping.MapDocument("CURRENT")
df    = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.MakeRasterLayer_management(YmapFile,"Y Map")
arcpy.mapping.AddLayer(df,"Y Map", "BOTTOM")


The raster is properly created and saved in the folder/geodatabase defined by env.workspace.

Also, I can load the raster layer using the arcpy.SetParameterAsText() function (when the parameter is set as a derived entity in the scipt's properties option box), so I know that the layer is being properly created.

Any suggestions?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III
You'll need to save a raster layer as a .lyr file to disk with the symbology you want, then use arcpy.mapping.Layer to open the layer file and set the dataSoruce attribute to the source of the new raster you've created.

View solution in original post

0 Kudos
2 Replies
JasonScheirer
Occasional Contributor III
You'll need to save a raster layer as a .lyr file to disk with the symbology you want, then use arcpy.mapping.Layer to open the layer file and set the dataSoruce attribute to the source of the new raster you've created.
0 Kudos
MannyGimond
New Contributor III
Thanks Jason. Saving the layer to a file was the missing link to the workflow. Here's the modified "working" code snippet:

env.workspace = arcpy.GetParameterAsText(0)
...
YmapFile      = arcpy.GetParameterAsText(4) # Output filename
...
YMAP = arcpy.NumPyArrayToRaster(YmapArray,lowerLeft,pixSize,pixSize)
YMAP.save(YmapFile)
...
mxd = arcpy.mapping.MapDocument("CURRENT")
df    = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.MakeRasterLayer_management(YmapFile,"Y Map")
ymapLyr = arcpy.mapping.Layer(r"c:/tmp/YMAP.lyr")
arcpy.mapping.AddLayer(df, ymapLyr,"BOTTOM")


I'm now encountering another stumbling block. arcpy.mapping.Layer is saving a layer file that points to some obscure C:\Users\jdoeL\AppData\Local\Temp\19374123947128412.xc8915868_b3b2_4d7b_9bc6_aec2e2cbbc89y0.afr file (the raster layer it should point to is in a different folder/geodatabase). Oddly, if the raster layer is removed from its location, the layer file link is broken (indicating that the *.afr is a pointer to a pointer?).

This poses a problem when I attempt to use that layer in a Raster Calculator operation. The geoprocessing does not seem to like the .afr pointer. Any idea what I may be doing wrong?

I'm using 10 SP1.
0 Kudos