Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

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 http://gis.stackexchange.com/a/40666/217

--EDIT

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

share|improve this answer
    
It looks like you have provided the Answer to your own Question so do not be afraid to hit the Tick on it so that it comes of the Unanswered list. –  PolyGeo Nov 22 '12 at 7:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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