4

Using ArcGIS 10 wanted to update the raster file in a mxd file using python. for example, i created a.mxd which contains b.tif file in it. now, i obtained c.tif and want to update the a.mxd, i.e. want to delete b.tif and add c.tif, or overwrite b.tif with c.tif

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\data\a.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
updateLyr = arcpy.mapping.ListLayers(mxd)[0]
sourceLyr = arcpy.mapping.Layer(arcpy.Raster(r"C:\data\c.tif"))
arcpy.mapping.UpdateLayer(df, updateLyr, sourceLyr, True)
del mxd

i started as above, but i think that i am not getting most of the things correctly.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 13, 2011 at 20:11

2 Answers 2

3

I think you want replaceDataSource instead of UpdateLayer. Update uses a template, the sourceLyr, and applies its properties (data path, symbology, etc.) to the layer-to-be-updated whereas you only want to change the data path/filename.

See Updating and fixing data sources with arcpy.mapping in the Esri docs for more detail, and Changing data source path involving feature dataset in *.lyr files using ArcPy? here on GIS.se for some quirky behaviour to be aware of.

answered Sep 27, 2011 at 23:21
2

Create a layer file consisting of your new tif, named 'c.lyr' and place it in the same directory (i.e. c:\data\c.lyr) Then run this inside the map document:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for df in arcpy.mapping.ListDataFrames(mxd):
 for refLayer in arcpy.mapping.ListLayers(mxd, "*b.tif", df):
 mosaic = arcpy.mapping.Layer(u'C:\\data\\c.lyr')
 arcpy.mapping.InsertLayer(df, refLayer, mosaic, "BEFORE")
 mosaic.visible = refLayer.visible
 mosaic.brightness = refLayer.brightness
 mosaic.contrast = refLayer.contrast
 mosaic.transparency = refLayer.transparency
 mosaic.name = refLayer.name
 arcpy.mapping.RemoveLayer(df, refLayer)
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd, df
try:
 del refLayer, mosaic
except:
 pass
finally:
 print u'TIF updated!'

Or if you prefer a function:

def UpdateTIF(mxd, old, new):
#mxd: path to mxd to update
#old: wildcard matching pattern for the old source
#new: layer file referencing new source
 import arcpy
 mxd = arcpy.mapping.MapDocument(mxd)
 for df in arcpy.mapping.ListDataFrames(mxd):
 for refLayer in arcpy.mapping.ListLayers(mxd, old, df):
 mosaic = arcpy.mapping.Layer(new)
 arcpy.mapping.InsertLayer(df, refLayer, mosaic, "BEFORE")
 mosaic.visible = refLayer.visible
 mosaic.brightness = refLayer.brightness
 mosaic.contrast = refLayer.contrast
 mosaic.transparency = refLayer.transparency
 mosaic.name = refLayer.name
 arcpy.mapping.RemoveLayer(df, refLayer)
 del mxd, df
 try:
 del refLayer, mosaic
 except:
 pass
 finally:
 print u'TIF updated!'

Execute this with a call like:

UpdateTIF('C:\\data\\a.mxd', '*b.tif', 'C:\\data\\c.lyr')
answered Sep 28, 2011 at 0:43

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.