Hello
I'd like to add several raster files (format: .ecw) from one folder with several subfolders to my mxd-file. It works fine with the following code:
workspace = r"C:\Workspace" #Workspace
...
...
... #Code
... import arcpy, os
...
... mxd = arcpy.mapping.MapDocument("CURRENT")
... df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
...
... walk = arcpy.da.Walk(workspace)
...
... for dirpath, dirnames, filenames in walk:
... for filename in filenames:
... arcpy.mapping.AddLayer(df, arcpy.mapping.Layer(os.path.join(dirpath, filename)))The Problem is, that after adding them like this, they appear with a symbology "stretched values along a color ramp" but they should be drawn as "RGB composite".
Can someone help me?
Solved! Go to Solution.
I got it to work by adding the ECWs, then updating the stretched layer using a template RGB .lyr file:
import os
workspace = r"C:\Temp\Test" #Workspace
rgb_lyr = arcpy.mapping.Layer(r"C:\Temp\rgb.lyr")
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
walk = arcpy.da.Walk(workspace)
for dirpath, dirnames, filenames in walk:
for filename in filenames:
print os.path.join(dirpath, filename)
lyr = arcpy.mapping.Layer(os.path.join(dirpath, filename))
arcpy.mapping.AddLayer(df, lyr)
updateLayer = arcpy.mapping.ListLayers(mxd, lyr.name, df)[0]
arcpy.mapping.UpdateLayer(df, updateLayer, rgb_lyr)You could also use replaceDatasource on the template layer:
import os
workspace = r"C:\Temp\Test" #Workspace
rgb_lyr = r"C:\Temp\rgb.lyr"
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
walk = arcpy.da.Walk(workspace)
for dirpath, dirnames, filenames in walk:
for filename in filenames:
lyr = arcpy.mapping.Layer(rgb_lyr)
lyr.replaceDataSource (dirpath, 'RASTER_WORKSPACE', filename)
lyr.name = filename
arcpy.mapping.AddLayer(df, lyr)
that's perfect!
Thanks a lot! 😄