1

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,714
  • 4
  • 46
  • 87
user14589
  • 21
  • 3
  • 1
    Do 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 Jan 25 '13 at 08: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 Jan 25 '13 at 15:20

1 Answers1

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
dango
  • 771
  • 4
  • 16
  • 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 Jan 25 '13 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 Jan 28 '13 at 00:02
  • If the step you are stuck at is adding your 191 layers into a single group layer in the TOC and then saving that out as a single layer file then perhaps review this Q&A from a few days ago. – PolyGeo Jan 28 '13 at 23:05