解决python ogr shp字段写入中文乱码的问题


Posted in Python onDecember 31, 2018

首先,先确认一下你的字段值是不是乱码,如果是,按照以下方法:

我的字段值是来自于一个geojson字符串,我在对它解析时做了如下处理:

properties = fea.get("properties")
pro_json=json.dumps(properties)
pro_json.replace('u\'','\'')#将unicode编码转化为中文先处理一下
pro_json=pro_json.decode("unicode-escape") #将unicode编码转化为中文
properties=json.loads(pro_json)

这样即可消除字段值中的中文乱码。

字段值没有乱码了,可是这样写入shp,shp中会出现乱码,使用如下方法解决:

首先,你需要用driver方法创建shp文件而不是直接用ogr.open:

driver=ogr.GetDriverByName("ESRI Shapefile")
ds =driver.CreateDataSource(shp_path)#打开要写入的数据源

然后,在driver创建之前加入如下两句:

gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
gdal.SetConfigOption("SHAPE_ENCODING", "GBK")

成了。

源码如下:

def create_shp_with_geoJson2(a,shp_path):
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
gdal.SetConfigOption("SHAPE_ENCODING", "GBK")
driver=ogr.GetDriverByName("ESRI Shapefile")
ds =driver.CreateDataSource(shp_path)#打开要写入的数据源
if ds is None:
sys.exit('Could not open this folder!')
if ds.GetLayer('test_polygon'):
ds.DeleteLayer('test_polygon')#如果存在,就删除该数据
feature0=a['features'][0]
geo = feature0.get("geometry")
geo_type = geo.get('type')#获取图层类型
properties = feature0.get("properties")
keys=properties.keys()#获取字段名称数组
if geo_type=='Polygon' or 'MultiPolygon':
ogr_type=ogr.wkbPolygon
else:
if geo_type=='Point':
ogr_type=ogr.wkbPoint
else:
if geo_type=='LineString' or 'MultiLineString':
ogr_type=ogr.wkbLineString
out_lyr=ds.CreateLayer('test_polygon',None,ogr_type)#创建图层
#接下来往图层中写入feature
for key in keys:
field_testfield = ogr.FieldDefn(key, ogr.OFTString)#创建字段
field_testfield.SetWidth(254)
out_lyr.CreateField(field_testfield)
for fea in a['features']:
geometry_json=fea.get("geometry")
properties = fea.get("properties")
pro_json=json.dumps(properties)
pro_json.replace('u\'','\'')#将unicode编码转化为中文先处理一下
pro_json=pro_json.decode("unicode-escape") #将unicode编码转化为中文
properties=json.loads(pro_json)
geom=ogr.CreateGeometryFromJson(str(geometry_json))
out_defn=out_lyr.GetLayerDefn()
out_feat=ogr.Feature(out_defn)
out_feat.SetGeometry(geom)#创建geometry
for i in range(len(keys)):
value=properties.get(keys[i])#获取属性值
print(value)
out_feat.SetField(i,value)
out_lyr.CreateFeature(out_feat)#在图层中插入该要素
if __name__ == '__main__':
create_shp_with_geoJson2(a,'web')

以上这篇解决python ogr shp字段写入中文乱码的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python写的一个文本编辑器
Jan 23 Python
python控制台显示时钟的示例
Feb 24 Python
数据挖掘之Apriori算法详解和Python实现代码分享
Nov 07 Python
深入解析Python中的WSGI接口
May 11 Python
Python matplotlib绘图可视化知识点整理(小结)
Mar 16 Python
Tensorflow 合并通道及加载子模型的方法
Jul 26 Python
python爬虫框架scrapy实现模拟登录操作示例
Aug 02 Python
python线程的几种创建方式详解
Aug 29 Python
python实现简单俄罗斯方块
Mar 13 Python
浅谈python锁与死锁问题
Aug 14 Python
使paramiko库执行命令时在给定的时间强制退出功能的实现
Mar 03 Python
详解Django的MVT设计模式
Apr 29 Python
Python图像处理之gif动态图的解析与合成操作详解
Dec 30 #Python
python爬虫获取小区经纬度以及结构化地址
Dec 30 #Python
python实现播放音频和录音功能示例代码
Dec 30 #Python
利用python实现简易版的贪吃蛇游戏(面向python小白)
Dec 30 #Python
python中partial()基础用法说明
Dec 30 #Python
python读取各种文件数据方法解析
Dec 29 #Python
python 读取鼠标点击坐标的实例
Dec 29 #Python
You might like
Php中用PDO查询Mysql来避免SQL注入风险的方法
2013/04/25 PHP
调整PHP的性能
2013/10/30 PHP
php向js函数传参的几种方法
2014/08/10 PHP
ThinkPHP连接数据库及主从数据库的设置教程
2014/08/22 PHP
php post大量数据时发现数据丢失问题解决方法
2015/06/20 PHP
php使用SAE原生Mail类实现各种类型邮件发送的方法
2016/10/10 PHP
PHP asXML()函数讲解
2019/02/03 PHP
Valerio 发布了 Mootools
2006/09/23 Javascript
JavaScript触发器详解
2007/03/10 Javascript
JavaScript 函数调用规则
2009/09/14 Javascript
extjs 学习笔记(二) Ext.Element类
2009/10/13 Javascript
JavaScript测试工具之Karma-Jasmine的安装和使用详解
2015/12/03 Javascript
JS生成某个范围的随机数【四种情况详解】
2016/04/20 Javascript
js实现数组和对象的深浅拷贝
2017/09/30 Javascript
angular json对象push到数组中的方法
2018/02/27 Javascript
详解javascript中的babel到底是什么
2018/06/21 Javascript
微信实现自动跳转到用其他浏览器打开指定APP下载
2019/02/15 Javascript
jQuery模拟html下拉多选框的原生实现方法示例
2019/05/30 jQuery
vue element 关闭当前tab 跳转到上一路由操作
2020/07/22 Javascript
vue 使用 sortable 实现 el-table 拖拽排序功能
2020/12/26 Vue.js
[44:50]DOTA2上海特级锦标赛B组小组赛#2 VG VS Fnatic第二局
2016/02/26 DOTA
[07:06]2018DOTA2国际邀请赛寻真——卫冕冠军Team Liquid
2018/08/10 DOTA
Python 文件读写操作实例详解
2014/03/12 Python
Python中的ceil()方法使用教程
2015/05/14 Python
深入了解Python中pop和remove的使用方法
2018/01/09 Python
python导出hive数据表的schema实例代码
2018/01/22 Python
Django之创建引擎索引报错及解决详解
2019/07/17 Python
详解Python修复遥感影像条带的两种方式
2020/02/23 Python
CSS3截取字符串实例代码【推荐】
2018/06/07 HTML / CSS
canvas之自定义头像功能实现代码示例
2017/09/29 HTML / CSS
马来西亚奢侈品牌购物商城:Valiram 247
2020/09/29 全球购物
应聘编辑职位自荐信范文
2014/01/05 职场文书
2015初一年级组工作总结
2015/07/24 职场文书
Python Django框架介绍之模板标签及模板的继承
2021/05/27 Python
python 爬取京东指定商品评论并进行情感分析
2021/05/27 Python
SpringBoot系列之MongoDB Aggregations用法详解
2022/02/12 MongoDB