2

This is related to my question ArcPy replaceDataSource, but can anyone explain to me why the following code fails to add a valid layer from a .lyr file, and I get the same problems as in that question (broken data source):

import time
import arcpy
fullpath = r'\\mxdPath'
mxd = arcpy.mapping.MapDocument(fullpath) 
for df in arcpy.mapping.ListDataFrames(mxd):
 if df.name == 'Overview' or df.name == 'Keyplan':
 temp = r'C:\TMP\xxx.lyr'
 lyrNew = arcpy.mapping.Layer(temp)
 arcpy.mapping.AddLayer(data_frame,lyrNew,"TOP")
time.sleep(15) 
mxd.save()
del mxd

I added in the delay because I wondered whether the layer wasn't fully added before the MXD was saved, but this doesn't seem to work consistently.

My geodatabases do seem to take a long time to open, but it's crazy not to be able to undertake such a simple scripting operation. I guess the same problem is affecting both operations, but any ideas why?

asked Nov 8, 2012 at 10:33
2
  • I don't think the delay is needed. As a test have you tried changing the UNC path to a drive mapped locally to see if that enables your code to work? Commented Nov 9, 2012 at 3:23
  • thanks @PolyGeo. It seems to be an issue with relative paths, as per answer Commented Nov 9, 2012 at 9:20

1 Answer 1

1

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

answered Nov 9, 2012 at 9:25
1
  • It would be good if this information were added to the excellent Paths Explained document and if ArcPy could be used to unset Relative Paths. Commented Nov 9, 2012 at 22:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.