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.8k4 gold badges48 silver badges87 bronze badges
asked Jan 25, 2013 at 6:40
2
  • 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. Commented 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. Commented Jan 25, 2013 at 15:20

1 Answer 1

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
answered Jan 25, 2013 at 8:08
3
  • 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! Commented 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!? Commented Jan 28, 2013 at 0: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. Commented Jan 28, 2013 at 23:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.