Select_analysis using custom tool.

1198
7
06-08-2017 02:02 PM
StephanieHoutman
New Contributor

I want to create a custom tool so users can provide input for arcpy.Select_analysis(in_feature, out_feature, where_clause).

This is the line I want to change into a form for a  custom tool:

arcpy.Select_analysis("C:/Birds/Data/nso.shp", "C:/Birds/Data/active.shp", """"NEST" = 'active'""") #NEST is the attribute table field and active is an attribute.

But I want a custom tool so the user can input there own values.

I can properly code for the in_feature and out_feature variables using arcpy.GetParameterAsText.

I need help with the where_clause.

Here is some of my code:

inFeature = arcpy.GetParameterAsText (0) #variable for user to input feature
OutFeature = arcpy.GetParameterAsText(1) # variable for the output feature

FieldForSelect = arcpy.GetParameterAsText(5) #user inputs field in attribute table to select features

AttributeForSelect = arcpy.GetParameterAsText(6) #user inputs attribute to select for.

Would the code be

ActiveNest = arcpy.Select_analysis(inFeature, OutFeature, """"FieldForSelect" = 'AttributeForSelect'""") ?

My second question is what is the data type for FieldForSelect (Is it Field?)?This is a field in the attribute table.

And what is the data type for the actual attribute AttributeForSelect (Is it String?)? I think it is string since the attribute is a word/string.

From my example the field is NEST and I am selecting for "active".

Thank you.

0 Kudos
7 Replies
ChrisCatania
Esri Community Team

Hi Stephanie - Thanks for asking your questions and welcome to the GeoNet community! I wanted to let you know that we're moving your question to the https://community.esri.com/community/gis/web-gis/arcgisonline?sr=search&searchId=53087784-5625-4efb-... space so our Esri and user subject matter experts can further help answer this and future questions.  So you're aware on how and where to post your questions, here's a few quick tips and reminders:

  1. First, use the GeoNet search (top right corner by your profile icon) to search and see if your question has already by asked/answered previously.
  1. If not, than we suggest reviewing the GeoNet Community Structure, to find the most relevant space to post your question. 
  1. The GeoNet GeoNet Help group is for questions and tips on how to use the GeoNet community platform not the Esri ArcGIS platform. Following steps 1 & 2 above will give you the best results for getting Esri product questions answered.
  2. This GeoNet 101 "How to Ask Questions on GeoNet" post is also a helpful guide to asking questions: https://community.esri.com/community/help-and-feedback/blog/2016/08/15/community-news-and-tips-how-t...

 

Thanks again for contributing and I hope this helps and let us know if you have any questions.

Esri Community Strategist
0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Stephanie,

What flavor (ArcMap/Catalog v's Pro) and version are you using?  There are some differences between the two flavors for sure.  You may want to look at and bookmark /blogs/dan_patterson/2016/05/09/the-links?sr=search&searchId=51799929-4e8d-497c-ae37-e8295602d240&se...

also, it would help if you would post you code (change any sensitive info) https://community.esri.com/people/curtvprice/blog/2014/09/25/posting-code-blocks-in-the-new-geonet?s...

are you creating a tool in a toolbox and using the standard interface to create the user dialog?  Are you planning on creating a geoprocessing tool or will it just be run in the users session? Sorry for all the questions, but they may help others when responding.

StephanieHoutman
New Contributor

Hello Rebecca,

I am using ArcGIS 10.5 products. I am creating a too in a toolbox using the standard interface. I am going to have it run in a user session. 

I actually found that this works:

ActiveNest  = arcpy.Select_analysis(inFeature, OutFeature, FieldForSelect = AttributeForSelect)) 

My mistake was in setting the parameters for the variable in the script properties.

I do have a second question. My script at this point allows for two features to buffer. As the script runs it ask for the first field, attribute,  then second field, attribute. For this application I almost always only have two attribute I want to separate. But is there an easy way to code for a variable amount of inputs? Can you suggest some resources? I have kept my coding fairly simple in the past. 

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Glad you resolved your initial question.

FYI, in this case, you could typically mark your question as correct or as "assumed answered" so others would know it is resolved.  You could then start a new post if needed.  This keeps threads from getting too long and/or off topic. However, this is related enough that I think it's ok.

unfortunately I'm not at my desktop right now, so can't give specifics, but you can populate an input list of fields based on the input of the Layer and have it show a checkbox so the parameter creates a list. (Unless you want the user to know and type the field list, i.e. Think it would be "fields;field2")

ive done something similar, but as mentioned, not where I can look.  I'll tag a couple others that might know (if they haven't responded as I am typing this) Dan_Pattersoncurtvprice‌ 

0 Kudos
DanPatterson_Retired
MVP Emeritus

If you have a featureclass as a parameter (or table from one), then your next parameter could be a field which is 'derived' from the above... I think there is an sql parameter which would allow you to build a query within the dialog.  I don't have an example on hand, but the sequence of doing a query using your own tool is the same as you would from the standard dialog.

0 Kudos
curtvprice
MVP Esteemed Contributor

I agree, expose the SQL expression as a parameter depending on the input table.

0 Kudos