Python中shapefile轉換geojson的示例
更新時間:2019年01月03日 11:46:16 作者:staHuri
今天小編就為大家分享一篇關于Python中shapefile轉換geojson的示例,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
shapefile轉換geojson
import shapefile
import codecs
from json import dumps
# read the shapefile
def shp2geo(file="line出產(chǎn).shp"):
reader = shapefile.Reader(file)
fields = reader.fields[1:]
field_names = [field[0] for field in fields]
buffer = []
for sr in reader.shapeRecords():
record = sr.record
record = [r.decode('gb2312', 'ignore') if isinstance(r, bytes)
else r for r in record]
atr = dict(zip(field_names, record))
geom = sr.shape.__geo_interface__
buffer.append(dict(type="Feature", geometry=geom, properties=atr))
# write the GeoJSON file
geojson = codecs.open(file.split('.')[0] + "-geo.json", "w", encoding="gb2312")
geojson.write(dumps({"type": "FeatureCollection", "features": buffer}, indent=2) + "\n")
geojson.close()
if __name__ == '__main__':
# import os
# for z,x,c in os.walk('.'):
# for zz in c:
# if zz.endswith(".shp"):
# shp2geo(zz)
# shp2geo(file='D.shp')
shp2geo(file='ttttttttttt.shp')
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
相關文章
Python+?Flask實現(xiàn)Mock?Server詳情
這篇文章主要介紹了Python+?Flask實現(xiàn)Mock?Server詳情,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
python通過elixir包操作mysql數(shù)據(jù)庫實例代碼
這篇文章主要介紹了python通過elixir包操作mysql數(shù)據(jù)庫,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
詳解使用pymysql在python中對mysql的增刪改查操作(綜合)
本篇文章主要介紹了使用pymysql在python中對mysql的增刪改查操作,通過pymysql向數(shù)據(jù)庫進行查刪增改,具有一定的參考價值,有興趣的可以了解一下。2017-01-01
Windows 下更改 jupyterlab 默認啟動位置的教程詳解
這篇文章主要介紹了Windows 下更改 jupyterlab 默認啟動位置,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Python+matplotlib實現(xiàn)填充螺旋實例
這篇文章主要介紹了Python+matplotlib實現(xiàn)填充螺旋實例,具有一定借鑒價值,需要的朋友可以參考下2018-01-01

