Select to view content in your preferred language

Using arcpy.mapping, how to rename sde feature class in mxd files

2830
7
05-01-2013 06:22 AM
AllenGuan1
Deactivated User
We have many mxd files by different users and I want to change one SDE feature class name in all those mxd files without changing SDE connections. As a test, I thought this script would work, but I always get "Unable to change name" exception. Where am I wrong?

mxd = arcpy.mapping.MapDocument(r"L:\users\guana\test_fc_rename.mxd")
for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd,"",df):
        if lyr.supports("DATASOURCE"):
            if lyr.dataSource.find("SDE_GOM.LEXCO_LEASE") <> -1:
                (str1,str2,fname) = lyr.dataSource.split("\\")
                sdecon = str1 + "\\" + str2
                print "sdecon = " + sdecon
                try:
                    lyr.replaceDataSource(sdecon,"SDE_WORKSPACE","SDE_GOM.LEASE_LEXCO")
                except:
                    print "Unable to change name"
0 Kudos
7 Replies
MichaelVolz
Esteemed Contributor
Can you please post what you get for the variable sdecon?

This needs to be a .sde file that you can find in ArcCatalog Add Spatial Database Connection.
0 Kudos
AllenGuan1
Deactivated User
Can you please post what you get for the variable sdecon?

This needs to be a .sde file that you can find in ArcCatalog Add Spatial Database Connection.


For this particular mxd file, the print is "sdecon = Database Connections\guana_npwd@usprd06.sde" (I also tried Database "sdecon=Connections\\guana_npwd@usprd06.sde"). But it can be very varied. Each user connects to sde DB using his own credential. Yes the documention says the first parameter for lyr.replaceDataSource should be a sde connection file, but I also saw an example somewhere that uses something like "Database Connections\user@sdedb.sde". We can not use a common connection file for all users. Should arcpy.mapping have a method that can rename a feature class without changing the workplace path?

Allen
0 Kudos
MichaelVolz
Esteemed Contributor
If you are using ArcGIS v10.1, I believe you need the full path to the connection file.

You have "Database Connections\guana_npwd@usprd06.sde",

where I have found at v10.1 you need something like:

c:\Users\Roaming\guana_npwd@usprd06.sde (or wherever your SDE connection file is found)
0 Kudos
AllenGuan1
Deactivated User
If you are using ArcGIS v10.1, I believe you need the full path to the connection file.

You have "Database Connections\guana_npwd@usprd06.sde",

where I have found at v10.1 you need something like:

c:\Users\Roaming\guana_npwd@usprd06.sde (or wherever your SDE connection file is found)


What I need is a simple function inside arcpy.mapping that can change a feature class name in mxd or layer file without having to touch the sde connection path or workspace path. I think arcpy.mapping totally misses this one. We can not use hundreds of sde connection files, each containing each user's sde credential, for batching processing. A single sde connection file only works for OS authentications, which we don't use.
0 Kudos
MichaelVolz
Esteemed Contributor
I would open a ticket with ESRI technical support at this point, as they may be able to point you to a simple function that change a feature class name in mxd or layer file besides the replaceDataSource method.
0 Kudos
AllenGuan1
Deactivated User
I would open a ticket with ESRI technical support at this point, as they may be able to point you to a simple function that change a feature class name in mxd or layer file besides the replaceDataSource method.


Yes, will ask them. Thanks.
0 Kudos
markdenil
Frequent Contributor
You should search the mxd to find the layer for which you want to change the source
use ListLayers()

The layer has a read only workspacePath property
which will be the connection file for an SDE layer

Use that workspacePath in the
replaceDataSource(workspace_path, workspace_type, {dataset_name}, {validate})
method on the same layer

This way, the existing layer tells you what SDE connection file is currently used
and you just re-use it with a new feature class in the same database.
0 Kudos