Select to view content in your preferred language

replaceDataSource connecting to SDE Instance

6931
11
02-01-2012 04:15 AM
ChristinaGnadinger
Emerging Contributor
Good morning! I am trying to create a re-pathing script in python but am running into a few kinks. I am hoping that someone can offer some assistance 🙂


What I have right now is ...

import arcpy

mxd = arcpy.mapping.MapDocument("C:\TEMP\TEST.mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.dataSource == "Database Connections\\SDEConnection.sde\PTD.AIRPORTS":
            lyr.replaceDataSource("Database Connections\\SDEConnection.sde\PTD.ap", "SDE_WORKSPACE", "Airports")
mxd.saveACopy(r"C:\TEMP\NEWTEMP.mxd")


I've tried multiple versions in here with varying levels of success. Some don't change anything and just save a new MXD with a broken link. This one is actually changing the path but it's changing it to
C:\Users\NAME\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\SDEConnection.sde\PTD.ap

Close .. but I'm trying to connect to our ArcGIS Server instance and NOT a local copy on my machine.

Ultimately I am wanting the script to run through a list of layers so it can update multiple ones within the same MXD, all who will have new feature class names, which is why I am using the if lyr.dataSource = section...

Does anyone know what I'm missing? I've been reading through the online documentation but it is very vague when it comes to replaceDataSource ..

Thank you!
0 Kudos
11 Replies
ChristinaGnadinger
Emerging Contributor
Thanks for the suggestion!!! I am going to try to look into this today. I've been detoured temporarily. I'll have to look at the comparison in Python between SDE feature class and datasetname .. I'm still learning some of the conventions within Python.

I guess once I get the script up and running I may survey the users to see how many scripts they have that will need altering. I haven't actually gotten that far down the list, however if it's a memory issue then I may just keep it simple.

I also tested doing this imported as a model and did not have any better success in that route. It seems like I always want to write the scripts that have one impossible hang up ...



Hi Christina:

In your python script, maybe you can use the datasetname to get the full name of the SDE feature class so it differentiates between BULLITT.bg and PTD.bg.  You can then use If Then statements to make sure it gets resourced to the correct data.

In addition, do you plan on running this script on many mxds at once in a loop?  I have been writing similar python scripts where I resource SDE data and it appears there is a memory leak as the memory usage for the script keeps going up until it locks up the computer.  I am currently working with ESRI Technical Support to resolve this particular issue.

Please let me know if you need any clarification or additional information from my response.  Thank you.
0 Kudos
ChristinaGnadinger
Emerging Contributor
This is what I was trying the last time I worked on it, but this did not work.

When you mention using If Then are you thinking that it should be before or after the replace data? I was working on the hopes that if it mis-assigned it to BULLITT.cn in the replacedatasource I could change it afterwards, which did not work. I figured it was a long shot but worth trying!


import arcpy, os
from arcpy import env
from arcpy import mapping
env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument("C:\TEMP\TEST.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):

      if lyr.datasetName == "PTD.CONTOURS_JEFF":
            lyr.replaceDataSource("Database Connections\\GIS_LOJIC.sde", "SDE_WORKSPACE", "PTD.cn")
            if "BULLITT" in lyr.name:
                        lyr.name = "PTD" + lyr.name.split(".")[2]

           
mxd.saveACopy(r"C:\TEMP\NEWTEST.mxd")

0 Kudos