在ogr或geopandas python中的shapefile中创建FID列

在ogr或geopandas python中的shapefile中创建FID列,python,shapefile,geopandas,osgeo,Python,Shapefile,Geopandas,Osgeo,我需要形状文件中FID列的信息。不知何故,在加载shapefile时,该列并没有“显示”。我需要该专栏的信息,因为我需要说: 氢火焰离子化检测器 身份证件 标签 1. 1. 森林 2. 1. 森林 3. 2. 水 4. 2. 水 5. 3. 城市的 好的。。。读了一点之后,我发现了一种更有效的方法 import os from osgeo import ogr src = "C:/path/" #### Read SHP and create X_train_x segm

我需要形状文件中FID列的信息。不知何故,在加载shapefile时,该列并没有“显示”。我需要该专栏的信息,因为我需要说:

氢火焰离子化检测器 身份证件 标签 1. 1. 森林 2. 1. 森林 3. 2. 水 4. 2. 水 5. 3. 城市的
好的。。。读了一点之后,我发现了一种更有效的方法

import os
from osgeo import ogr

src = "C:/path/"

#### Read SHP and create X_train_x segments array ####
# open training data
train_cl_fn = os.path.join(src, "train.shp")
driver = ogr.GetDriverByName('ESRI Shapefile')
train_ds = driver.Open(train_cl_fn, 1) # 0 means read-only. 1 means writeable.
layer = train_ds.GetLayer()

fieldDefn = ogr.FieldDefn('fidl', ogr.OFTReal) 
fieldDefn.SetWidth(14) 
fieldDefn.SetPrecision(6)
layer.CreateField(fieldDefn)

for Ft in layer:
    ThisID = int(Ft.GetFID())
    #print('id is {}'.format(ThisID))
    Ft.SetField('fidl',ThisID)          # Write the FID to the fidl field
    layer.SetFeature(Ft)              # update the feature
dataSource = None  
你知道更好的方法吗