montaguerichard

Two problems with using layer.replaceDataSource.

Discussion created by montaguerichard on Aug 30, 2013
Latest reply on Sep 4, 2013 by montaguerichard
We have a little over 300 layers in 275 or so .lyr files.  These files have been created over the course of years and have many different workspace paths.  We are replacing our GIS server so I need to change the connection file for all of the layers.

The first and most important is the datasource is not changed.  Maybe I am too close to it but I have tried many different ways to write this but I can't seem to get the layer file to change datasources.

The second and not so critical problem is memory usage.  The directory has a little over 300 layers, some of which are in group layers.  As the program progresses, the memory usage keeps building until PyScripter crashes.  I get the same results if I run the script in the python window in ArcCatalog.  If I run a subset of the files the memory usages builds and isn't released until I close out PyScripter or ArcCatalog.  In a command window it uses up all of the available memory after around 60 layers.

Below is the code I am using.  Thanks for any help I can get.


import arcpy
import os
from arcpy import env

env.overwriteOutput = True

TheLayerDir = 'J:\\ArcMap Layers\\dbx\\'
for dirname, dirnames, filenames in os.walk(TheLayerDir):
    [INDENT]for filename in filenames:
        [INDENT]newFilename =  os.path.join(dirname, filename)
        if newFilename[-4:] == '.lyr':
            [INDENT]lyrFile = arcpy.mapping.Layer(newFilename)
            for lyr in arcpy.mapping.ListLayers(lyrFile):
               [INDENT] if lyr.supports("SERVICEPROPERTIES"):
                    [INDENT]if not lyr.isGroupLayer:
                        [INDENT]servProp = lyr.serviceProperties
                        NewDataSource = 'J:\\SDEConnections\\dbx\\GISuser-' + servProp.get('Database', 'N/A') + '.sde'
                        try:
      [INDENT] lyr.replaceDataSource(NewDataSource,"SDE_WORKSPACE")[/INDENT]
                        except:
                           [INDENT] print "This is where it breaks: " + lyr.name
                            print "The data source is: " + NewDataSource
                            print arcpy.GetMessages(2)[/INDENT][/INDENT][/INDENT][/INDENT][/INDENT][/INDENT]
            lyrFile.save[/INDENT]
print "That's all folks"

Outcomes