Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

replaceDataSource in layer file (not mxd)

$
0
0
My world: Win7, ArcGIS 10.0 SP1, Python 2.6.5

I am attempting to update a group layer file with new data source locations (FGDB moved from one location to another location). I get no errors running this code, but the new file locations are not updated in the layer file. Most of the discussions in this forum (related to this issue) are related to using an mxd, but in my case, I want to update a layer file (*.lyr).
  • Have verified that the paths are correct (via print statements).
  • Have verified that i am updating feature classes and that they support the DATASOURCE property
  • Have also tried saving the .lyr file using "arcpy.mapping.Layer(inLayerFile).saveACopy("newfilename")"
  • I have tried findAndReplaceWorkspacePath with no success.
  • Have tried converting newPath variable to unicode - again no success.


Any help greatly appreciated!

Code:

# xModifyDataLocationsInlayerFiles.py

import sys, os
import arcpy

outFolder = r"C:\Projects\CoreData\GeoName"
newFolder = r"C:\projects\testmove\test2\GeoName"

inLayerFile = r"C:\projects\testmove\test2\GeoName\LayerFiles\AG10\BaseInfo.lyr"

for lyr in arcpy.mapping.Layer(inLayerFile):
  pout = lyr.isFeatureLayer
  if pout == True:
    if lyr.supports("DATASOURCE"):
      currWSPath = str(lyr.workspacePath)
      scol = str.upper(currWSPath).find('GEONAME')
      scol = scol + len('GEONAME') + 1
      newPath = newFolder + os.sep + currWSPath[scol:]
      print lyr.name + " | Path: " + newPath + " | DSN: " + lyr.datasetName
      lyr.replaceDataSource(unicode(newPath), 'FILEGDB_WORKSPACE', lyr.datasetName, True)
    else:
      print "Layer " + lyr.name + " does not support datasource changes"
  else:
    pass


Viewing all articles
Browse latest Browse all 2485

Trending Articles