1

I'm dealing with several layer files that have out-of-date sources; the actual layers refered to in the .lyr file started in L: and have since been copied to W:/FOR/RSI/DKL/Local_Data .

I'm trying to update the .lyr files (in Python 2.7) to point to the new source layers. It should be straightforward, as each .lyr file has a single source, so I'm not (yet) dealing with group layers or anything like that.

My output (see below) tells me that the lyr data source is updated, but checking it in ArcCatalog, I see this is not true.

How do I actually update the dataSource in the layer file?

It seems to me I am simply changing a property of the lyr object.

I have a feeling that my arguments are maybe not quite correct in this line:

lyr.replaceDataSource (newLayerString,"SHAPEFILE_WORKSPACE", layerName)

Code:

for layerFile in glob.glob(layerFilePath + "/*.lyr"):
 print layerFile
 lyr = arcpy.mapping.Layer(layerFile) # returns the object from each 
layer file
 layers = arcpy.mapping.ListLayers(lyr) # returns the list of objects 
(i.e. the actual layers) referenced by the layer file
 for layer in layers:
 if layer.supports("dataSource"): # some layers might not support the property "dataSource"
 layerString = str(layer.workspacePath)
 layerName = layer.datasetName
 print '\t',"Layer ToC name is %s and workspace path is %s" %(layer.name, layer.workspacePath)
 if layer.dataSource[-4:] == ".shp":
 workspaceType = "SHAPEFILE_WORKSPACE" #add in other possibilities here for coverage sources, SDE feature classes etc..
 if 'L:' in layerString: #this is a better way to select part of a string than an element search i.e.
 newLayerString = layerString.replace(r"L:",r"W:/FOR/RSI/DKL/Local_Data",1)
 lyr.replaceDataSource (newLayerString,"SHAPEFILE_WORKSPACE", layerName)
 print '\t',"The updated source for %s is %s" %(layer.name,lyr.dataSource)
 del layer, layers #get rid of these objects
 print""
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked May 12, 2017 at 20:27
1
  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. Commented May 13, 2017 at 2:13

1 Answer 1

2

Your code is missing:

lyr.save()

which is needed to write the change you made back to the layer file.

answered May 12, 2017 at 20:44
0

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.