GDAL设置shapefile编码

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 &quot;&quot; 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-&gt;Integer/Integer64 or Integer64-&gt;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"])
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要将Shapefile格式文件转换为DWG格式,你可以使用GDAL库中的OGR(OpenGIS Simple Features Reference Implementation)功能。下面是使用GDAL进行转换的基本步骤: 1. 引用GDAL和OGR库:在C#项目中,首先需要引用GDAL和OGR库。你可以在代码文件中添加以下引用: ```csharp using OSGeo.GDAL; using OSGeo.OGR; ``` 2. 初始化GDAL:在程序的入口点或初始化代码中,调用GDAL的初始化方法: ```csharp Gdal.AllRegister(); ``` 3. 打开Shapefile数据源:使用OGR打开Shapefile数据源: ```csharp DataSource sourceDS = Ogr.Open("path/to/your/shapefile", 0); ``` 4. 创建DWG文件数据源:使用OGR创建DWG文件数据源: ```csharp Driver dwgDriver = Ogr.GetDriverByName("DWG"); DataSource targetDS = dwgDriver.CreateDataSource("path/to/your/dwgfile", null); ``` 5. 复制图层:遍历Shapefile数据源中的图层,并将其复制到DWG数据源中: ```csharp for (int i = 0; i < sourceDS.GetLayerCount(); i++) { Layer sourceLayer = sourceDS.GetLayerByIndex(i); Layer targetLayer = targetDS.CreateLayer(sourceLayer.GetName(), sourceLayer.GetSpatialRef(), sourceLayer.GetGeomType(), null); FeatureDefn featureDefn = sourceLayer.GetLayerDefn(); for (int j = 0; j < featureDefn.GetFieldCount(); j++) { FieldDefn fieldDefn = featureDefn.GetFieldDefn(j); targetLayer.CreateField(fieldDefn, 1); } sourceLayer.ResetReading(); Feature feature; while ((feature = sourceLayer.GetNextFeature()) != null) { Feature targetFeature = new Feature(featureDefn); targetFeature.SetFrom(feature); targetLayer.CreateFeature(targetFeature); feature.Dispose(); targetFeature.Dispose(); } sourceLayer.Dispose(); targetLayer.Dispose(); } ``` 6. 关闭数据源:在使用完数据后,需要关闭数据源以释放资源: ```csharp sourceDS.Dispose(); targetDS.Dispose(); ``` 以上是使用GDAL库将Shapefile格式文件转换为DWG格式的基本步骤。请注意,转换过程中可能需要进行投影转换或其他额外的操作,具体取决于你的数据和需求。你可以根据需要进行进一步的处理和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

llc的足迹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值