Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.


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!
Markus

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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
try:
        del refLayer, year, name, path, mosaic #Do your cleanup properly
except:
        pass
finally:
        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.

share|improve this answer

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.

http://forums.arcgis.com/threads/22835-Loading-a-raster-into-a-dataframe-in-arcpy

Update:

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)
share|improve this answer
    
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
env.workspace=path
mxd = arcpy.mapping.MapDocument("E:\\mappingtest\\mxddtet.mxd") 
outputPDF = arcpy.mapping.PDFDocumentCreate(outPDFpath)
sourceLayer=arcpy.mapping.Layer("E:\\mappingtest\\raster\\update.lyr")


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

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

    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)
    outputPDF.appendPages(str(PDFPath))
    x=x+1
outputPDF.saveAndClose()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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