Select to view content in your preferred language

ArcPy Scripting Help

561
4
12-06-2022 06:36 AM
TaylorBoles1
New Contributor II

Hello,

I am trying to create a custom tool to use in ArcPro that takes:

1) Input value--the feature class I want to work with

2) Input value--the name of the resulting output feature class from selection

3) Input value--the year in my data set that I want selected and the records copied into a new feature class

I am having trouble with the last part of my script:

import arcpy


# Setting some variables
inputfc = arcpy.GetParameterAsText(0)
outputfc = arcpy.GetParameterAsText(1)
year_value = arcpy.GetParameter(2)
where_clause = 'SURVEY_YEAR = {0}'.format(year_value)
arcpy.Select_analysis(inputfc, outputfc, where_clause)

I get an execute error and it says that my parameters are not valid. I am not sure what is wrong--I am also newish to ArcPy which is why I am struggling with this one.

Thanks for your help!

Edit: here's a screenshot of the error message

prob.JPG

0 Kudos
4 Replies
JohannesLindner
MVP Frequent Contributor

Markdown doesn't work here. To format code:

JohannesLindner_0-1670340168041.pngJohannesLindner_1-1670340188604.png

 

 

Can you post a screenshot of the error message? That way, we can try to backtrack to where your script fails.

Does your inputfc really have a field called SURVEY_YEAR, and is that field really an integer field?

 


Have a great day!
Johannes
0 Kudos
TaylorBoles1
New Contributor II

Thank you. I changed some things around to make it easier to read/work with. Also, yes, the inputfc (the input feature chosen that I am working with) does have a field called SURVEY_YEAR, and that field contains short-numeric int values.

0 Kudos
DanPatterson
MVP Esteemed Contributor

Where are the parameters coming from?

Are you using a custom toolbox?

A quick tour of creating tools with Python—ArcGIS Pro | Documentation

I would suggest using it since you can associate your parameters in the dialog with those in the script easier and visually


... sort of retired...
Brian_Wilson
Regular Contributor II

Put in a line to test if it can find the input file, after line 5, perhaps

if not arcpy.Exists(inputfc): exit(-1)

 

The next thing I would try is to hard code values and see what happens, testing only the function with three known good inputs, like for example,

 

import arcpy

# Setting some variables
#inputfc = arcpy.GetParameterAsText(0)
#outputfc = arcpy.GetParameterAsText(1)
#year_value = arcpy.GetParameter(2)
#where_clause = 'SURVEY_YEAR = {0}'.format(year_value)
inputfc = "C:\\some\\file\\path\\survey.shp"
outputfc = "C:\\Temp\\output.shp"
where_clause = "1=1"
arcpy.Select_analysis(inputfc, outputfc, where_clause)

 

Then you might be able to narrow it down removing those changes one at a time to see what it's griping about.