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"): sourcePath = os.path.dirname(lyr.dataSource) if lyr.dataSource.count(".sde") > 0: mxd.findAndReplaceWorkspacePaths(sourcePath, r"Database Connections\Test_CartaTest_GISUSER.sde\CartaTest.TRANS.BikeFeatures") #change to Carta instead of CartaTest when done
import arcpy mxd = arcpy.mapping.MapDocument(r"C:\temp\sde_test.mxd") mxd.findAndReplaceWorkspacePaths(r"Database Connections\Connection to Oracle.sde", r"Database Connections\Connection to SQL Server.sde") mxd.saveACopy(r"C:\temp\migrate_server.mxd") del mxd print "done"
I tried to change a layer's dataSource in a gdb, and to different feature class dataset in another gdb.
It took long time that I grasped replaceDataSource needs only gdb path.
Any dataset name is not necessary.
(ex)
if you want to change " fc1 in a.gdb\datasetA " to " fc2 in b.gdb\datasetB "
C:\a.gdb\datasetA\fc1 -> C:\b.gdb\datasetB\fc2
you need to prepare 3 informations :
1. where to change : "C:\b.gdb" dataset name is not necessary !!
2. type of workspace: "FILEGDB_WORKSPACE"
3. name of datasource : "datasetB" if this is RASTER, it needs an extension of the file(RASTER_WORKSPACE), but is SHAPEFILE(SHAPEFILE_WORKSPACE), it dont need an extention.
then, they apply to the function:
lyr.reaplceDataSource( 1 , 2 , 3 )
It may not be an answer you needs.
I hope that it will be helpful for you.
references:
3 [2] python - Arcpy's replaceDataSource Error - Geographic Information Systems Stack Exchange
import arcpy from time import gmtime, strftime Oracle = r"Database Connections\Connection to tempest map.sde" SQLServer = r"Database Connections\Connection to betty4.sde" mxd = arcpy.mapping.MapDocument(r"C:\temp\analysts\forum\sde\Oracle_map.mxd") # examine the input data source - just looking at the first layer lyr = arcpy.mapping.ListLayers(mxd)[0] print "Input data source:", lyr.dataSource mxd.findAndReplaceWorkspacePaths(Oracle, SQLServer) mxd.saveACopy(r"C:\temp\analysts\forum\sde\SQLServer_map.mxd") del mxd, lyr #examine the output data source - just looking at the first layer mxd2 = arcpy.mapping.MapDocument(r"C:\temp\analysts\forum\sde\SQLServer_map.mxd") lyr2 = arcpy.mapping.ListLayers(mxd2)[0] print "Output data source:", lyr2.dataSource del mxd2, lyr2 print "Completed at", strftime("%Y-%m-%d %H:%M:%S", gmtime()) print "========================================"
import arcpy from time import gmtime, strftime SQLServer = r"Database Connections\Connection to betty4.sde" mxd = arcpy.mapping.MapDocument(r"C:\temp\analysts\forum\sde\Oracle_cat.mxd") lyrs = arcpy.mapping.ListLayers(mxd) for lyr in lyrs: #examine the input data sources print "Input data source:", lyr.dataSource lyr.replaceDataSource(SQLServer, "SDE_WORKSPACE", lyr.datasetName, True) mxd.saveACopy(r"C:\temp\analysts\forum\sde\SQLServer_map.mxd") del mxd, lyr #examine the output data source - just looking at the first layer mxd2 = arcpy.mapping.MapDocument(r"C:\temp\analysts\forum\sde\SQLServer_map.mxd") lyr2 = arcpy.mapping.ListLayers(mxd2)[0] print "Output data source:", lyr2.dataSource del mxd2, lyr2 print "Completed at", strftime("%Y-%m-%d %H:%M:%S", gmtime()) print "========================================"