Get Specific Field 'value' and return names and count.

966
6
Jump to solution
01-31-2017 10:02 AM
AleahWorthem
New Contributor III

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)
   

Tags (2)
Reply
0 Kudos
1 Solution

Accepted Solutions
WesMiller
Regular Contributor III

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

View solution in original post

6 Replies
JoshuaBixby
MVP Esteemed Contributor

I don't have time at the moment to mock up some code, but I will share a few thoughts:

  • Do you expect duplicate names?  I ask because wouldn't returning a list of names basically give the user the count as well?  Do you need to explicitly return both or would just a list of names work?
  • It looks like you are using the older/original cursors.  I recommend using the somewhat new-ish Data Access module cursors.
  • I think the best approach here will be a combination of SQL and cursors.  Since the user will be picking a value from a well-structured, pre-defined list of facilities, using an SQL where clause to either create a selection set on a layer or define the search cursor will work and perform well.  Then, you only loop through the records returned and not all of the records.
Reply
0 Kudos
AleahWorthem
New Contributor III

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

Reply
0 Kudos
WesMiller
Regular Contributor III

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.

Reply
0 Kudos
AleahWorthem
New Contributor III

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()?

Reply
0 Kudos
WesMiller
Regular Contributor III

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

View solution in original post

JoshuaBixby
MVP Esteemed Contributor

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.