1

I am new to GIS and Python is not my forte, so I am looking for some help. Ideally, what I am trying to do is create a tool that allows the user to select an address from my Address points layer. I need to select values from multiple fields: STREET_NUMBER, STREET_NAME and STREET_TYPE. What I really need help with is building my expression. Here is what I have so far:

# Import system modules
import arcpy
from arcpy import env

# Set workspace
workspace = r"O:\Users\Student\Scratch\SC_Test.gdb"

# Set variables
stNum = arcpy.GetParameterAsText(0)    
stName = arcpy.GetParameterAsText(1)    
stType = arcpy.GetParameterAsText(2)

# Make layer from feature class
arcpy.MakeFeatureLayer_management("Address", "add_lyr")


arcpy.SelectLayerByAttribute_management("add_lyr", "NEW_SELECTION", "STREET_NUMBER" =  stNum)
1
  • Welcome to GIS stack exchange. There are tons of questions around here like yours, here's one that may help gis.stackexchange.com/questions/144838/… If you're still getting errors, it's best to include them in your question. – mr.adam Jun 3 '15 at 16:45
2

Your where clause needs to be wrapped in quotes. Also, is the 'STREET_NUMBER' field numeric? If so, your expression can be fixed by doing this:

arcpy.SelectLayerByLocation_management("add_lyr", "NEW_SELECTION",'"STREET_NUMBER" = {}'.format( stNum))

If it is a text field, you will need to wrap that in single quotes:

arcpy.SelectLayerByLocation_management("add_lyr", "NEW_SELECTION", ''' "STREET_NUMBER" = '{}' '''.format( stNum))
2
  • I ran the tool in ArcMap using just the STREET_NUMBER parameter (which is numeric) to test and the tool ran fine, however it did not select anything from my Address layer. Maybe I should be more explicit... I want this tool to select an address from my Address layer, that way it can be easily located in ArcMap. I also want the expression to facilitate my STREET_NAME and STREET_TYPE fields, which are both text, to locate a single, specific address. Thank you ! – mgreen Jun 3 '15 at 19:54
  • Perhaps instead of just having a text parameter for the stNum variable, you can just change that parameter to a SQL data type and that way you can use the Select By Attribute GUI. You will have to make sure that it is obtained from your Address Feature class though. – crmackey Jun 3 '15 at 20:09

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.