Python 3.x 在形状文件的字段中查找最大值
我有一个shapefile(mich_co.shp),我试图找到人口最多的县。我的想法是使用max()函数,这是不可能的。以下是我目前的代码:Python 3.x 在形状文件的字段中查找最大值,python-3.x,gis,gdal,ogr,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 = data
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:“层”对象不能解释为整数
如有任何帮助或提示,将不胜感激
谢谢你 需要一个iterable,例如list。尝试建立一个列表:
pops = [ feature.GetField("pop") for feature in layer ]
print(max(pops))