# --------------------------------------------------------------------------- # adjacent.py # Created on: 2012-07-12 15:14:17.00000 # (generated by ArcGIS/ModelBuilder) # Usage: adjacent <countyName> # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy # Set Geoprocessing environments arcpy.env.scratchWorkspace = "C:\\ESRItest\\model\\process.gdb" arcpy.env.workspace = "C:\\ESRItest\\model\\process.gdb" # Script arguments countyName = arcpy.GetParameterAsText(0) if countyName == '#' or not countyName: countyName = "Boone" # provide a default value if unspecified # Local variables: selected_County = countyName Adjacent_Counties = selected_County county = "county" # Process: Select Layer By Attribute arcpy.SelectLayerByAttribute_management(county, "NEW_SELECTION", "\"NAME\" = '%countyName%'") # Process: Select Layer By Location result = arcpy.SelectLayerByLocation_management(selected_County, "BOUNDARY_TOUCHES", "", "", "NEW_SELECTION") ## NOW WHAT?
Solved! Go to Solution.
county = 'filepath to a feature class on disk' # Process: Select Layer By Attribute county_selection = arcpy.SelectLayerByAttribute_management(county, "NEW_SELECTION", "\"NAME\" = '%countyName%'") # Process: Select Layer By Location result = arcpy.SelectLayerByLocation_management(county_selection, "BOUNDARY_TOUCHES", "", "", "NEW_SELECTION")
county = arcpy.MakeFeatureLayer_management(<feature class on disk>, "outlayer name")
# ---------------------------------------------------------------------------
# adjacent.py
# Created on: 2012-07-12 15:14:17.00000
# (generated by ArcGIS/ModelBuilder)
# Usage: adjacent <countyName>
# Description:
# ---------------------------------------------------------------------------
# Import arcpy module
import arcpy
# Set Geoprocessing environments
arcpy.env.scratchWorkspace = "C:\\ESRItest\\model\\process.gdb"
arcpy.env.workspace = "C:\\ESRItest\\model\\process.gdb"
#overwrite pre-existing files
arcpy.env.overwriteOutput = True
# Script arguments
countyName = arcpy.GetParameterAsText(0)
if countyName == '#' or not countyName:
countyName = "Boone" # provide a default value if unspecified
# Local variables:
county = "county"
county_Layer = "county_Layer"
countyList = []
txt_list = ""
#Output_Feature_Class = "C:\\ESRItest\\model\\process.gdb\\county_CopyFeaturesOutput"
# Process: Make Feature Layer
county_Layer = arcpy.MakeFeatureLayer_management("C:\ESRItest\model\process.gdb\county", county_Layer, "", "", "OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;NAME NAME VISIBLE NONE;CNTY_FIPS CNTY_FIPS VISIBLE NONE;FIPS FIPS VISIBLE NONE;POP2005 POP2005 VISIBLE NONE;POP05_SQMI POP05_SQMI VISIBLE NONE;SQMI SQMI VISIBLE NONE;NAME2 NAME2 VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE;Shape_Area Shape_Area VISIBLE NONE")
#Process: Select Layer By Attributes
countySelection = arcpy.SelectLayerByAttribute_management(county_Layer, "NEW_SELECTION", "\"NAME\" = '%countyName%' ")
# Process: Select Layer By Location
results = arcpy.SelectLayerByLocation_management(countySelection, "INTERSECT", "", "", "NEW_SELECTION")
# Process: Copy Features
#arcpy.CopyFeatures_management(county_Layer, Output_Feature_Class, "", "0", "0", "0")
## Step through the selection to get the values from the name field
rows = arcpy.SearchCursor(results,"","","NAME","NAME")
for row in rows:
neighborVal = row.getValue("NAME")
countyList.append(neighborVal)
txt_list = ','.join(countyList)
print txt_list
import arcpy
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "ForestStats"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [getSummary]
class getSummary(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "GetData"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName="Area of Interest",
name="in_area",
datatype="String",
parameterType="Required",
direction="Input")
param1 = arcpy.Parameter(
displayName="Forest Statistics",
name="forest_statistics",
datatype="String",
parameterType="Derived",
direction="Output")
params = [param0, param1]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def execute(self, parameters, messages):
"""The source code of the tool."""
d = {}
tbl = r'E:\gis\forestry\20120711_forestryWebApp\Forestry.gdb\wholeCity'
with arcpy.da.SearchCursor(tbl, ('rangeClass', 'count')) as c:
for r in c:
d[r[0]] = r[1]
print d
arcpy.AddMessage(str(d))
#params[1] = str(d)
return
I did realize somewhere along the way that I couldn't use a featureclass as input.
I'm getting confused on whether I should run this from the python shell or if I can run it as a script in a toolbox. If I run it as a script, it gives me the prompt I expect for the name, but it doesn't seem to return anything. There aren't any errors, either. I'm not sure there's even a place for my results to go since it's just returning a list and not anything like a layer or table.
If I run it through the shell, then when do I specify my input string to search on?
I'm trying to return a string from a gp service....How do i pass str(d) to the output param1? Once I do this, I should have a gp service that returns some good clean data!
countyName = raw_input('What County do you want to select?')countySelection = arcpy.SelectLayerByAttribute_management(county_Layer, "NEW_SELECTION", "\"NAME\" = countyName")
Traceback (most recent call last):I figured it wanted some quotes, so I changed it to
File "C:\ESRItest\model\adjacent.py", line 36, in <module>
countySelection = arcpy.SelectLayerByAttribute_management(county_Layer, "NEW_SELECTION", "\"NAME\" = countyName")
File "C:\Program Files\ArcGIS\Desktop10.0\ArcPy\arcpy\management.py", line 4259, in SelectLayerByAttribute
raise e
ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).
countySelection = arcpy.SelectLayerByAttribute_management(county_Layer, "NEW_SELECTION", "\"NAME\" = 'countyName'")
# --------------------------------------------------------------------------- # adjacent.py # Created on: 2012-07-12 15:14:17.00000 # (generated by ArcGIS/ModelBuilder) # Usage: adjacent <countyName> # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy # Set Geoprocessing environments arcpy.env.scratchWorkspace = "C:\\ESRItest\\model\\process.gdb" arcpy.env.workspace = "C:\\ESRItest\\model\\process.gdb" #overwrite pre-existing files arcpy.env.overwriteOutput = True # Script arguments #countyName = arcpy.GetParameterAsText(0) countyName = raw_input('What County do you want to select?') # Local variables: county = "county" county_Layer = "county_Layer" countyList = [] txt_list = "" #Output_Feature_Class = "C:\\ESRItest\\model\\process.gdb\\county_CopyFeaturesOutput" # Process: Make Feature Layer county_Layer = arcpy.MakeFeatureLayer_management("C:\ESRItest\model\process.gdb\county", county_Layer, "", "", "OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;NAME NAME VISIBLE NONE;CNTY_FIPS CNTY_FIPS VISIBLE NONE;FIPS FIPS VISIBLE NONE;POP2005 POP2005 VISIBLE NONE;POP05_SQMI POP05_SQMI VISIBLE NONE;SQMI SQMI VISIBLE NONE;NAME2 NAME2 VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE;Shape_Area Shape_Area VISIBLE NONE") #Process: Select Layer By Attributes #print countyName whereClause=" \"NAME\" = " + "'"+countyName+"'" #print whereClause countySelection = arcpy.SelectLayerByAttribute_management(county_Layer, "NEW_SELECTION", whereClause) # Process: Select Layer By Location results = arcpy.SelectLayerByLocation_management(countySelection, "BOUNDARY_TOUCHES", "", "", "NEW_SELECTION") # Process: Copy Features #arcpy.CopyFeatures_management(county_Layer, Output_Feature_Class, "", "0", "0", "0") ## Step through the selection to get the values from the name field rows = arcpy.SearchCursor(results,"","","NAME","NAME") for row in rows: neighborVal = row.getValue("NAME") if neighborVal != countyName: countyList.append(neighborVal) txt_list = ','.join(countyList) print txt_list