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.

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.

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')

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.