Select to view content in your preferred language

findAndReplaceWorkspacePaths for different ArcSDE connections in an mxd

1460
4
07-27-2011 05:56 PM
GordonSumerling
Esri Regular Contributor
Hi,

I am using the findAndReplaceWorkspacePaths for ArcSDE databases in mxd's in a series of folders to update the 3 teir connections to Direct connections. The command I am using is:

mxd.findAndReplaceWorkspacePaths(find_workspace_path=r"", replace_workspace_path=r"C:\Temp\arcsde_DIRECT.sde")

This works fine if I want to replace all the connections in the MXD.

I have the situation though where I have multiple people make mods to the mxd's and have the following list of connections strings:
C:\Documents and Settings\Fred\Application Data\ESRI\ArcCatalog\Direct_vector.sde
C:\Documents and Settings\Fred\Application Data\ESRI\ArcCatalog\Direct_raster.sde
C:\Documents and Settings\Johnathon\Application Data\ESRI\ArcCatalog\Direct_vector.sde
SERVER=production;INSTANCE=port:5152;USER=Johnathon;

Connections 1,3,4 are all the same database while 2 is a different database.

Is there a way I can wildcard search for the direct_vector.sde (1,3,4) connection without Explicitly stating the path? For example:
mxd.findAndReplaceWorkspacePaths(find_workspace_path=r"*\ArcCatalog\Direct_vector.sde", replace_workspace_path=r"C:\Temp\arcsde_DIRECT.sde")

and how can I use the findAndReplaceWorkspacePaths to update the 4th connection?

Cheers
Gordon
Tags (2)
0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
You can use a wildcard to select the layers, for example by specify the database name.  Below is an example:

lstMXDs = glob.glob(env.workspace + "\\" + "*.mxd")

for mxd in lstMXDs:
    mxd = mapping.MapDocument(mxd)
    for df in mapping.ListDataFrames(mxd, ""):
        lstLayers = mapping.ListLayers(mxd, "*", df)
        for lyr in lstLayers:
            if "vector" in lyr.dataSource:
                print lyr.dataSource
                lyr.replaceDataSource("arcsde_DIRECT.sde", "SDE_WORKSPACE", "")
                print "Successfuly updated data sources"
            else:
                print lyr.dataSource
                lyr.replaceDataSource("RASTER.sde", "SDE_WORKSPACE", "")
                print "Successfully updated data sources"
    mxd.save()
del mxd


I'm using the wildcard "vector" to find all data sources in my VECTOR database and replacing the datasource to my SDE direct connection.  If the layer is not in my VECTOR database, I replace it with a new connection to my RASTER database.
0 Kudos
BPriyaK
Regular Contributor

Hello Im trying to solve a similar problem ..

We have moved from multiple sde databases to one single database 

I'm trying replace database connections in an existing mxd with  connection name rather than having paths based on users. and then replace the database connection to the new sde database connection.

I'm looking to accomplish this in a two step approach by using find and replace workspace paths first and replace Datasource later.

I have figured the later part but this is where I'm stuck and manually replacing the paths for each mxd using the set datasource option on arccatalog.

C:\Users\jdoe\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\gis.sde 

C:\Users\djohn\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\gis.sde 

import arcpy,os
from arcpy import env 
env.overwriteOutput = True 
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("WORKSPACEPATH"):
        print lyr.workspacePath
        lyr.findAndReplaceWorkspacePath(lyr.workspacePath,"Database Connections\\gis.sde","TRUE")
print lyr.dataSource‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This is the error that i get


File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\_mapping.py", line 696, in findAndReplaceWorkspacePath
return convertArcObjectToPythonObject(self._arc_object.findAndReplaceWorkspacePath(*gp_fixargs((find_workspace_path, replace_workspace_path, validate), True)))
ValueError: Layer: Unexpected error‍‍‍‍

Any direction on how to resolve this will be appreciated.

Thanks

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

It would help if you reposted you code /blogs/dan_patterson/2016/08/14/script-formatting?sr=search&searchId=147137e7-ef65-4684-ba98-d682e01...‌  so any indentation errors can be eliminated.

May be overkill, but I have a /blogs/myAlaskaGIS/2015/08/31/python-addin-for-data-inventory-and-broken-link-repair?sr=search&searc...‌ which might help.  If the links aren't currently broken, just rename the .sde connection file to make them broken and see if it lists them.  Test on a copy of mxd's in a test folder first.

0 Kudos
BPriyaK
Regular Contributor

Hello Rebecca,

Thanks for the heads up to post code with formatting.

I have figured it out each user has named the connection to the database differently and added if else statements to eliminate the unexpected error msg.

Thanks