0

I am using the following code to add the raster layer using arcpy in arcmap

def AddRasterLayer(path,layerName):
 layer=os.path.join(path,'new'+str(layerName)+'.lyr')
 mxd=arcpy.mapping.MapDocument("CURRENT")
 df=arcpy.mapping.ListDataFrames(mxd,'*')[0]
 arcpy.MakeRasterLayer_management(Raster(layerName),layer)
 newlayer=arcpy.mapping.Layer(layer)
 arcpy.mapping.AddLayer(df,newlayer,'TOP')
 new_name=arcpy.mapping.ListLayers(mxd,newlayer,df)
 new_name[0].name=str(layerName)
 del layer

After using this function raster layer is added to my current data frame. But when I am using any other tools it is showing as in the image

enter image description here

it is showing all the tree structure of the layer. Why is it so ? I need only the layers name to be displayed here.

asked Oct 30, 2014 at 7:07

1 Answer 1

1

This is because you are giving the layer the full path name when you create the Raster Layer. This is what you are doing:

arcpy.MakeRasterLayer_management(Raster(layerName),layer)

The output argument "layer" is a full file path. What you should change it to is this:

arcpy.MakeRasterLayer_management(Raster(layerName),layerName)

This will give you your required result.

I think you are trying to create a layer '.lyr' file using the following code: layer=os.path.join(path,'new'+str(layerName)+'.lyr')

This will not work, to do this please read the following link:

Save To Layer File (Data Management)

answered Oct 30, 2014 at 9:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.