Select to view content in your preferred language

Filter script tool parameter to unique field values

1218
2
02-02-2023 01:53 PM
Labels (1)
GregKeith
Frequent Contributor

Howdy!

I'm writing a python tool to download data from a hosted feature layer in our Portal to a csv file. I've got it working when downloading all data, but I want to be able to filter the data based on the Site field. What I'd like to have is the Site parameter in the tool be a dropdown of the unique values in the Site field.

I can do this if I manually fill in a Value List for the parameter, but ideally it should read them from the layer. Even better, I could also use this to filter the gis.content.get call, but I don't see a filter option there. I can filter the results in a dataframe before exporting to csv if necessary, but still don't know how to populate the parameter list. Is this possible? Thanks!

2 Replies
Clubdebambos
Frequent Contributor

Hi @GregKeith 

Where are you making the tool, ArcGIS Pro, ArcMap or alternative? 

I have achieved this in ArcGIS Pro Custom Tool using the following in the Validation tab of the Tool Properties.

Get the url for the feature layer (not the feature service). This ends in a number specifying the layer number, generally 0 (zero) if only one layer.

Use set comprehension to return a set of all unique (change the FIELD_NAME to match yours).  This is wrapped in a list function because the filter must be a list.

Initialize the parameter, [0] here indicates the filter is the first parameter. This workflow assumes that the first parameter is a string and that the Site field is also a string.

    def initializeParameters(self):
        # Customize parameter properties. 
        # This gets called when the tool is opened.
        url = "url to the LAYER in the Feature Service"
        site_list = list({site[0] for site in arcpy.da.SearchCursor(url, "FIELD_NAME")})
        self.params[0].filter.list = site_list
        return

 

 

~ learn.finaldraftmapping.com
GregKeith
Frequent Contributor

Thanks Clubdebambos! I'm using ArcGIS Pro 2.9.5. I'll give this a shot. 

0 Kudos