-1

I am creating code that accesses a shapefile named Schools.shp. I data I am trying to locate is the attribute field named FACILITIES. Within the FACILITIES field I am trying to get values that the field contains named HIGH SCHOOLS. In my code I need count the number of high schools I found. The value High School needs to be accessed through user input. (raw_input statement).

Using arcpy.da formatting.

import arcpy

work = "c:\Scripts\Lab 6 Data"
facility = "High School"
whereClause = "\"FACILITY\" = 'HIGH SCHOOL' "
shapefile = "Schools.shp"
arcpy.env.workspace = work
field = ['FACILITY']

searchCurs = arcpy.da.SearchCursor(shapefile, field, whereClause)
row = searchCurs.next()
for row in searchCurs:
    # using getValue() to get the name of the high school
    value = row.getValue("NAME")
    row = searchCurs.next()
    #count the number of specific facilities   
    result = row.GetCount_management("NAME")
    count = int(result.getOutput(0))
    print (count)

My issue is setting up the code so that the user can enter various facility types and the correct count for each facility is returned. Ex: User inputs High School, 29 is returned..... User inputs Junior High, 6 is returned.

  • 1
    Use Add Field Delimiters tool for your sql clause to rule out errors with " or ' signs – BERA Jan 30 '17 at 20:58
  • 2
    You've got several significant errors in this Python code. DA cursor rows do not support getValue, so you never got to the GetCount_management error. – Vince Jan 30 '17 at 20:59
  • @Vince Is AddField the arcpy.da equivalent to getValue? – A_Worthy Jan 30 '17 at 21:23
  • 1
    It would be best to roll back the edits to include your original question and the screenshot, then add edits to that if necessary. – Aaron Jan 30 '17 at 21:44
  • 2
    Welcome to GIS SE! As a new user be sure to take the Tour. For questions that involve code we ask that you show us not just some code, but also the precise error message, including line number, that you get when you run the exact code that you present. See meta.gis.stackexchange.com/questions/4312 for tips. – PolyGeo Jan 30 '17 at 21:48
1

You can wrap a data access da Search Cursor in a list comprehension. Try the following:

import arcpy

shp = r'C:\path\to\your\shapefile.shp'

facility_count = len([row[0] for row in arcpy.da.SearchCursor(shp, "FACILITY") if "HIGH SCHOOL" in row[0]])
print facility_count

Alternatively, you can count all the items in a field using the collections module...

import arcpy
from collections import Counter

facility = [row[0] for row in arcpy.da.SearchCursor(shp, "FACILITY")]

print Counter(facility)

Which yields something like...

Counter({u'HIGH SCHOOL': 4, u'MIDDLE SCHOOL': 1, u'ELEMENTARY SCHOOL': 1})
  • I get this error still RuntimeError: A column was specified that does not exist. – A_Worthy Jan 30 '17 at 20:56
  • The High schools are not a field name but a sub value of the Facility field – A_Worthy Jan 30 '17 at 20:56
  • Ok, Thanks for the clarification. I will update my answer with the necessary changes. – Aaron Jan 30 '17 at 20:57
  • Are you querying two fields, first the "FACILITIES" field, which contains a value of interest called "HIgh School" and then another field with the name of the facility? – Aaron Jan 30 '17 at 21:01
  • 1
    Could you add a screenshot of your attribute table highlighting the FACILITY field? It is important to see exactly how the text is expressed. – Aaron Jan 30 '17 at 21:14
-1
import arcpy
try:
    work = "c:\Scripts\Lab 6 Data"
    facility = "High School"
    whereClause = "\"FACILITY\" = 'HIGH SCHOOL' "
    shapefile = "Schools.shp"
    arcpy.env.workspace = work
    field = ['FACILITY']

    searchCurs = arcpy.da.SearchCursor(shapefile, field, whereClause)
    row = searchCurs.next()
    for row in searchCurs:
    # using getValue() to get the name of the high school
    value = row[0]
    high_schools = [row[0] for row in arcpy.da.SearchCursor(shapefile, field, whereClause)]
    count = len(high_schools)
    print count

I've altered the getValue methods towards a method more acceptable towards arcpy.da "row[0]"

I have also got the count of high schools using. Though this result prints out 29x. It is acceptable because i have not found a way for it to print once.

high_schools = [row[0] for row in arcpy.da.SearchCursor(shapefile, field, whereClause)]
    count = len(high_schools)
  • 3
    Is this a solution or additional info for your question? If it's your solution please edit to include a bit of a description around what you've done. If it's additional info then please edit your question. – Midavalo Jan 30 '17 at 23:47

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.