Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在形状文件的字段中查找最大值_Python 3.x_Gis_Gdal_Ogr - Fatal编程技术网

Python 3.x 在形状文件的字段中查找最大值

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

我有一个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:“层”对象不能解释为整数

如有任何帮助或提示,将不胜感激

谢谢你

需要一个iterable,例如list。尝试建立一个列表:

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