Add tif, jpeg2000, or Sid to current project with standalone Python script

763
1
03-07-2014 07:31 AM
JeffreyPardus
New Contributor
Any insight on how to do this?  I've been looking all over for a stand alone script and I've found nothing unfortunately.  I'm Running ArcGIS 10.1
Tags (2)
0 Kudos
1 Reply
JeffreyPardus
New Contributor
After doing a ton of digging, trial and error, found the solution.

import arcpy, os
from arcpy import env
import re

mxdPath = r'put path to your mxd here'
md = arcpy.mapping.MapDocument(mxdPath)
df = arcpy.mapping.ListDataFrames(md)[0]
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r'folder where your rasters are'
print "Adding nameoflayer layer to your project, please wait..."
#this section is optional if you're going to add it to the data frame, if not create a new group layer and save it as a lyr
targetGroupLayer = arcpy.mapping.ListLayers(md, "name of group layer", df)[0] 
addLayer = arcpy.mapping.Layer(r'location and name of the layer file you created.lyr')
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM")
layers = arcpy.mapping.ListLayers(md)
for layer in layers:
    if layer.name == "New Group Layer":
        layer.name = "nameoflayer"
addtominemaps = arcpy.mapping.ListLayers(md, �??whatever you want to rename it to", df)[0]
#end of optional text - this now looks for your rasters and adds them
rasterList = arcpy.ListRasters("*", "ALL")
for raster in rasterList:
    layername = raster[:-4]
    lyrfile = layername   
    result = arcpy.MakeRasterLayer_management(raster, layername)
    layer = result.getOutput(0)
#you can do addlayer instead of addlayertogroup if you're adding them to the dataframe instead of a specific layer
    arcpy.mapping.AddLayerToGroup(df, addtominemaps, layer, 'AUTO_ARRANGE')
    print "Adding " + raster + " to nameoflayer..."
md.save()
del md
0 Kudos