任务说明:需要将shp文件转换为json坐标数据文件,然后加载到地图中,使用python中的pyshp批量完成
第一步:安装pyshp
pip install pyshp
第二步:编写代码
import os import codecs import shutil import shapefile def shp2json(fn_in, fn_out): shps = shapefile.Reader(fn_in) # print(shps.shapeRecord()) i_len = len(shps.shapes()) print(shps.shpLength) print('len:%s' % i_len) for i_index in range(i_len): fn_out_temp = fn_out if i_len > 1: fn_out_temp = fn_out[:-5] + '_' + str(i_index + 1) + '.json' if os.path.exists(fn_out_temp): print('find:%s' % fn_out_temp) s = '{"data":[\n%s\n]}' s_data = '' r = shps.shape(i_index) print('shapeTypeName:%s' % r.shapeTypeName) ls_p = r.points print('point count:%s' % len(ls_p)) i = -1 for p in ls_p: i += 1 # print(p[0]) if i > 0: s_data += ',\n' s_data += '[%s,%s]' % (p[0], p[1]) s = s % s_data f = open(fn_out_temp, 'w') f.write(s) f.close() print('save:%s' % fn_out_temp) def shp2json_dir(dir_in, dir_out): ls = os.listdir(dir_in) for l in ls: fn_in = '%s/%s' % (dir_in, l) if fn_in.lower().endswith('.shp'): # print(fn_in) fn_out = '%s/%s.json' % (dir_out, l[:-4]) if os.path.exists(fn_out): print('find:%s' % fn_out) continue shp2json(fn_in, fn_out) def check_dir(dir_in, dir_out): ls = os.listdir(dir_in) for l in ls: dir2 = '%s/%s' % (dir_in, l) # print(dir2) if os.path.isdir(dir2): check_dir(dir2, dir_out) if os.path.isfile(dir2): if (dir2.endswith('_50km.shp') or dir2.endswith('_100km.shp')) and '_temp_' not in dir2: d1, f1 = os.path.split(dir2) fn_out = '%s/%s' % (dir_out, f1.replace('.shp', '.json')) shp2json(dir2, fn_out) # 遍历目录,shp转json def auto(dir_in, dir_out): if not os.path.exists(dir_out): os.makedirs(dir_out) check_dir(dir_in, dir_out) def create_index(dir_in, dir_out): if not os.path.exists(dir_out): os.makedirs(dir_out) f = codecs.open('area.txt', 'r', 'utf-8') ls = f.readlines() f.close() ls2 = os.listdir(dir_in) for l in ls: r = l.split('\t') if len(r) < 2: continue tit = r[0] code = r[1].strip() ls_50 = [] ls_100 = [] b_find = False for l2 in ls2: if tit in l2: b_find = True if '_50km' in l2: fn_in = '%s/%s' % (dir_in, l2) fn_out = '%s/%s_%s' % (dir_out, code, l2) shutil.copyfile(fn_in, fn_out) fn = '%s_%s' % (code, l2) ls_50.append(fn) if '_100km' in l2: fn_in = '%s/%s' % (dir_in, l2) fn_out = '%s/%s_%s' % (dir_out, code, l2) shutil.copyfile(fn_in, fn_out) fn = '%s_%s' % (code, l2) ls_100.append(fn) print('%s %s' % (b_find, tit)) if not b_find: continue print(ls_50) print(ls_100) s1 = '' s2 = '' for l in ls_50: if s1 != '': s1 += ',\n' s1 += '"%s"' % l for l in ls_100: if s2 != '': s2 += ',\n' s2 += '"%s"' % l fn = '%s/%s_polygon.json' % (dir_out, code) s_json = '''{ "title": "%s", "data": [ { "color": "#FF4500", "file": [], "info": "红色:责任区" }, { "color": "#FF8C00", "file": [ %s ], "info": "橙色:警戒区" }, { "color": "#0000FF", "file": [ %s ], "info": "蓝色:监视区" } ] }''' % (tit, s1, s2) f = codecs.open(fn, 'w', 'utf-8') f.write(s_json) f.close() def del_file(dir): ls = [ '西宁市城区线图_50km.json', '西宁市城区线图_100km.json', '海南州非地市级线图_50km.json', '海南州非地市级线图_100km.json', '茫崖县级线图_50km.json', '茫崖县级线图_100km.json', '茫崖市边界线图_50km.json', '茫崖市边界线图_100km.json', '茫崖市县边界线图_50km.json', '茫崖市县边界线图_100km.json', '都兰县线图_50km.json', '都兰县线图_100km.json', '格尔木市线图_50km.json', '格尔木市线图_100km.json', '海东市市级线图_50km.json', '海东市市级线图_100km.json', '海南州市级线图_50km.json', '海南州市级线图_100km.json', '贵南县级线图_50km.json', '贵南县级线图_100km.json', ] for l in ls: fn = '%s/%s' % (dir, l) if os.path.exists(fn): os.remove(fn) print('delete:%s' % fn) else: print('not find:%s' % fn) if __name__ == '__main__': # fn_in ='../data/shp/西宁市市级线图.shp' # fn_out = '../data/json/xn.json' # shp2json(fn_in, fn_out) # dir_in = '../data/shp' # dir_out = '../data/json' # shp2json_dir(dir_in, dir_out) # shp转json dir_in = 'D:/arcgisfile_青海/线图' dir_out = '../data/json/all' auto(dir_in, dir_out) # 先删除多余文件 del_file(dir_out) # 生成索引文件 dir_in = '../data/json/all' dir_out = '../data/json/polygon' create_index(dir_in, dir_out)
地图加载数据后的效果: