是否有原因我的计数等于 0,而它应该是 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 所学校的文件。
我需要在该文件中进入一个名为“设施”的字段,然后在其中获取“高中”的数量(我知道如何做这部分)。但我也需要这段代码作为一个函数工作(这是我遇到的问题)。