GDAL设置shapefile编码
设置全局变量
# 设置为空时可自动识别编码,GBK和UTF-8都可以识别
gdal.SetConfigOption("SHAPE_ENCODING", "")
查看options属性字段
支持的属性有两种方法可以查看:
注:ogrinfo --formats命令可以打印出支持的所有驱动名称
1.命令行
>ogrinfo --format "ESRI Shapefile"
Format Details:
Short Name: ESRI Shapefile
Long Name: ESRI Shapefile
Supports: Vector
Extensions: shp dbf
Help Topic: drv_shape.html
Supports: Open() - Open existing dataset.
Supports: Create() - Create writable dataset.
Supports: Virtual IO - eg. /vsimem/
Creation Field Datatypes: Integer Integer64 Real String Date DateTime
<CreationOptionList />
<LayerCreationOptionList>
<Option name="SHPT" type="string-select" description="type of shape" default="automatically detected">
<Value>POINT</Value>
<Value>ARC</Value>
<Value>POLYGON</Value>
<Value>MULTIPOINT</Value>
<Value>POINTZ</Value>
<Value>ARCZ</Value>
<Value>POLYGONZ</Value>
<Value>MULTIPOINTZ</Value>
<Value>POINTM</Value>
<Value>ARCM</Value>
<Value>POLYGONM</Value>
<Value>MULTIPOINTM</Value>
<Value>POINTZM</Value>
<Value>ARCZM</Value>
<Value>POLYGONZM</Value>
<Value>MULTIPOINTZM</Value>
<Value>MULTIPATCH</Value>
<Value>NONE</Value>
<Value>NULL</Value>
</Option>
<Option name="2GB_LIMIT" type="boolean" description="Restrict .shp and .dbf to 2GB" default="NO" />
<Option name="ENCODING" type="string" description="DBF encoding" default="LDID/87" />
<Option name="RESIZE" type="boolean" description="To resize fields to their optimal size." default="NO" />
<Option name="SPATIAL_INDEX" type="boolean" description="To create a spatial index." default="NO" />
<Option name="DBF_DATE_LAST_UPDATE" type="string" description="Modification date to write in DBF header with YYYY-MM-DD format" />
<Option name="AUTO_REPACK" type="boolean" description="Whether the shapefile should be automatically repacked when needed" default="YES" />
<Option name="DBF_EOF_CHAR" type="boolean" description="Whether to write the 0x1A end-of-file character in DBF files" default="YES" />
</LayerCreationOptionList>
<OpenOptionList>
<Option name="ENCODING" type="string" description="to override the encoding interpretation of the DBF with any encoding supported by CPLRecode or to "" to avoid any recoding" />
<Option name="DBF_DATE_LAST_UPDATE" type="string" description="Modification date to write in DBF header with YYYY-MM-DD format" />
<Option name="ADJUST_TYPE" type="boolean" description="Whether to read whole .dbf to adjust Real->Integer/Integer64 or Integer64->Integer field types if possible" default="NO" />
<Option name="ADJUST_GEOM_TYPE" type="string-select" description="Whether and how to adjust layer geometry type from actual shapes" default="FIRST_SHAPE">
<Value>NO</Value>
<Value>FIRST_SHAPE</Value>
<Value>ALL_SHAPES</Value>
</Option>
<Option name="AUTO_REPACK" type="boolean" description="Whether the shapefile should be automatically repacked when needed" default="YES" />
<Option name="DBF_EOF_CHAR" type="boolean" description="Whether to write the 0x1A end-of-file character in DBF files" default="YES" />
</OpenOptionList>
2.python脚本
In [3]: from osgeo import ogr
In [4]: shp_driver = ogr.GetDriverByName('ESRI Shapefile')
In [10]: shp_driver.GetMetadata()
# shp_driver.GetMetadata_Dict() 和 shp_driver.GetMetadata_List()也可以
Out[10]:
{'DCAP_CREATE': 'YES',
'DCAP_OPEN': 'YES',
'DCAP_VECTOR': 'YES',
'DCAP_VIRTUALIO': 'YES',
'DMD_CREATIONFIELDDATATYPES': 'Integer Integer64 Real String Date DateTime',
'DMD_CREATIONOPTIONLIST': '<CreationOptionList/>',
'DMD_EXTENSION': 'shp',
'DMD_EXTENSIONS': 'shp dbf',
'DMD_HELPTOPIC': 'drv_shape.html',
'DMD_LONGNAME': 'ESRI Shapefile',
'DMD_OPENOPTIONLIST': '<OpenOptionList> <Option name=\'ENCODING\' type=\'string\' description=\'to override the encoding interpretation of the DBF with any encoding supported by CPLRecode or to "" to avoid any recoding\'/> <Option name=\'DBF_DATE_LAST_UPDATE\' type=\'string\' description=\'Modification date to write in DBF header with YYYY-MM-DD format\'/> <Option name=\'ADJUST_TYPE\' type=\'boolean\' description=\'Whether to read whole .dbf to adjust Real->Integer/Integer64 or Integer64->Integer field types if possible\' default=\'NO\'/> <Option name=\'ADJUST_GEOM_TYPE\' type=\'string-select\' description=\'Whether and how to adjust layer geometry type from actual shapes\' default=\'FIRST_SHAPE\'> <Value>NO</Value> <Value>FIRST_SHAPE</Value> <Value>ALL_SHAPES</Value> </Option> <Option name=\'AUTO_REPACK\' type=\'boolean\' description=\'Whether the shapefile should be automatically repacked when needed\' default=\'YES\'/> <Option name=\'DBF_EOF_CHAR\' type=\'boolean\' description=\'Whether to write the 0x1A end-of-file character in DBF files\' default=\'YES\'/></OpenOptionList>', 'DS_LAYER_CREATIONOPTIONLIST': "<LayerCreationOptionList> <Option name='SHPT' type='string-select' description='type of shape' default='automatically detected'> <Value>POINT</Value> <Value>ARC</Value> <Value>POLYGON</Value> <Value>MULTIPOINT</Value> <Value>POINTZ</Value> <Value>ARCZ</Value> <Value>POLYGONZ</Value> <Value>MULTIPOINTZ</Value> <Value>POINTM</Value> <Value>ARCM</Value> <Value>POLYGONM</Value> <Value>MULTIPOINTM</Value> <Value>POINTZM</Value> <Value>ARCZM</Value> <Value>POLYGONZM</Value> <Value>MULTIPOINTZM</Value> <Value>MULTIPATCH</Value> <Value>NONE</Value> <Value>NULL</Value> </Option> <Option name='2GB_LIMIT' type='boolean' description='Restrict .shp and .dbf to 2GB' default='NO'/> <Option name='ENCODING' type='string' description='DBF encoding' default='LDID/87'/> <Option name='RESIZE' type='boolean' description='To resize fields to their optimal size.' default='NO'/> <Option name='SPATIAL_INDEX' type='boolean' description='To create a spatial index.' default='NO'/> <Option name='DBF_DATE_LAST_UPDATE' type='string' description='Modification date to write in DBF header with YYYY-MM-DD format'/> <Option name='AUTO_REPACK' type='boolean' description='Whether the shapefile should be automatically repacked when needed' default='YES'/> <Option name='DBF_EOF_CHAR' type='boolean' description='Whether to write the 0x1A end-of-file character in DBF files' default='YES'/></LayerCreationOptionList>"}
输出cpg文件
创建图层时可设置options相关属性,设置多个属性时用逗号“,”隔开,这样生成的shp数据会包含一个.cpg文件。
shp_layer = shp_ds.CreateLayer(shp_path,
srs=srs,
geom_type=ogr.wkbPoint,
options=["ENCODING=UTF-8"])