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.

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.