2

I have a Python script tool where I have a parameter of type Feature Layer because I want to have the already opened layers as a dropdown list.

I was wondering if there is a way to filter this Feature Layer parameter dropdown list in order to show Polygon Shapefiles only?

10
  • Did you search for similar problems on the forum? This seems like an answer to your question: gis.stackexchange.com/questions/67473/…
    – dru87
    Mar 4, 2016 at 9:07
  • 1
    The possible duplicate show how to handle the type of features shown on the dropdown list. I want to filter the list just for the shapefiles to be visible as well.
    – Panos
    Mar 4, 2016 at 9:14
  • 1
    Can you include a code snippet that illustrates how you are receiving this parameter (and perhaps printing its value), and then a picture of its tool properties showing how you have this parameter being configured, please? If you are already using any tool validation code then including that as text would be useful too.
    – PolyGeo
    Mar 4, 2016 at 9:52
  • I have the answer with the code ready to post. Mind opening the question? Mar 4, 2016 at 10:00
  • @PolyGeo, this question does not require a code snippet to understand and answer, for someone familiar with the task at hand. This is evidenced by the fact that someone already has an answer to give. Please open the question back up.
    – RHB
    Mar 4, 2016 at 12:33

1 Answer 1

3

You should use the ToolValidator class for that.

  1. Create a parameter of type String in script tool. Set the Filter to be the Value List. Leave the list empty.

  2. Edit the script tool Python file.

import arcpy
in_layer_name = arcpy.GetParameterAsText(0)
in_layer = arcpy.MakeFeatureLayer_management(in_layer_name,"out_fl")
arcpy.CopyFeatures_management(in_layer,"OUT_TEST")
  1. Edit the ToolValidator class' initializeParameters.
def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""
    mxd = arcpy.mapping.MapDocument("CURRENT")
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    lyrs = [lyr for lyr in arcpy.mapping.ListLayers(mxd, "",df)
            if lyr.dataSource[-4:] == ".shp" and
            arcpy.Describe(lyr.dataSource).shapeType == "Polygon"]

    self.params[0].filter.list = [lyr.longName for lyr in lyrs]
    return

Now you will get the list of layer names (as strings) in the drop-down list of the parameter. You just need to make those feature layers (which is done on the 3rd line in the script tool file above).

3
  • Thanks. I will implement it and let you know if it works as I am expecting. :-)
    – Panos
    Mar 18, 2016 at 9:30
  • I implemented it like this: mxd = arcpy.mapping.MapDocument("CURRENT") lyrs = [lyr for lyr in arcpy.mapping.ListLayers(mxd) if lyr.supports("DATASOURCE") and lyr.dataSource[-4:] == ".shp" and arcpy.Describe(lyr.dataSource).shapeType == "Polygon"] self.params[1].filter.list = [lyr.longName for lyr in lyrs] but the result is not what I want. The String hides the icon on the right of the parameter, so the user can choose sth outside the mxd. Is there a way to get the Feature Layer list in ToolValidator, filter it and put it back?
    – Panos
    Mar 18, 2016 at 10:34
  • Please ask a new question about that Mar 18, 2016 at 11:56

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.