Pass user selection to custom python toolbox

4810
5
Jump to solution
02-23-2016 07:31 AM
KateJohnson
New Contributor III

Hi folks, I posted a similar question back in October that has remained unanswered, and have seen this question before on Stack Exchange (arcpy - How to pass only selected features in python toolbox - Geographic Information Systems Stack ...) but found that the answer did not work. I've created a toolbox that will allow a user to select certain polygon features (in this case, grid squares from a LiDAR survey). The selection *should* then be then used to get the selected .LAS files (based on the name, extracted from the polygon feature) from a specified folder, and create an LAS Dataset. I have found that despite my best efforts, the script still does not recognize the user selection and tries to put ALL of the features in the polygon feature class into the LAS Dataset.

From what I've read, if a user has selected features from a feature class, a Search Cursor should just recognize that and will run on solely those features that were selected. For some reason this does not seem to be working for me. Any help appreciated. Also for what it's worth, I've tried doing the syntax highlighting for Python about 5 times and it refuses to post with it, so I apologize for that. Not sure what the deal is.

try:

    importarcpy, sys, traceback, os.path

    fromarcpy.sa import*

    arcpy.env.overwriteOutput =1

    arcpy.env.workspace =r"C:\Users\Kate\GIS"

    # NEED TO TURN THIS INTO A TOOLBOX SO THAT THE SELECTION IS HONORED.

    # User define input SHP (LASGRID FILE)

    LASGridU =sys.argv[1]

    # User define location of LAS tiles to be used

    LASLocationU =sys.argv[2]

    # User define output location of output LAS dataset

    LASDname =sys.argv[3]

    # User define spatial reference (not optional)

    spatialRefU =sys.argv[4]

   

    count =0

    # Create new feature class from selected features in LASGrid (apparently only way)

    #arcpy.CopyFeatures_management(LASGridU,selGrid)

    # Create the search cursor

    sCursor =arcpy.SearchCursor(LASGridU)

    # Create list to hold names of selected LAS files

    LASList =[]

    finalLAS =[]

    forrow insCursor:

        LASList.append(str(row.getValue("LAS_File")))

        count +=1

    forlas inLASList:

        finalLAS.append(os.path.join(LASLocationU+"/"+las))

    arcpy.AddMessage(LASList)

    arcpy.AddMessage(finalLAS)

       

    # create LAS Dataset with the selected features in the index

    arcpy.CreateLasDataset_management(finalLAS, LASDname,"","",spatialRefU,"COMPUTE_STATS","RELATIVE_PATHS")

    printcount

    delrow, sCursor

except:

    tb =sys.exc_info()[2]

    tbinfo =traceback.format_tb(tb)[0]

    pymsg =tbinfo +"\n"+str(sys.exc_type)+": "+str(sys.exc_value)

    arcpy.AddError(pymsg)

    arcpy.AddError(arcpy.GetMessages(2))

1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

from the help topic

Copy Features—Help | ArcGIS for Desktop

Copies features from the input feature class or layer to a new feature class. If the input is a layer which has a selection, only the selected features will be copied. If the input is a geodatabase feature class or shapefile, all features will be copied.

You need to set your parameter for the layer with the selection to a feature layer not a feature class.  If you are selecting the layer (name) from disk rather than the layer in arcmap, then the selection will be ignored.  If you ensure that the parameter is a featurelayer, then files on disk won't appear since they aren't in arcmap

View solution in original post

0 Kudos
5 Replies
FreddieGibson
Occasional Contributor III

Can you show how you were creating the cursor when you were trying to use the selection? I'm curious if you were using the layer object in the map or the path to the data on disk. The SearchCursor should respect the selection in the layer object. I would think that you'd be able to use a tool validator to parse the needed information and pass it to your tool.

KateJohnson
New Contributor III

Freddie, the cursor is called on the user-inputted polygon feature class, which would have features selected. This is the variable is "LASGridU" which as you can see the SearchCursor is called on.

I have tried selecting features, and then running the script in the toolbox, but it doesn't work.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Selections are honored, if the layer is in arcmap.  If the layer isn't in arcmap, you have to create a new set of data...perhaps in memory, or on disk, and use it.  The easiest approach is to design tools to function within arcmap, using layers so you can make selections etc.  Python toolboxes are not necessary and conventional toolboxes and tools are the simplest to design and implement

0 Kudos
KateJohnson
New Contributor III

I have the layer open in ArcMap. I then select features from it, and open the toolbox. I input the layer that has selected features "LASGridU" and run the script, but it does not honor the selection.

It is actually just a regular Toolbox, but I have added a script written in Python to it. Sorry for the misnomer.

0 Kudos
DanPatterson_Retired
MVP Emeritus

from the help topic

Copy Features—Help | ArcGIS for Desktop

Copies features from the input feature class or layer to a new feature class. If the input is a layer which has a selection, only the selected features will be copied. If the input is a geodatabase feature class or shapefile, all features will be copied.

You need to set your parameter for the layer with the selection to a feature layer not a feature class.  If you are selecting the layer (name) from disk rather than the layer in arcmap, then the selection will be ignored.  If you ensure that the parameter is a featurelayer, then files on disk won't appear since they aren't in arcmap

0 Kudos