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)
-
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– mr.adam2015年06月03日 16:45:36 +00:00Commented Jun 3, 2015 at 16:45
1 Answer 1
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))
-
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– mgreen2015年06月03日 19:54:24 +00:00Commented Jun 3, 2015 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– crmackey2015年06月03日 20:09:41 +00:00Commented Jun 3, 2015 at 20:09