有没有理由我的计数等于零,当它应该是29.我试图初始化我的函数内的计数但是它说它没有定义。
shapefile = "Schools.shp"
work = r"c:\Scripts\Lab 6 Data"
facility = "HIGH SCHOOL"
count = 0
def numSchools(work, shapefile, facility):
whereClause = "\"FACILITY\" = 'HIGH SCHOOL' " # where clause for high schools
cursor = arcpy.SearchCursor("Schools.shp", facility)
result = arcpy.GetCount_management(cursor)
for result in cursor:
count = int(result.getOutput(0))# Get the first value from the Result object
return count
print ("The number of " + facility + " are:"), int(count)
在最基本的形式中,此代码返回正确数量的学校。
import arcpy
arcpy.env.workspace = r"c:\Scripts\Lab 6 Data"
result = arcpy.GetCount_management("Schools.shp")
count = int(result.getOutput(0))
print ("The number of rows in 'schools' is "),int(count)
但是,Schools.shp是一个包含201所学校的文件。
我需要在该档案中进入一个名为“设施”的字段,然后在其中获取“高中”的数量' (我知道如何做这部分)。但我也需要这个代码作为一个函数(这是我遇到的麻烦)。
答案 0 :(得分:0)
虽然你的问题没有解决,但我认为你正在寻找这个!
print ("The number of " + facility + " are:", int(count) )
答案 1 :(得分:-1)
searchCurs = arcpy.da.SearchCursor(shapefile, field, whereClause)
row = searchCurs.next()
for row in searchCurs:
value = row[0]
high_schools = [row[0] for row in arcpy.da.SearchCursor(shapefile, field, whereClause)]
count = len(high_schools)
print count