shapefile轉(zhuǎn)換geojson
創(chuàng)新互聯(lián)建站于2013年開始,先為印臺等服務建站,印臺等地企業(yè),進行企業(yè)商務咨詢服務。為印臺企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。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')