# -- coding:utf-8 -- import geopandas as gpd import os from osgeo import ogr # shp 文件批量添加字段 def addfeilds(filepath): for filename in os.listdir(filepath): print(filename) if filename.endswith(".shp"): try: driver = ogr.GetDriverByName('ESRI Shapefile') datasource = driver.Open(filepath + filename, 1) # 0只读,1可写 layer = datasource.GetLayer() layer.CreateField(ogr.FieldDefn('_id',ogr.OFTInteger)) layer.CreateField(ogr.FieldDefn('province_c',ogr.OFTInteger64)) layer.CreateField(ogr.FieldDefn('province_n',ogr.OFTString)) layer.CreateField(ogr.FieldDefn('city_code',ogr.OFTInteger64)) layer.CreateField(ogr.FieldDefn('city_name',ogr.OFTString)) layer.CreateField(ogr.FieldDefn('county_cod',ogr.OFTInteger64)) layer.CreateField(ogr.FieldDefn('county_nam',ogr.OFTString)) layer.CreateField(ogr.FieldDefn('Hazard_cla',ogr.OFTString)) layer.CreateField(ogr.FieldDefn('class',ogr.OFTInteger)) except Exception as ex: print('文件{0}添加字段失败,错误信息为:{1}'.format(filename, ex))
标签:shp,layer,FieldDefn,CreateField,filename,ogr,添加,文件批量 From: https://www.cnblogs.com/gzl0612/p/17479674.html