0

I have written a script that looks at layer (.lyr) files inside of multiple directories and successfully repoints their corresponding datasets to a new ArcSDE connection. When I test my script on a sub-directory containing a bunch of rasters, upon encountering arcpy.Exists(lyr.datasetName), my script will crash on random "File System Rasters," that is, rasters whose datasets are apparently stored in folders, not in ArcSDE/SQL Server. Since these datasets are not stored in ArcSDE, the arcpy.Exists() function should simply return a value of False and pass over these rasters. And it does successfully pass over most of them, but then it will crash on others and not necessarily the same dataset each time.

In order, I will receive the following messages:

enter image description here

enter image description here

After clicking through the messages, I see the following in my interpreter:

LiDAR Post-Sandy DEM, 1-m grid (USACE) KG Test.lyr
Layer is a group layer

lyr: 2012_PostSandy_NY_NJ_000248_1mGrid.tif
2012_PostSandy_NY_NJ_000248_1mGrid.tif
Traceback (most recent call last):
  File "<string>", line 73, in execInThread
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\netref.py", line 196, in __call__
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\netref.py", line 71, in syncreq
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\protocol.py", line 431, in sync_request
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\protocol.py", line 379, in serve
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\protocol.py", line 337, in _recv
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\channel.py", line 50, in recv
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\stream.py", line 166, in read
EOFError: [Errno 10054] An existing connection was forcibly closed by the remote host

Here is a Stack Exchange-friendly version of my code:

import arcpy, os

layersFolder = r'MY_LAYERS_PATH'
arcpy.env.workspace = r'MY_SDE_CONNECTION'
newSDEPath = arcpy.env.workspace

walk = arcpy.da.Walk(layersFolder, datatype="Layer")
for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        print filename
        path = os.path.join(dirpath, filename)
        layer = arcpy.mapping.Layer(path)

        layerList = []

        if layer.isGroupLayer:
            for sublayer in layer:
                layerList.append(sublayer)
        else: layerList.append(layer)

        for singleLayer in layerList:
            lyr = singleLayer
            print "lyr: {}".format(lyr)

            if lyr.supports("dataSource") and lyr.supports("datasetName"):
                print lyr.datasetName

                # This is where Python is crashing
                if arcpy.Exists(lyr.datasetName):

                    if lyr.isFeatureLayer == True:
                        print "Feature layer"

                    elif lyr.isRasterLayer == True:
                        print "Raster layer"

                    else:
                        print "PASSING LAYER: {}\n".format(lyr.name)
                        # break out of current loop
                        break

                    lyr.findAndReplaceWorkspacePath(lyr.workspacePath, newSDEPath, True)

                else:
                    print "PASSING LAYER: {}\n".format(lyr.name)

        layer.save()

print "Script Complete."

UPDATE: To get around this issue, I've added the following line of code:

if "sde.SDE" in lyr.dataSource:

before I use:

if arcpy.Exists(lyr.datasetName):

I think it's a good temporary workaround, but I'd still like to know why Python is crashing...

  • You say these rasters are in a folder, but you've set your arcpy.env.workspace to an SDE connection and your example output leads me to believe that the dataset name supplied to arcpy.Exists is just a name and not a full path. In that case, Arcpy would check to see if something with that name exists in the current workspace (which is an SDE connection in this case). Do the layers that work properly use full paths? Is there a reason you are setting arcpy.env.workspace to what it is? – Evil Genius Jan 11 '17 at 15:57
  • @EvilGenius I set 'arcpy.env.workspace' to the new SDE connection, where I want to check whether a dataset exists or not. This is where most of the datasets will actually be located. As I said, the script was working fine until I tested it on a directory w/ some raster layers whose datasets are in folders. I expected 'arcpy.Exists()' to return 'False', precisely because those datasets are not located in my workspace. And you're right about full paths, it's not a path, but a name. But that's how it's supposed to work, since I'm looking to see if a dataset name is in my 'arcpy.env.workspace'/SDE – Kristen G. Jan 11 '17 at 17:01
  • @EvilGenius I edited the question to improve clarity. Maybe this will help. – Kristen G. Jan 11 '17 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.