2

I have this py script for selecting one point.

# Within selected features, select only those points of a chosen value 
arcpy.SelectLayerByAttribute_management("Stations", "NEW_SELECTION", ' "OID" = 465 ')

That works but every time I want to select a new station then I have to change the station number in the SQL statement.

How can I select a new station as a raw input instead of editing the script every time?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jun 30, 2013 at 9:21
1
  • 1
    Why do you want to use raw input? If you are running this as a script tool you could set up a parameter that lets you enter one or multiple values, and then plug that into the where clause with something like one of these answers: 1 2 3 Commented Jul 8, 2013 at 17:46

2 Answers 2

5

To select multiple OIDs, you could just hardcode it like so:

number1 = 25
number2 = 37
'"OID"=' + str(number1) + ' OR "OID"=' + str(number2)
'"OID"=25 OR "OID"=37'

This becomes a problem when you need to select multiple OIDs, so you can use something like below. I've successfully used this as inputs to cursors and select layer by attribute/location, but it can't be used for anything over a few thousand entries as it causes fatal ArcMap crashes. (I'm not sure what the cut off point is.) If you need to be selecting that many OIDs, it's best to write all your data to a list of lists and filter out what you want in Python.

numberlist = [25,37,29,251,17]
'"OID"=' + ' OR "OID"='.join(map(str, numberlist))
'"OID"=25 OR "OID"=37 OR "OID"=29 OR "OID"=251 OR "OID"=17'

As @nmpeterson pointed out, the following is much better and probably doesn't suffer from the crashes:

numberlist = [25,37,29,251,17]
'"OID" IN ({1})'.format(",".join(map(str, numberlist))) 
'"OID" IN (25,37,29,251,17)'
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Jun 30, 2013 at 15:30
0
3

This should work. It uses Python string formatting to construct the where_clause.

stationNumber = raw_input("Enter the value")
arcpy.SelectLayerByAttribute_management("Stations", "NEW_SELECTION", "OID = {0}".format(stationNumber))
answered Jun 30, 2013 at 9:33
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.