I'm trying to make a .lyr file to bring into ArcMap with arcpy.mapping
. I can bring a single raster into a .lyr with the script below. I'm unsure how to proceed to get multiple rasters from the same folder into a .lyr. Any help appreciated.
Thanks
# Import system modules
import arcpy, os
from arcpy import env
# Workspace Directory
env.workspace = r'F:\Ortho_prelim\TEST'
workspace = r'F:\Ortho_prelim\OUT'
# Set local variables
inLyr = 'ImageLyr'
outLyr0 = 'ImageLyr'
outLyr = workspace + os.sep + 'Image.lyr'
# Make feature layer variables
fList = os.listdir(r'F:\Ortho_prelim\TEST')
for f in fList:
if fList.endswith('.ecw'):
print fList
arcpy.MakeRasterLayer_management(f, outLyr0, "", "")
print 'Made ' + outLyr0
print outLyr0
# Save Feature Layer
arcpy.SaveToLayerFile_management(inLyr, outLyr)
print 'Made layer ' + outLyr
So the pseudo code should be something like:
- Look in folder
- Look up all files with extension .ecw
- Add all the .ecw files in the folder to a .lyr file...
Chad Cooper
12.8k4 gold badges48 silver badges87 bronze badges
-
1Do you really want to use Python for this? I would multiple select all the rasters from the Catalog window, drag and drop them into an empty map (they should stay selected), then right click to create a Layer Group which you can right click to Save as Layer File.PolyGeo– PolyGeo ♦2013年01月25日 08:36:01 +00:00Commented Jan 25, 2013 at 8:36
-
I would do that, but I have 191 .ecw files and ArcMap crashes when I try to load them that way. This script is a precursor to adding the .lyr file to map with the arcpy.mapping module and then printing to pdf.user14589– user145892013年01月25日 15:20:16 +00:00Commented Jan 25, 2013 at 15:20
1 Answer 1
Hi maybe you could do something like this.
fList = os.listdir(r'F:\Ortho_prelim\TEST')
count = 0
for f in fList:
if fList.endswith('.ecw'):
outlyr = "outlyr" + str(count)
print fList
arcpy.MakeRasterLayer_management(f, outLyr, "", "")
print 'Made ' + outLyr
arcpy.SaveToLayerFile_management(inLyr, outLyr)
print 'Made layer ' + outLyr
count = count + 1
-
I think this is along the lines of what I need. However I really need it to iterate through the .lyr files so I end up with: Image1.ecw in Layer1.lyr >> Image2.ecw in Layer2.lyr >> etc. Then I can add all the .lyr's to a group layer in ArcMap with the arcpy.mapping module. Thanks for your help!user14589– user145892013年01月25日 15:30:27 +00:00Commented Jan 25, 2013 at 15:30
-
Yep, code in post above didn't work out because I need to iterate each layer as well. Not sure on correct syntax for that!?user14589– user145892013年01月28日 00:02:46 +00:00Commented Jan 28, 2013 at 0:02
-
lang-py