5

I'm attempting to replace a datasource in an outline map frame using ArcPy. The rest of the script seems to run OK, but after the script has run the datasource the datasource of that layer shows an error. A section of my code is below:

for df in arcpy.mapping.ListDataFrames(mxd):
    if df.name == 'Overview' or df.name == 'Keyplan':
        df.scale = 500000
    for lyr in arcpy.mapping.ListLayers(mxd,"",df):
        if lyr.datasetName == 'blah':
            arcpy.mapping.RemoveLayer(df,lyr)
            lyr.replaceDataSource(r'\\directory','FILEGDB_WORKSPACE','FCName')
            arcpy.mapping.AddLayer(df,lyr,"TOP")

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
fullpath = os.path.join(fileDir,'ALTERED_' + fileName)
mxd.saveACopy(fullpath)
del mxd

I have had a look at http://forums.arcgis.com/threads/23803-issues-updating-data-source-via-arcpy.mapping?p=78411&viewfull=1 and used some of the tricks there, but I can't seem to make it work. Picture of layer datasource

before: enter image description here

and after enter image description here

You'll see it has the correct layer name, but no actual layer. Any ideas what could be causing this. I thought at one point relative paths, but apparently they do not matter when using UNC paths, as I am.

This appears to work when I do it in ArcGIS itself using the "CURRENT" map, so pretty sure it's not a pathname error.

Any ideas?

2

This seemed to be an issue with relative paths. The mxd that I was trying to update was stored as Relative Paths in Document Properties. Once this was unchecked, the script worked as expected (without the delay).

So you can check each mxd manually, but given that this is a script, that slightly defeats the purpose. Unfortunately though ArcPy doesn't expose an API to change this particular setting (at least in 10.0).

So I have created a python script that uses ArcObjects to do it, with a lot of help from other people on the forum and outside. It needs an external library installed, but it is conceptually very simple. It can be found at https://gis.stackexchange.com/a/40666/217

There may also have been an issue with long path names - if you are still having problems try copying to a local directory (I was on a network) and running the script from there. I found I then had to re-run the ArcObjects script to change the relative path setting, because mysteriously the ReplaceDataSource() method turned this on in the copied MXD. This method has got really strange behaviour, and from what I've seen, it doesn't seem to be fixed in 10.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.