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.

i 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... plz can you help me? thanks a lot!

share|improve this question
add comment

2 Answers

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 Change data source path in .LYR files in Arcgis 10 here on GIS.se for some quirky behaviour to be aware of.

share|improve this answer
add comment

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')
share|improve this answer
add comment

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.