在shapefile的字段中查找最大值

时间:2019-02-19 07:32:40

标签: python-3.x gis gdal ogr

我有一个shapefile(mich_co.shp),我试图找到人口最多的县。我的想法是不可能使用max()函数。到目前为止,这是我的代码:

from osgeo import ogr
import os

shapefile = "C:/Users/root/Python/mich_co.shp"
driver = ogr.GetDriverByName("ESRI Shapefile")
dataSource = driver.Open(shapefile, 0)
layer = dataSource.GetLayer()

for feature in layer:
    print(feature.GetField("pop"))
layer.ResetReading()

但是,上面的代码仅打印“ pop”字段的所有值,如下所示:

10635.0
9541.0
112039.0
29234.0
23406.0
15477.0
8683.0
58990.0
106935.0
17465.0
156067.0
43868.0
135099.0

我尝试过:

print(max(feature.GetField("pop")))

,但返回TypeError:“ float”对象不可迭代。为此,我也尝试过:

for feature in range(layer):

并返回TypeError:“图层”对象无法解释为整数。

任何提示的帮助将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

How to change the session timeout in PHP?需要迭代,例如列表。尝试建立清单:

pops = [ feature.GetField("pop") for feature in layer ]
print(max(pops))
相关问题