Layer.replaceDataSource

11593
5
09-27-2012 05:01 AM
MaciekFertala1
New Contributor
Hi,
I'm trying to change layer data source from one shape file to another in different workspace (directory).

mxd = arcpy.mapping.MapDocument(r"Z:\02_GIS\Project\mxd\P01.mxd")
print "mxd"
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyrfile = arcpy.mapping.Layer(r"Z:\Maps\GIS_utilities\python_script\lyr\_kabel.lyr")
print lyrfile.workspacePath # old path
lyrfile.replaceDataSource(r"Z:\02_GIS\Project\wektor", "SHAPEFILE_WORKSPACE", "P01_RIVER.shp")


But this way its not working. What should I change?

Thanks form help,
MF
Tags (2)
0 Kudos
5 Replies
JakeSkinner
Esri Esteemed Contributor
Try the following:

mxd = arcpy.mapping.MapDocument(r"Z:\02_GIS\Project\mxd\P01.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
    if lyr.name == "_kabel":
            lyr.replaceDataSource(r"Z:\02_GIS\Project\wektor", "SHAPEFILE_WORKSPACE", "P01_River")

mxd.save()
0 Kudos
JeffBarrette
Esri Regular Contributor
Are you trying to change the workspace for a data source in a layer file or for a layer in an mxd?  Both scenarios are supported but the two snippets of code are doing different things?

If you are trying to work with layer files you are missing one step.  Treat a layer file like an MXD.  Once you connect to it, you still need to search for the layers within it using ListLayers.

#In example below, a layer file if pointing to a shapefile in c:\temp
#It is redirected to a data source in c:\active
#and then saved to a new layer file (in c:temp)
#Old shapefile was usa.shp, new one is use2.shp

import arcpy
lyrFile = arcpy.mapping.Layer(r"C:\Temp\usa.lyr")
for lyr in arcpy.mapping.ListLayers(lyrFile):                  #THIS IS THE MISSING PIECE
  if lyr.name == "usa":
    lyr.replaceDataSource(r"C:\Active", "SHAPEFILE_WORKSPACE", "usa2")
lyrFile.saveACopy(r"C:\Temp\usa2.lyr")


Jeff
MaciekFertala1
New Contributor
Im trying to change data source in a layer in an mxd.
MF
0 Kudos
JeffBarrette
Esri Regular Contributor
Then try what Jake suggests.  In your original code, you reference an MXD but never a layer within it.  Rather you are referencing a layer file via the .Layer function.  You need to use ListLayers to reference a layer in an MXD.  Then you can switch out the data source.

Jeff
Yaron_YosefCohen
Occasional Contributor II

according to Jake note this script work fine for me:

import arcpy, os, sys

from arcpy import env

env.workspace = r"F:\Projects\Nadav\gany_tikva\gis"

for mxdname in arcpy.ListFiles("*.mxd"):

    print mxdname

    mxd = arcpy.mapping.MapDocument(r"F:\Projects\Nadav\gany_tikva\gis\\" + mxdname)

    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

    for lyr in arcpy.mapping.ListLayers(mxd, "*", df):

        if lyr.name == "50000.sid":

           

            # very importent that "RASTER_WORKSPACE" will change according to

            # the data we want to replace

           

            lyr.replaceDataSource(r"F:\GIS\topo_50000", "RASTER_WORKSPACE", "50000.sid", True)

            print 'replaceDataSource'      

    mxd.save()

del mxd

0 Kudos