-1

是否有原因我的计数等于 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 所学校的文件。

我需要在该文件中进入一个名为“设施”的字段,然后在其中获取“高中”的数量(我知道如何做这部分)。但我也需要这段代码作为一个函数工作(这是我遇到的问题)。

4

2 回答 2

0

尽管您的问题尚不清楚,但我认为您正在寻找这个!

print ("The number of " + facility + " are:", int(count) ) 
于 2017-01-28T19:51:05.097 回答
-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
于 2017-01-31T17:25:53.367 回答