解決python ogr shp字段寫入中文亂碼的問題
首先,先確認一下你的字段值是不是亂碼,如果是,按照以下方法:
我的字段值是來自于一個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中會出現(xiàn)亂碼,使用如下方法解決:
首先,你需要用driver方法創(chuàng)建shp文件而不是直接用ogr.open:
driver=ogr.GetDriverByName("ESRI Shapefile")
ds =driver.CreateDataSource(shp_path)#打開要寫入的數(shù)據(jù)源
然后,在driver創(chuàng)建之前加入如下兩句:
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)#打開要寫入的數(shù)據(jù)源
if ds is None:
sys.exit('Could not open this folder!')
if ds.GetLayer('test_polygon'):
ds.DeleteLayer('test_polygon')#如果存在,就刪除該數(shù)據(jù)
feature0=a['features'][0]
geo = feature0.get("geometry")
geo_type = geo.get('type')#獲取圖層類型
properties = feature0.get("properties")
keys=properties.keys()#獲取字段名稱數(shù)組
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)#創(chuàng)建圖層
#接下來往圖層中寫入feature
for key in keys:
field_testfield = ogr.FieldDefn(key, ogr.OFTString)#創(chuàng)建字段
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)#創(chuàng)建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中ThreadPoolExecutor線程池和ProcessPoolExecutor進程池
這篇文章主要介紹了python中ThreadPoolExecutor線程池和ProcessPoolExecutor進程池,文章圍繞主題相關資料展開詳細的內容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06
Pytorch中torch.argmax()函數(shù)使用及說明
這篇文章主要介紹了Pytorch中torch.argmax()函數(shù)使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
Python+Tkinter實現(xiàn)股票K線圖的繪制
K線圖又稱蠟燭圖,常用說法是“K線”。K線是以每個分析周期的開盤價、最高價、最低價和收盤價繪制而成。本文將利用Python+Tkinter實現(xiàn)股票K線圖的繪制,需要的可以參考一下2022-08-08
在Python的Django框架的視圖中使用Session的方法
這篇文章主要介紹了在Python的Django框架的視圖中使用Session的方法,包括相關的設置測試Cookies的方法,需要的朋友可以參考下2015-07-07
在pycharm中使用git版本管理以及同步github的方法
今天小編就為大家分享一篇在pycharm中使用git版本管理以及同步github的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01

