Select to view content in your preferred language

Arcpy: replaceDataSource (from gdb to SDE w/ change in data source name)

6872
13
07-31-2015 03:13 PM
CrystalSchiffbauer-Bowles1
Regular Contributor

Hello all. We have a large task of replacing datasources for MANY feature classes and rasters (we received data from a consultant, and in order to place the data on the Client's enterprise server, all data has to conform to schema requirements). I have saved all of the data in their respective receptacles on the enterprise SDE environment. Now, I need to change the datasources within each of the mxds - this means every feature class in every mxd has to be relinked to the new name and location on the SDE. I want to do this with Python - naturally, otherwise task is beyond tedious.

So, I have my list of broken links, I have a table with name and location crosswalk for reference. I am not at all a Python guru and could really use some help in figuring out what the error below means (I feel like it has something to do with the enterprise SDE connection properties, but honestly don't know, especially since it is referencing my storage properties):

I have my broken list already, so am referencing that:

>>> for lyr in brokenlist:

...     if lyr.supports ("DATASOURCE"):

...         if lyr.dataSource == "Y:\00726_11_BDCP\data\DHCCP_ENG_Rev10b\DHCCP_Eng_Rev10b.gdb\Option_Point_Features":

...             lyr.replaceDataSource ("Y:\00726_11_BDCP\data\DHCCP_ENG_Rev10b\DHCCP_Eng_Rev10b.gdb\Option_Point_Features", "SDE_WORKSPACE", "C:\Users\cbowles\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\dhccp_eng.SDE.ER10b_Option_Point_Features")

... mapdoc.save ()

... del mapdoc

Runtime error

Traceback (most recent call last):

  File "<string>", line 5, in <module>

  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_

    return fn(*args, **kw)

  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\_mapping.py", line 801, in save

    return convertArcObjectToPythonObject(self._arc_object.save(*gp_fixargs((), True)))

IOError: MapDocObject: Unable to save.  Check to make sure you have write access to the specified file and that there is enough space on the storage device to hold your document.

I appreciate all helpful comments.

Thanks,

Crystal

Tags (2)
0 Kudos
13 Replies
CrystalSchiffbauer-Bowles1
Regular Contributor

Hello Michael. I have run some updated code. The first 3 layers were updated however, everything else after only had the lyr.name within TOC updated, where the actual properties of the data still shows the original data source. I am not sure why. I tried just updating single layers at a time, and I got the same result. I find it strange that their are no syntax errors and that the datasource is correct (since it's changing the name in the TOC), but not actually updating the properties. Any comments/ideas are much appreciated. Thank you, Crystal Bowles

Example of portion of code attached:

0 Kudos
CrystalSchiffbauer-Bowles1
Regular Contributor

UPDATE:

So, there apparently is known (though not-well documents) issues with arcpy and the replaceDataSource from one workspace to an SDE Workspace. ESRI customer support is trying to figure out the cause and perhaps answer. So... this issue has not been solved for replacing all Data Sources within to start with, a single mxd. I have been able to get a single feature class to update, sometimes, until it stops working... weird issues.

Anyway, here is the code that does work... sometimes:

>>> #import arcpython module
>>> import arcpy
>>> #define mapdocument
>>> mxd = arcpy.mapping.MapDocument (r"CURRENT")

>>> #ListLayers function to update DataSource

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

...     if lyr.supports ("DATASOURCE"):

...         if lyr.dataSource == r"full fc file path":

...             lyr.replaceDataSource (r"Database Connections\connection name.sde", "SDE_WORKSPACE", "name.sde.SDE.newFCname")

...             lyr.name = "FC name in TOC"

... del mxd

So, good luck and happy GIS-ing.

0 Kudos
CrystalSchiffbauer-Bowles1
Regular Contributor

Happy Holidays all! So, this issue has not been resolved. We have a programmer working on the process - utilizing other code to see if it will work. We have yet to get any resolution. ESRI did put some effort into finding a solution, but to no avail. So... we are waiting to see what our programmer comes up with - to be honest, I have little hope it will work, as I have gone through and manually updated the data sources for more than 400 map documents since Sept. Sorry for not having a solution, but wanted to give an update anyway.

Happy Christmas and New Year!

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

If you programmer hasn't looked at this already, you might want them to see how I did it.  Not perfect and I'll be making additional updates at some point (off that project right now).

Python addin for data inventory and “broken-link” repair.

0 Kudos