0

I'm trying to batch export selected layers that I drag and drop in the tool that I've created by a selecting feature class. It's working when features are input from the source and layers in TOC. However, you get an error when the layers are in group layers. "Parameters are not valid. ERROR 000732: Input Features: Dataset 'Topography layers\Linear' does not exist or is not supported"

enter image description here

Here's the code:

import arcpy, os, sys

try:
        arcpy.env.overwriteOutput = True

        def selectExport (Layer, LyrSelected, outName):

                for fc in inFeatures:
                        Layer = arcpy.MakeFeatureLayer_management(fc, "outlayer")
                        LyrSelected = arcpy.SelectLayerByLocation_management(Layer,"INTERSECT",SelectLyr,"","NEW_SELECTION")
                        result = int(arcpy.GetCount_management(LyrSelected).getOutput(0)) 
                        if result > 0:
                                if fc.endswith(".shp"):
                                        bName= fc.split(".")[0]
                                        outName = os.path.join(outPath, os.path.basename(bName))
                                        arcpy.CopyFeatures_management(LyrSelected, outName)
                                else:
                                        outName = os.path.join(outPath, os.path.basename(fc))
                                        arcpy.CopyFeatures_management(LyrSelected, outName)
                                arcpy.AddMessage("*******Geoprocessing done successfully!*******")
                        else:
                                arcpy.AddMessage("*******No features were selected*******")

except:
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n     " +        str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
        msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"

        arcpy.AddError(msgs)   
        arcpy.AddError(pymsg)

        print msgs
        print pymsg

        arcpy.AddMessage(arcpy.GetMessages(1))

        print arcpy.GetMessages(1)

if __name__ == '__main__':

    outPath = ""
        arcpy.env.workspace = outPath
        inFeatures = arcpy.GetParameterAsText(0).split(";")
        SelectLyr = arcpy.GetParameterAsText(1)
        outPath = arcpy.GetParameterAsText(2)

        selectExport (inFeatures, SelectLyr, outPath)
  • I'd say test the input first, e.g. split it by "\' and if length of output ==2, it is most likely sitting in the group. Alternatively just move them outside the group Ctrl-Drag helps to create a clone quickly – FelixIP Jun 1 '16 at 2:54
  • @FelixIP thanks. But there are a lot of already set up map templates with layers and sublayers that we use. What I'm after is how I can deal with group and sub layers in the script, 'cos there might be a lot layers involved so I don't want to do that manually. – Matt Jun 1 '16 at 3:03
  • If no subgroups in your groups than see 1st part of my comment – FelixIP Jun 1 '16 at 3:28
  • It's because of the space in your group layer name, change to Topography_layers (or similar) and it should work... spaces and punctuation are bad in names and paths and you should avoid using them - or suffer the consequences that come back to bite you when you least expect it. That is of course if Linear is a feature layer and not another group layer. – Michael Stimson Jun 1 '16 at 3:32
  • @MichaelMiles-Stimson Thanks, that's right, when removing the space it'll work. – Matt Jun 1 '16 at 5:25
4

As @MichaelMiles-Stimson has mentioned, you can get around this by removing the space in the group layer name.

However as you have mentioned that you have several MXDs and layers already set up, it is a relatively small change to your code (which doesn't seem to touch the space) that will get it working.

The fc from inFeatures is passing a string like "'Topography Layers\\Linear'" (note the extra single quotes ' in there!) - if you strip out this extra set of single quotes the MakeFeatureLayer tool works:

for fcString in inFeatures:
    fc = fcString.strip("'") # Strip out the single quotes
    Layer = arcpy.MakeFeatureLayer_management(fc, "outlayer")

Oddly, removing the space from the Group Layer name will also overcome this (without the need to remove the single quotes).


Alternatively (and possibly the better or more pythonic solution) is to use arcpy.GetParameter() instead of arcpy.GetParameterAsText(), which passes the layer objects rather than a text string (and then a list via your split).

inFeatures = arcpy.GetParameter(0)

for fc in inFeatures:
    Layer = arcpy.MakeFeatureLayer_management(fc, "outlayer")

This way it doesn't matter about single quotes or spaces in your layer/group layer names.

  • Awesome, it's now working with the sub layers as well. Thank you. – Matt Jun 1 '16 at 5:20

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.