如何获取形状文件的特征数?

2024-04-26 07:22:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从一个shapefile中获取特征的数量,但无法正确获取。 代码如下:

import ogr

# Open the shapefile and get the first layer
datasource = ogr.Open("E:HP_Python/GeospatialDevelopmentByExampleWithPython/ExampleData/data/world_borders_simple.shp")

layer = datasource.GetLayerByIndex(0)

print("Number of features:{}".format(layer.GetFeatureCount()))

此处显示错误消息:

AttributeError                            Traceback (most recent call last)
<ipython-input-1-b694e03f871f> in <module>()
      2 # Open the shapefile and get the first layer
      3 datasource = ogr.Open("E:HP_Python/GeospatialDevelopmentByExampleWithPython/ExampleData/data/world_borders_simple.shp")
----> 4 layer = datasource.GetLayerByIndex(0)
      5 print("Number of features:{}".format(layer.GetFeatureCount()))

AttributeError: 'NoneType' object has no attribute 'GetLayerByIndex'

Tags: andthelayerworlddatagetopenhp