0

I would like to use a similar code as below, but one that uses relative paths. Since my input is a batch of tif files (not layers), I do not know if I need to use other functions like arcpy.SaveToLayerFile_management or os.path.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")

for lyr in arcpy.mapping.ListLayers(mxd):

    lyr.saveACopy(lyr.name + ".lyr")
  • I should be specific that I'd also like to also have a relative input and output folder rather than importing from a document. – AF2k15 Mar 1 '15 at 23:16
  • Is your output a layer? Do you want to save in the same folder it came from, or a subfolder? Is this to run in ArcMap? Is the folder relative to the map document or the location of the layer? – Michael Stimson Mar 1 '15 at 23:29
  • I'd like the output to go into a separate folder location; so the infolder containing the tiffs is a different folder location, and I would like to run it with the windows command window, without arcmap actually open. Also, I'd like for the output to have .tif.lyr extensions. – AF2k15 Mar 1 '15 at 23:33
  • So you supply the location of the tiff files then the output folder and you want layer files created for each tiff - is that right? – Michael Stimson Mar 1 '15 at 23:34
  • Correct. Without calling an MXD. – AF2k15 Mar 1 '15 at 23:35
1

Add all of your Tiffs to map by dragging them from ArcCatalog. then use this code in python windows:

import arcpy
from arcpy import env

mxd = arcpy.mapping.MapDocument("CURRENT")
env.workspace = "d:/Layers"

for lyr in arcpy.mapping.ListLayers(mxd):
    print lyr.name
    try:
        arcpy.SaveToLayerFile_management(lyr, lyr.name + ".lyr", "RELATIVE")
    except:
        print arcpy.GetMessages()

This code create new layers with relative path, from your tiffs, and put them in D:\Layers directory. Change this directory accordingly.

1

This should work:

import arcpy, os, sys

InFolder = sys.argv[1]
OutFolder = sys.argv[2]

arcpy.env.workspace = InFolder

for ThisRas in arcpy.ListRasters():
    arcpy.MakeRasterLayer_management (ThisRas,"Layer");
    arcpy.SaveToLayerFile_management("Layer",OutFolder + "\\" + ThisRas + ".lyr",True,"CURRENT");
    arcpy.Delete_management("Layer")

It creates a raster layer then saves it as a layer file. You can adjust the naming of the layer... however the symbology will be default. The reason to save from the map document is that the symbology is set, these layers will be no different to adding the tiff files to ArcMap. You could use a tool like Apply Symbology from Layer to copy an existing layers symbology to the new layers.

  • Ah, I thought I might need that combination of functions. However, I'm not familiar with sys.argv. I was guessing I would need to put in the path to the input and output folders -- something like InFolder = r"C:\VMshared\NoName_2009_50m\SnowDepth2". Does this need to be supplied somehow? – AF2k15 Mar 1 '15 at 23:50
  • sys.argv is the arguments that are entered at the command line, if it is typed script.py arg1 arg2 arg3 then sys.argv[1] is arg1. Be careful of spaces when executing from command prompt: script.py "this is arg 1" arg2 arg3 is correct or the first argument is this and not this is arg 1. It's your code, feel free to change sys.arg with a hard path (don't forget to double slash paths or use r'path' for raw input). – Michael Stimson Mar 1 '15 at 23:55
  • Great, that structure works. I'll just set the folder equal to the actual paths for this. Seems a little easier. Calling an mxd wasn't going to work with 3,000 some tifs :) – AF2k15 Mar 1 '15 at 23:59
  • Yes, 3k + layers wouldn't crash ArcMap but you'd be waiting for a very long time! I've done 5k shapefile layers (it took 8 hours to draw). The trick is to turn off the rendering, that way it doesn't refresh each time you change one layer. – Michael Stimson Mar 2 '15 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.