Add Raster File to empty table of content in Arcmap Desktop

507
3
03-18-2022 12:13 AM
TessaKhairaniErmanto
New Contributor II

Hello,

I am trying to add a raster file from my directory to my current mxd file, but with arcpy.mapping.AddLayer function seems only adding one band per file, is there a sign for me to add RGB composite automatically using arcpy? Thankyou for your help.

Tags (3)
0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor
0 Kudos
TessaKhairaniErmanto
New Contributor II

Thank you for your response Dan,

I did, but I am getting confused when making a layer file from table of content, because if I run it manually, the script will only adding 1 band instead of 3 composite band, so I need a trigger to make a layer.

Is it possible for me to making an automize script that will adding the raster layer of my directory file without any trigger? 

Here is my code :

 

import arcpy, os, glob, pythonaddins
import Tkinter as tk
import tkFileDialog as filedialog

mxd = arcpy.mapping.MapDocument("Current")
mxd.author = "Me"
mxd.save()

root=tk.Tk()
root.withdraw()    
folder_selected = filedialog.askdirectory()
root.destroy

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

file_name=[]
for file in os.listdir(folder_selected😞
    if file.endswith(".tif"😞
        file_name.append(file)

# arcpy.mapping.AddLayer(df, arcpy.mapping.Layer(os.path.join(folder_selected,file_name[0])))

for lyr in arcpy.mapping.ListLayers(mxd😞
    lyr.saveACopy(file_name[0] + ".lyr")

source_layer = arcpy.mapping.Layer(os.path.join(folder_selected,(file_name[0] + ".lyr")))

count = 0

for file in os.listdir(folder_selected😞
    if file.endswith(".tif"😞
        name = file.split(".")[0]

        rasterPath = os.path.join(folder_selected,file)
        update_layer = arcpy.mapping.Layer(rasterPath)
        arcpy.mapping.UpdateLayer(df, update_layer, source_layer, True)
        arcpy.mapping.AddLayer(df,update_layer,"BOTTOM")
        count = count + 1
0 Kudos
DanPatterson
MVP Esteemed Contributor

I would go with makerasterlayer.  perhaps it behaves differently than mapping's AddLayer


... sort of retired...