2019-03-01 13:34:44 1861瀏覽
本篇文章扣丁學(xué)堂Python培訓(xùn)小編給讀者們分享一下Python中shapefile如何轉(zhuǎn)換geojson的,對Python開發(fā)技術(shù)感興趣或者是想要參加Python培訓(xùn)學(xué)習(xí)Python開發(fā)技術(shù)的小伙伴下面就隨小面來看一下Python中shapefile轉(zhuǎn)換geojson的示例吧。
shapefile轉(zhuǎn)換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')
最后想要了解更多關(guān)于Python方面內(nèi)容的小伙伴,請關(guān)注扣丁學(xué)堂Python培訓(xùn)官網(wǎng)、微信等平臺,扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育平臺為您提供權(quán)威的Python開發(fā)環(huán)境搭建視頻,Python培訓(xùn)后的前景無限,行業(yè)薪資和未來的發(fā)展會越來越好的,扣丁學(xué)堂老師精心推出的Python視頻直播課定能讓你快速掌握Python從入門到精通開發(fā)實(shí)戰(zhàn)技能??鄱W(xué)堂Python技術(shù)交流群:279521237。
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】
查看更多關(guān)于"Python開發(fā)資訊"的相關(guān)文章>