I have this attribute table where I am supposed to access the Field "Facility" and based on what type of facility the user enters, my code is supposed to return the names and count of that facility type. For example: I enter High School as my facility type. My code needs to return the names of the high schools, as well as how many high schools there are. Oh and this is supposed to operate as a function.
import arcpy
arcpy.env.workspace = raw_input("Enter the workspace path: ")# c:\Scripts\Lab 6 Data
shapefile = "Schools.shp"
work = arcpy.env.workspace
sTyp = "HIGH SCHOOL"
def numSchools(work, shapefile, sTyp):
sc = arcpy.SearchCursor(work, shapefile)
field = "FACILITY"
for row in sc:
if row.getValue(field)== sTyp
return row.getValue("NAME")
numSchools(work, shapefile, sTyp)
Solved! Go to Solution.
Add a where clause to your search cursor
SearchCursor—Help | ArcGIS for Desktop
import arcpy
fc = r"Your\feature\class\here"
fld = "fieldName"
sType = "ValueYourLooking4"
whereclause = """{} = '{}'""".format(arcpy.AddFieldDelimiters(fc, fld),sType)
count = 0
with arcpy.da.SearchCursor(fc, fld, whereclause) as rows:
for row in rows:
count+=1
print count
I don't have time at the moment to mock up some code, but I will share a few thoughts:
Sorry, I do not need to return the names of the schools just the count. I works fine when I hard-coded it but I do not know how to change the sTyp to High School or Elementary School and return the correct output.
I am required to use Search Cursors to write my code. using arcpy or arcpy.da
How do you plan to get the user to input the data?
Will this be a model, a python addin or other?
My first thought would be Select Layer By Attribute—Help | ArcGIS for Desktop then Get Count—Help | ArcGIS for Desktop
EDIT:
Just read your second post all the way.
Still would need to know what you intend for an input.
You could use a query on your search cursor to limit the number of records. Then loop through and count them.
The sTyp will be converted into a raw_input statement. It is just hard-coded here for convenience.
Instead of select layer by attribute, is there a way to get to the field "Facility" the value 'High School' by using SearchCursor()?
Add a where clause to your search cursor
SearchCursor—Help | ArcGIS for Desktop
import arcpy
fc = r"Your\feature\class\here"
fld = "fieldName"
sType = "ValueYourLooking4"
whereclause = """{} = '{}'""".format(arcpy.AddFieldDelimiters(fc, fld),sType)
count = 0
with arcpy.da.SearchCursor(fc, fld, whereclause) as rows:
for row in rows:
count+=1
print count
For counting with cursors, Python's built in sum works great, and is more idiomatic if you care about such things.
import arcpy
fc = r"Your\feature\class\here"
fld = "fieldName"
sType = "ValueYourLooking4"
whereclause = """{} = '{}'""".format(arcpy.AddFieldDelimiters(fc, fld),sType)
with arcpy.da.SearchCursor(fc, fld, whereclause) as rows:
count = sum(1 for row in rows)
print count
As much as I like working with Python's built-in functions, for simple counting of selections or subsets of records, especially with large data sets, the Get Count tool is the quickest.