Not understanding replaceDataSource method

1791
4
08-20-2010 09:22 AM
MichaelBishopp
Occasional Contributor
I am trying to implement the replaceDataSource method in some code and am having no luck.  The code is:

mxd = arcpy.mapping.MapDocument(fullpath)
            DFList = arcpy.mapping.ListDataFrames(mxd)
            for df in DFList:
                lyrList = arcpy.mapping.ListLayers(mxd, "", df)
                for lyr in lyrList:
                    if lyr.supports("DATASOURCE"):
                        #print lyr.dataSource
                        if lyr.dataSource == r"Database Connections\GIS_USER2PGIS.sde\SDE.Streets\SDE.LISStreetSegment":
                            lyr.replaceDataSource(r"Database Connections\Test_Carta_LIS.sde", "SDE_WORKSPACE", "Carta.LIS.StreetSegment")
                            lyr.name = "Carta.LIS.StreetSegment"


I am wondering if the SDE connections are just not working.  Any thoughts?

Michael
0 Kudos
4 Replies
JamesHood
Occasional Contributor
I don't know anything about ArcPy or SDE servers,  but perhaps you are having difficulties in the way your are populating your lists and the code structure?

I might change your code to the following... but thats for python not ArcPy?  Are they very different?:

mxd = arcpy.mapping.MapDocument(fullpath)

DFList = []
while arcpy.mapping.ListDataFrames(mxd):
    DFinsert = arcpy.mapping.ListDataFrames(mxd)
    DFList.append(DFinsert)     

lyrList = [] 
for df in DFList:
    lyrInsert = arcpy.mapping.ListLayers(mxd, "", df)
    lyrList.append(lyrInsert)

for lyr in lyrList:
    if lyr.supports("DATASOURCE"):
        #print lyr.dataSource
        if lyr.dataSource == r"Database Connections\GIS_USER2PGIS.sde\SDE.Streets\SDE.LISStreetSegment":
            lyr.replaceDataSource(r"Database Connections\Test_Carta_LIS.sde", "SDE_WORKSPACE", "Carta.LIS.StreetSegment")
            lyr.name = "Carta.LIS.StreetSegment"
0 Kudos
MichaelBishopp
Occasional Contributor
I don't know anything about ArcPy or SDE servers,  but perhaps you are having difficulties in the way your are populating your lists and the code structure?

I might change your code to the following... but thats for python not ArcPy?  Are they very different?:

mxd = arcpy.mapping.MapDocument(fullpath)

DFList = []
while arcpy.mapping.ListDataFrames(mxd):
    DFinsert = arcpy.mapping.ListDataFrames(mxd)
    DFList.append(DFinsert)     

lyrList = [] 
for df in DFList:
    lyrInsert = arcpy.mapping.ListLayers(mxd, "", df)
    lyrList.append(lyrInsert)

for lyr in lyrList:
    if lyr.supports("DATASOURCE"):
        #print lyr.dataSource
        if lyr.dataSource == r"Database Connections\GIS_USER2PGIS.sde\SDE.Streets\SDE.LISStreetSegment":
            lyr.replaceDataSource(r"Database Connections\Test_Carta_LIS.sde", "SDE_WORKSPACE", "Carta.LIS.StreetSegment")
            lyr.name = "Carta.LIS.StreetSegment"


I'm not really struggling with having the code work except for the piece that says

if lyr.dataSource == r"Database Connections\GIS_USER2PGIS.sde\SDE.Streets\SDE.LISStreetSegment":
            lyr.replaceDataSource(r"Database Connections\Test_Carta_LIS.sde", "SDE_WORKSPACE", "Carta.LIS.StreetSegment")
            lyr.name = "Carta.LIS.StreetSegment"
0 Kudos
MichaelBishopp
Occasional Contributor
I'm not really struggling with having the code work except for the piece that says

if lyr.dataSource == r"Database Connections\GIS_USER2PGIS.sde\SDE.Streets\SDE.LISStreetSegment":
            lyr.replaceDataSource(r"Database Connections\Test_Carta_LIS.sde", "SDE_WORKSPACE", "Carta.LIS.StreetSegment")
            lyr.name = "Carta.LIS.StreetSegment"


I even have tried the example code with no success.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
for lyr in arcpy.mapping.ListBrokenDataSources(mxd):
    if lyr.supports("DATASOURCE"):
        if lyr.dataSource == r"C:\Project\Data\Transportation.gdb\MajorRoads":
            lyr.replaceDataSource(r"C:\Project\Data\Transportation.gdb", "FILEGDB_WORKSPACE", "Highways")
            lyr.name = "Highways"
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd


I set up the file based GDB and a map doc called Project.mxd.  I created the Transportation.gdb and added data to a FeatureClass called MajorRoads. I added the FeatureClass MajorRoads to the Project.mxd.  I saved and quit out of the document. I changed the Name of the featureClass in the Transportation.gdb from MajorRoads to Highways.  I ran the script---everything looked like to worked (no error messages) and it still did not change the DataSource in the Project2.mxd.  Can someone else try this example code and see if it works for them?
0 Kudos
richwebb
New Contributor
I think you may have to include the path to the SDE connection file e.g. something like below
depending on where your .SDE con file exists...

"C:\Users\wbm_admin\AppData\Roaming\ESRI\ArcCatalog\GIS_USER2PGIS.sde\SDE.Streets\SDE.LISStreetSegment"
0 Kudos