Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
    
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 –  blah238 Jul 8 '13 at 17:46

2 Answers 2

up vote 2 down vote accepted

If your station number is held in a variable called stationNumber as a number then this untested syntax should work.

arcpy.SelectLayerByAttribute_management("Stations", "NEW_SELECTION", ' "OID" = ' + stationNumber)

share|improve this answer
    
Thanks that worked well after I defined stationNumber with stationNumber = raw_input("Enter the value") –  Rod Jun 30 '13 at 9:50
    
How could I select for 2 or more stations? –  Rod Jun 30 '13 at 14:05

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'

Edit: 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)'
share|improve this answer
1  
A more efficient query structure would be "OID" IN (x, y, z) — so, '"OID" IN (' + ','.join(map(str, numberlist)) + ')' –  nmpeterson Jun 30 '13 at 15:54
    
Wow, I need to learn more SQL apparently. That's much better. –  Paul Jun 30 '13 at 15:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.