Select to view content in your preferred language

Toolbox Python Script Dropdown Contains Duplicates

1010
2
02-12-2024 12:22 PM
survey123_solutions
Regular Contributor

Hi. I'm using ArcGIS 3.2.
I added a new python script to the Toolbox that was created by Default in my new project.  

Quick silent video of both scripts at work
https://www.loom.com/share/9e7b72ee9a3448d18994a255ee68d91a?sid=cf199512-b146-49b7-b012-bf4eb819f333

 

I'd like for user to select from a dropdown, and have report generated.
Currently both scripts work for the purpose of generating reports, but 
1- One script has user entering event_id manually.
2- Second script has the dropdown, but duplicates appear.  I've tried everything only to still have the duplicates appear.  

Any help is welcome and appreciated. I am open to your recommendations. 
I only need one script, sharing both to share my history and what brought me here.

 

0 Kudos
2 Replies
PedroCoutinhoMendonça
Occasional Contributor

Hello, you can try to use a set in your list to remove duplicated values. Line 13:

 

    def initializeParameters(self):
        """Initialize parameters."""
        # Retrieve unique event IDs from the hosted feature layer
        aprx = arcpy.mp.ArcGISProject("CURRENT")
        lyr = aprx.listMaps()[0].listLayers()[0]  # Assuming the hosted feature layer is the first layer in the map
        event_ids = set()
        with arcpy.da.SearchCursor(lyr, "event_id") as cursor:
            for row in cursor:
                event_ids.add(row[0])
        
        # Populate the dropdown parameter with unique event IDs from the hosted feature layer
        sel_event_id = self.params[0]
        sel_event_id.filter.list = sorted(list(set(event_ids))) # Set Function Added

        # 2-9-24 THIS SECTION IS NOT BEING EXECUTED ALTHOUGH SCRIPT WORKING OK Print all event IDs retrieved from the hosted feature layer
        for event_id in event_ids:
            arcpy.AddMessage("Retrieved Event ID: {}".format(event_id))

        # Print the datasource for the hosted feature layer
        arcpy.AddMessage("Connected Hosted Feature Layer: {}".format(lyr.dataSource))

        # Print the datasource for the related table (tbl)
        tbl = aprx.listMaps()[0].listTables()[0]  # Assuming the related table is the first table in the map
        arcpy.AddMessage("Connected Related Table: {}".format(tbl.dataSource))
        return

 

0 Kudos
AlexanderDanielPratama
Esri Contributor

Hi, I guess you need to create a set first after you create the list. Then, you convert it to list

I tried to minimize the chaneg of your code

def initializeParameters(self):
        """Initialize parameters."""
        try:
            # Define REST URLs for the main layer and related table
            main_layer_url = "https://services8.arcgis.com/CQeG4yN4mcOJaKOV/arcgis/rest/services/service_b4b3e49de39547628b85a73989096909/FeatureServer/0"
            related_table_url = "https://services8.arcgis.com/CQeG4yN4mcOJaKOV/arcgis/rest/services/service_b4b3e49de39547628b85a73989096909/FeatureServer/1"

            # Set the selected event ID parameter to populate dropdown
            sel_event_id  = [row[0] for row in arcpy.da.SearchCursor(main_layer_url, "event_id")]
            create_unique_sel_even_id = set(sel_event_id)
            self.params[0] = list(create_unique_sel_even_id)

            # Print all event IDs retrieved from the hosted feature layer
            event_ids = [row[0] for row in arcpy.da.SearchCursor(main_layer_url, "event_id")]
            for event_id in event_ids:
                arcpy.AddMessage("Retrieved Event ID: {}".format(event_id))

            # Print the datasource for the hosted feature layer
            lyr = arcpy.MakeFeatureLayer_management(main_layer_url, "Main_Layer")[0]
            arcpy.AddMessage("Connected Hosted Feature Layer: {}".format(lyr.dataSource))

            # Print the datasource for the related table
            tbl = arcpy.MakeTableView_management(related_table_url, "Related_Table")[0]
            arcpy.AddMessage("Connected Related Table: {}".format(tbl.dataSource))

        except Exception as e:
            arcpy.AddError("An error occurred during parameter initialization: {}".format(str(e)))

        return

 

 

 

 

0 Kudos