What i want to do seems simple. But again i can't even find a workaround...

I have got a lot of Raster-files (ESRI-GRID) which i need to insert into a layout mxd-file and then export the map to PDF/JPEG/whatever.

The Final Export in Python is no Problem.
But how do I insert/replace a Raster in the mxd file?
It has to be something like:

fcs = gp.ListRasters("*", "GRID")
for fc in fcs:
    [change Raster (/ Raster Data Source?)]
    arcpy.mapping.ExportToPDF(mxd, "D:\\test.pdf")

I Hope that anybody has an idea :)
Thanks for your help!


Here is a script that I used for changing all the sources in a map document from an SDE raster catalogs to raster mosaics. This could hopefully be a good lead for what you are trying to do. Notice that I pre-built the .lyr files and stored them in a specific location. You can use artwork21's method to automatically create .lyr files too instead of prebuilding.

import arcpy
path = u'sample' #Our internal path to the mosaic directory. Removed from this sample.
mxd = arcpy.mapping.MapDocument("CURRENT") #You can also have this go to a specific mxd
print "Updating Aerial Catalogs to Aerial Mosaics..."
for df in arcpy.mapping.ListDataFrames(mxd):
        for refLayer in arcpy.mapping.ListLayers(mxd, "*.AERIAL_*", df):
                year = refLayer.dataSource[-4:] #This is based on our naming convention, AERIAL_YYYY
                print  u'Updating AERIAL_' + year + u' to Mosaic....
                if year == u'1970':
                        year = u'1970_72'
                if year == u'1997':
                        year = u'1995_97'
                name = u'Mosaic_' + year + u'.lyr'
                mosaic = arcpy.mapping.Layer(path + name) #Making a new layer
                arcpy.mapping.InsertLayer(df, refLayer, mosaic, "BEFORE") #Insert the layer in front of the layer I am replacing
                mosaic.visible = refLayer.visible #Copying across all the attributes on the old layer
                mosaic.brightness = refLayer.brightness
                mosaic.contrast = refLayer.contrast
                mosaic.transparency = refLayer.transparency
                mosaic.name = refLayer.name
                arcpy.mapping.RemoveLayer(df, refLayer) #Take out the old raster layer
                print name + u' updated.'
arcpy.RefreshActiveView() #Not needed if you are not working on an open document
arcpy.RefreshTOC() #Not needed if you are not working on an open document
del mxd, df
        del refLayer, year, name, path, mosaic #Do your cleanup properly
        print u'All Mosaics updated!'

I have a much more complicated version of this that spiders through a drive and changes -every- mxd on the drive too, if that is needed.


Save you raster as a layer file first, then you can use the AddLayer option. Below is a link to a parallel question to yours.



Here is how you can save a raster to a lyr in python:

  # Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

# Local variables...
Aerial_0622_tif_lyr = "C:\\Temp\\Aerial_0622.tif.lyr"
Aerial_06_tif = "Aerial_06.tif"

# Process: Save To Layer File...
gp.SaveToLayerFile_management(Aerial_06_tif, Aerial_0622_tif_lyr)
  • but how can i save a raster as a layer file in python? – Markus Jul 6 '11 at 16:26
  • 1
    @Markus, I update my answer. – artwork21 Jul 6 '11 at 16:44

Okay, thanks to @blord-castillo and @artwork21 this is the final script that has worked for me!!

import arcpy, os, string, logging, datetime
from arcpy import env

path = "E:\\mappingtest\\raster\\" #Path to Raster
outPDFpath = "E:\\mappingtest\\out.pdf" #final PDF Output
mxd = arcpy.mapping.MapDocument("E:\\mappingtest\\mxddtet.mxd") 
outputPDF = arcpy.mapping.PDFDocumentCreate(outPDFpath)

fcs = arcpy.ListRasters("*", "GRID")

for fc in fcs:
    fc_lyr=path + fc[:-1] + str(x) +"_temp.lyr" 
    fc_pluspath=path + fc

    for df in arcpy.mapping.ListDataFrames(mxd):
        for refLayer in arcpy.mapping.ListLayers(mxd, "*[insert-keyword]*", df):
            arcpy.MakeRasterLayer_management(fc_pluspath, fc_rl_temp, "#", "", "#") 
            arcpy.SaveToLayerFile_management(fc_rl_temp, fc_lyr)
            lyr_file = arcpy.mapping.Layer(fc_lyr)
            arcpy.mapping.InsertLayer(df, refLayer, lyr_file, "BEFORE") # Insert New
            arcpy.mapping.UpdateLayer(df, lyr_file, sourceLayer, "TRUE") # Update Symbology with example lyr-file
            arcpy.mapping.RemoveLayer(df, refLayer) # Remove Old
    PDFPath = path[:-4] + str(x) + "_temp.pdf"
    arcpy.mapping.ExportToPDF(mxd, PDFPath)

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.