具备python开发环境后,使用geopandas库读取geojson文件,然后使用sqlalchemy库将数据入库到postgis中。
to_postgis方法中的schema默认为public
import geopandas as gpd
import os
from sqlalchemy import create_engineinputFolder = r'D:\项目\处理结果'
engine = create_engine("postgresql://username:password@ip:port/databasename")# 处理文件夹
def dealJsonFolder(inputFolder):# print(inputFolder)# 检查输入文件夹,如果不存在则返回if not os.path.exists(inputFolder):print("检查输入文件夹不存在!!!")returnfor item in os.listdir(inputFolder):# print(item)file_name, file_extension = os.path.splitext(item)# 如果是文件夹则递归执行if (file_extension == ''):dealJsonFolder(os.path.join(inputFolder, item))# 如果扩展名为geojson则执行下面的函数elif (file_extension.lower() == '.geojson'):file_full_name = os.path.join(inputFolder, item)json2Postgres(file_full_name, file_name)def json2Postgres(path, name):print(name, "准备入库")testjson = gpd.read_file(path)testjson.to_postgis(name=name, schema="geom_test", con=engine, if_exists="replace", chunksize=1000)#json2Postgres(r"D:\项目\处理\合并\node_zhutangzhen.geojson","node_zhutangzhen")
dealJsonFolder(inputFolder)
print("处理完成")