Geojson文件转为shp

原json文件:
Geojson文件转为shp_第1张图片
json里面有多个属性,其中需要提取这三个:
Geojson文件转为shp_第2张图片

json转txt:

import os
import json
geojsonPath = r'D:\chenxiao\original_data'
outPath = r'D:\chenxiao\txt'
geojsonFiles = [o for o in os.listdir(geojsonPath) if o.endswith('.json') and 'GEDI' in o]
for file in geojsonFiles:
    with open(geojsonPath + '/' + file, 'r', encoding='utf8') as fp:
        data = json.load(fp)
        lonlat_num = 0
        Lon, Lat, PAI = [], [], []
        for item in data['features']:
            if item["properties"]["pai"] is not None:
                Lon.append(item["properties"]["Longitude"])
                Lat.append(item["properties"]["Latitude"])
                PAI.append(item["properties"]["pai"])
                lonlat_num += 1
            else:
                print("无”pai“字段,请检查文件类型!")
                break
    jsonFilename = file.split('.')
    with open(outPath + '/' + jsonFilename[0] + '.txt', 'w', encoding='utf8') as fp:
        fp.write("longtitude,latitude,pai\n")
        for l in range(0,lonlat_num-1):
            fp.write(("%.15f,%.15f,%.18f\n" %(Lon[l], Lat[l], PAI[l])))
        print("%s Finished!" % jsonFilename[0])

txt结果:
Geojson文件转为shp_第3张图片

txt转shp:

# -*-coding:utf-8-*-
import shapefile as shp
import os

def trans_point(folder, delimiter=','):
    # create a point shapefile
    filepath = os.listdir(folder)
    for i in filepath:
        print(i)
        filepath_save = 'D:\\chenxiao\\shp\\'+i     #输出
        filepath_input = folder+i

        output_shp = shp.Writer(filepath_save, shp.POINT)
        output_shp.autoBalance = 1

        output_shp.field('longitude', 'F', 15)  # float
        output_shp.field('latitude', 'F', 15)  # float
        output_shp.field('pai', 'F', 15, 8)  # z
        #output_shp.field('elevation', 'F', 15, 8)

        with open(filepath_input, 'r', encoding='utf-8') as txtfile:
            reader = []
            count = 0
            for line in txtfile.readlines():
                if count == 0:
                    count = 1
                    continue
                else:
                    curLine = line.strip().split(",")
                    floatLine = list(map(float, curLine))  # 这里使用的是map函数直接把数据转化成为float类型
                    reader.append(floatLine[:])

            for row in reader:

                if len(row)==0:
                    break
                else:
                    lng= float(row[0])

                    lat = float(row[1])
                    pai = float(row[2])
                    #elevation = float(row[3])
                    output_shp.point(lng,lat)
                    # add attribute data
                    #output_shp.record(lng,lat,pai,elevation)
                    output_shp.record(lng, lat, pai)
    print("#####finish####")


if __name__ == '__main__':
    folder = 'D:\\chenxiao\\txt\\' #输入文件
    trans_point(folder)

或者:
在下面这个网址上将json先转为csv文件,再由csv转为shp
json转csv网址
csv转shp:

# -*-coding:utf-8-*-
import shapefile as shp
import os

def trans_point(folder, delimiter=','):
    # create a point shapefile
    filepath = os.listdir(folder)
    for i in filepath:
        print(i)
        filepath_save = 'D:\\chenxiao\\shp\\'+i     #输出
        filepath_input = folder+i

        output_shp = shp.Writer(filepath_save, shp.POINT)
        output_shp.autoBalance = 1

        output_shp.field('longitude', 'F', 15)  # float
        output_shp.field('latitude', 'F', 15)  # float
        output_shp.field('pai', 'F', 15, 8)  # z
        #output_shp.field('elevation', 'F', 15, 8)

        with open(filepath_input, 'r', encoding='utf-8') as txtfile:
            reader = []
            count = 0
            for line in txtfile.readlines():
                if count == 0:
                    count = 1
                    continue
                else:
                    curLine = line.strip().split(",")
                    floatLine = list(map(float, curLine))  # 这里使用的是map函数直接把数据转化成为float类型
                    reader.append(floatLine[:])

            for row in reader:

                if len(row)==0:
                    break
                else:
                    lng= float(row[0])

                    lat = float(row[1])
                    pai = float(row[2])
                    #elevation = float(row[3])
                    output_shp.point(lng,lat)
                    # add attribute data
                    #output_shp.record(lng,lat,pai,elevation)
                    output_shp.record(lng, lat, pai)
    print("#####finish####")


if __name__ == '__main__':
    folder = 'D:\\chenxiao\\txt\\' #输入文件
    trans_point(folder)

你可能感兴趣的:(python,json,开发语言)