【问题标题】:Find Max Value in a field of a shapefile在 shapefile 的字段中查找最大值
【发布时间】:2019-02-19 07:32:39
【问题描述】:

我有一个 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' object is not iterable。为此,我也尝试过:

for feature in range(layer):

它返回 TypeError: 'Layer' 对象不能被解释为整数。

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

谢谢!

【问题讨论】:

    标签: python-3.x gis gdal ogr


    【解决方案1】:

    max() 需要一个可迭代对象,例如一个列表。尝试建立一个列表:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多