1

Here is my code to replace the old SDE connection file with the new one. I am getting an error due to grouped layers, that I know. I have been trying to fix this but regardless of what I try I keep getting the ValueError: Layer: Unexpected error due to the grouped layer.

Can anyone let me know what I am doing wrong?

import arcpy
import os

for root, subFolders, files in os.walk(r"C:\Users\x\Desktop\lyrtest"):
    for filename in files:
        fullpath = os.path.join(root, filename)
        basename, extension = os.path.splitext(fullpath)
        if extension.lower() == ".mxd":
            print "------------------------------"
            print filename
            MXD = arcpy.mapping.MapDocument(fullpath)
            for lyr in arcpy.mapping.ListLayers(MXD):
                if not lyr.isGroupLayer:
                    if lyr.supports("DATASOURCE"):
                        source = lyr.workspacePath
                        print "%s -> %s" % (lyr, source)
                        basename, extension = os.path.splitext(source)
                        if extension.lower() == ".sde":
                            datapath = r"C:\Users\x\Desktop\lyrtest\xdbprod.sde"
                            lyr.findAndReplaceWorkspacePath(source, datapath)
                else:
                    print "exception"
            MXD.save()
            del MXD

Here is the error:

Traceback (most recent call last):
  File "C:\Users\x\Desktop\lyrtest\mxdchng5.py", line 20, in <module>
    lyr.findAndReplaceWorkspacePath(source, datapath)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\utils.py", line 182, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\_mapping.py", line 696, in findAndReplaceWorkspacePath
    return convertArcObjectToPythonObject(self._arc_object.findAndReplaceWorkspacePath(*gp_fixargs((find_workspace_path, replace_workspace_path, validate), True)))
ValueError: Layer: Unexpected error
  • 1
    please give the full error including a line number – Midavalo Jul 28 '16 at 23:09
  • 1
    what version of ArcGIS are you using? – Midavalo Jul 28 '16 at 23:10
  • I am using ArcGIS 10.3.1. So if I run this and the MXD does NOT have grouped layer files, I do not get an error. The full error report is too long for this comment section but the error occurs in line 20. – Strand83 Jul 28 '16 at 23:15
  • 1
    Please edit your question to include that information plus your error message. Paste your error message as text (not an image) – Midavalo Jul 28 '16 at 23:25
  • Please use the edit button beneath your question to revise it with any requested clarifications. – PolyGeo Jul 28 '16 at 23:27
1

The line if layer.supports("DATASOURCE") should get your passed any group layers - I don't have issues with group layers running the code below

import arcpy, os

workspace = r"D:\temp\SE\updatePath" # Change to match your MXD folder path
datapath = r"Database Connections\GISDatabase.sde" # Change to match your new SDE connection path

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, topdown=True):
    arcpy.env.workspace = dirpath
    mxds = arcpy.ListFiles('*.mxd')
    for filename in mxds:
        print "------------------------------"
        print filename
        mxd = arcpy.mapping.MapDocument(os.path.join(dirpath, filename))
        for layer in arcpy.mapping.ListLayers(mxd):
            if layer.supports("DATASOURCE"):
                print "{} -> {}".format(layer.name, layer.dataSource)
                if '.sde' in layer.dataSource:
                    layer.findAndReplaceWorkspacePath(layer.workspacePath, datapath)
        mxd.save()
  • Ok I'll see what happens with your suggestion, thank you! – Strand83 Jul 28 '16 at 23:59
  • Still get the same error :( – Strand83 Jul 29 '16 at 0:03
  • @Strand83 then I would suggest that Group Layers are not your issue. I've just re-run this on group layers containing group layers and it still works without error. You may need to re-check some of your data sources – Midavalo Jul 29 '16 at 0:04
  • Will do thanks for the tip. I have it running on multiple mxd's and it only has the error when there are grouped layers so I figured that was the issue. Guess I need to do some more spot checks. – Strand83 Jul 29 '16 at 0:08
  • 1
    For the time being I have thrown in a "if layer.isRasterLayer: print skip" and everything is running smoothly. Thank you again for the help! – Strand83 Jul 29 '16 at 1:18
1

According to http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/layer-class.htm

There are two ways of determining if a layer is a group layer. First, you can check to see if the layer supports the isGroupLayer property. Second, you can evaluate the longNameproperty. A layer's longName value will include the group name in addition to the layer name. For example, a layer named Layer1 in a group layer named Group1 will have a longNamevalue of Group1\Layer1. If the name value is equal to longName value, then the layer is not a group layer or the layer is not inside a group layer.

So I would try using longName value to see if this recognises that its a group layer on the line that checks group layer

Rather than

if not lyr.isGroupLayer:

Try

if lyr.name == lyr.longName:

EDIT:

I just realised this will probably just skip all layers inside the group layer. So not sure if this answer is helpful

  • Thanks for the tip, I will try this as a check to see if it will correctly ignore them. – Strand83 Jul 28 '16 at 23:40
  • Yeah I just tested it with your edit and it is skipping them. I'll keep looking into it, thank you for the help :) – Strand83 Jul 28 '16 at 23:58
0

Often the Unexpected Error results from bad arguments being passed into your .findAndReplaceWorkspacePath method . You can try running this snippet in your code before you get to the method:

argList = [source,datapath] argCnt = 0 for arg in argList: argCnt +=1 print '\t','\t',"Argument string %d is %s" %(argCnt,arg)

I've found many weird things in strings that I thought were fine just by running this. I hope that helps.

0

Try this:

findAndReplaceWorkspacePaths (find_workspace_path, replace_workspace_path, {validate})

validate would be False

i.e. lyr.findAndReplaceWorkspacePath(source, datapath, False)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.