0

I need a 2nd set of eyes to do a sanity check, as I am not been able to figure out why a script that runs fine in pywin, does not run properly as a tool in a toolbox. Below is the script that runs in pywin without an issue. It walks through a tree of folders and adds any shapefiles to an empty existing mxd (The mxd is not open in arcmap). Each shape is added with the coordsys name append to the layer name.

    import arcpy, os

    # Set the source path
    srcPath = r'C:\shpfiles'

    # get the map document
    mxd = arcpy.mapping.MapDocument(r'C\TEST.mxd')

    # get the data frame
    dataFrame = arcpy.mapping.ListDataFrames(mxd)[0]

    dirList = os.walk(srcPath)

    try:
        for root, dirs, files in dirList:
            for dir in dirs:
                #Set the path to the shapefile folder...
                path = os.path.join(root,dir)
                #Set the workspace
                arcpy.env.workspace = path
                #Get a list of shapefiles in each dir...
                featureclasses = arcpy.ListFeatureClasses()
                #Cycle through the list of shpfiles...
                for shp in featureclasses:
                    shpPath = os.path.join(path,shp)
                    coordsys = arcpy.Describe(shp).spatialReference
                    #Create a new layer name      
                    newLayer = shp[:-4] + "_" + coordsys.name
                    #add a new layer in the map document
                    layer = arcpy.mapping.Layer(shpPath)
                    #add the layer to the map at the bottom of the TOC
                    arcpy.mapping.AddLayer(dataFrame, layer, "BOTTOM")
                    layer.name = newLayer
                    del layer, newLayer, coordsys
    except:
        print arcpy.GetMessages(2)

    #save the mxd file
    mxd.save()
    del mxd

This works perfectly. The script loops through and loads all the shape files into TEST.mxd, with their projection name appended to the layer name.

Below is the same code, modified to run as a tool in toolbox.

    import arcpy, os

    # Set the source path
    srcPath = arcpy.GetParameterAsText(0)

    # get the map document
    mxd = arcpy.mapping.MapDocument(arcpy.GetParameterAsText(1))

    # Set the workspace
    arcpy.env.workspace = srcPath

    # get the data frame
    dataFrame = arcpy.mapping.ListDataFrames(mxd)

    dirList = os.walk(srcPath)

    try:
        for root, dirs, files in dirList:
            for dir in dirs:
                #Set the path to the shapefile...
                path = os.path.join(root,dir)
                #Set the workspace
                arcpy.env.workspace = path
                #Get a list of shapefiles in each dir...
                featureclasses = arcpy.ListFeatureClasses()
                #Cycle through the list of shpfiles...
                for shp in featureclasses:
                    shpPath = os.path.join(path,shp)
                    coordsys = arcpy.Describe(shp).spatialReference           
                    newLayer = shp[:-4] + "_" + coordsys.name
                    #add a new layer in the map document
                    layer = arcpy.mapping.Layer(shpPath)
                    print arcpy.AddMessage(layer)
                    #add the layer to the map at the bottom of the TOC
                    arcpy.mapping.AddLayer(dataFrame, layer, "BOTTOM")
                    layer.name = newLayer
                    del layer, newLayer
    except:
        print arcpy.GetMessages(2)

    #save the mxd file
    mxd.save()
    del mxd

Running this tool in ArcCatalog does not do anything; none of the shapefiles are loaded into the mxd. Only one of the shapefiles is looped through and the entire process finishes in < 1sec, and TEST.mxd is empty.

Adding print statements and reming out the code, the problem is isolated to:

arcpy.mapping.AddLayer(dataFrame, layer, "BOTTOM")

When this line of code is commented out, the tool script loops through and prints out every shapefile it encounters in the tree. Why would AddLayer stop the loop?

Am I missing some extra code to enable this to run as an arcToolbox tool script?

1

Your first script is getting the first Data Frame in a data frame list:

dataFrame = arcpy.mapping.ListDataFrames(mxd)[0]

Your second script is getting a list of Data Frames:

dataFrame = arcpy.mapping.ListDataFrames(mxd)

Which causes your AddLayer to bomb out.

  • I added the index to the 2nd script, same problem. It did not change the result. – John Mar 5 '14 at 17:53
  • I think getting the file name as a text parameter may be causing the issue. Have you tried setting the parameters for the script via arcpy.Parameter? resources.arcgis.com/en/help/main/10.1/index.html#//… – evv_gis Mar 5 '14 at 18:06
  • Now I am confused. When I imported the script into my toolbox, the parameters were defined in the Add Script dialog. I am checking the 10.0 version of the link you gave. – John Mar 5 '14 at 19:51

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.