I am trying to write an ArcPy script tool to simplify the spatial join tool for users. In particular, I want to:

1) Let user choose the target feature class.

2) Let user choose the output feature class name and location.

3) BUT force the users to choose between two particular feature classes (Districts and Countries) as the join feature class, which are stored in the workspace/folder that I would be sharing with the users.

Is number 3 possible? If so, how? I'm envisaging a dropdown list in the script tool interface, and they choose one of the two feature classes as the input. I have found some help pages that seem to come close to what I need but I can't quite get there. E.g. Limiting feature class parameter to specific geodatabase? - but in this example and others the input for the second parameter is based on the first, which is not the case for me.

My script so far is below. I have added "listFCs = arcpy.ListFeatureClasses()" because I feel like I'll need it but not sure how. I'm assuming I need to either edit "joinFC = arcpy.GetParameterAsText(1)", or change something in the script tool Parameters and/or Validation tabs, but I'm just not sure what.

I'm very new at this.

# This script creates a new "Country" or "District" column, using a Spatial Join, 
# and populates it for each feature based on the district/country. 
# Users can choose whether Country or District is the input.

import arcpy
import os
arcpy.env.workspace = os.path.dirname(os.path.realpath(__file__)) + "\Add Country or District Column\Districts_Countries.gdb"

arcpy.AddMessage("Workspace: " + arcpy.env.workspace)

# Get a list of all Feature Classes in the workspace - this should just be countries and districts
listFCs = arcpy.ListFeatureClasses()

# User input for the file that will have the extra field populated
targetFC = arcpy.GetParameterAsText(0)

# User input for the shapefile/FC that will be used to populate the extra field - i.e. Country or District
joinFC = arcpy.GetParameterAsText(1)

# Split the file path string for joinFC, so that the FC itself can be used in the final AddMessage statement
splitjoinFC = joinFC.split("\\")

# User input for the name and location of output file (input FC + column from joinFC)
outFC = arcpy.GetParameterAsText(2)

# Split the file path string for outFC, so that the FC itself can be used in the final AddMessage statement
splitoutFC = outFC.split("\\")

# Perform the spatial join
arcpy.SpatialJoin_analysis(targetFC, joinFC, outFC)

# Delete the extra fields that were added to the outFC during the spatial join
arcpy.DeleteField_management(outFC, ["Join_Count", "TARGET_FID", "GlobalID_1", "Shape_Leng", "Shape_Length_1"])

arcpy.AddMessage("\n" + splitjoinFC[-1] + " field added to " + splitoutFC[-1])

1 Answer 1


The way I would do this for a script tool assumes your joining layers never change their datasource names.

So add a parameter and make sure it is of type string, then you can set the Filter, this being a list of user friendly names. This will create a drop down like tool parameter listing the values.

Adding a list string

In your code when get the parameter value (joinFC = arcpy.GetParameterAsText(1)) you are getting the text Countries or Districts. At this point it is a simple if-else statement for choosing the actual path to the featureclass that will feed into the spatial join.

for example

if joinFC == "Districts":
    joinpath = r"c:\data\district.shp"

it would be joinpath that feeds into the spatial join tool.

  • This works, thank you very much! Jun 19, 2019 at 13:50

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.