python批量導入shp至postgresql數據庫中

原文地址:http://www.kingpika.top:5599/post/79

一、準備好數據並組織成以下格式:


點線面格式均可

二、代碼(需要安裝相應的庫)

#coding:utf-8
import geopandas as gpd
import pandas as pd
from sqlalchemy import create_engine
from geoalchemy2 import Geometry,WKTElement
import numpy as np
import os
import re
import json
from osgeo import ogr
#數據寫入函數:
def write_gis(path,engine):
    geoType=getGeoTypeFromDir(path)
    if(geoType):
        map_data = gpd.GeoDataFrame.from_file(path)
        map_data['geometry'] = map_data['geometry'].apply(lambda x: WKTElement(x.wkt,3857))
        # map_data.drop(['center','parent'], axis = 1, inplace=True)
        map_data.to_sql(
        name  = re.split('\\.',path)[0],
        con   = engine,
        if_exists= 'replace',
        dtype = {'geometry':Geometry(geometry_type =geoType,srid = 3857)}
        )
        return None

#創建批量任務
def to_do(file_path,username,password,dbname):
    os.chdir(file_path)
    link = "postgresql://{0}:{1}@localhost:3351/{2}".format(username,password,dbname)
    print(file_path)
    engine = create_engine(link,encoding = 'utf-8')
    file_list = os.listdir(file_path)
    print(file_list)
    map(lambda x: write_gis(x,engine),file_list)
    return None
#從文件夾中讀取shp,獲得其類型
def getGeoTypeFromDir(dirPath):![]()
    """從文件夾中讀取shp,獲得其類型"""
    # os.path.abspath(path)
    if os.path.isdir(dirPath):
        file_list = os.listdir(dirPath)
        for file in file_list:
            ext=os.path.splitext(file)[1]
            if(ext=='.shp'):
                driver = ogr.GetDriverByName('ESRI Shapefile')
                file=os.path.abspath(file)#返回文件的絕對路徑,這裏返回的是錯的,缺少了上級文件夾
                names=os.path.split(file)
                file=os.path.join(names[0],dirPath,names[1])
                dataSource = driver.Open(file,0)
                layer = dataSource.GetLayer(0)
                feat = layer.GetFeature(0)
                geom = feat.GetGeometryRef()
                geoCode=geom.GetGeometryType()
                return deGeoTypeCode(geoCode)
    return None
#解譯ogr的geometry code
def deGeoTypeCode(code):
    """解譯ogr的geometry code"""
    if code==1:
        return 'POINT'
    elif code==2:
        return 'LINESTRING'
    elif code==3:
        return 'POLYGON'
    return None

#執行任務計劃
if __name__ == '__main__':
    file_path = '/Users/jinming/Desktop/multipleImport/3857shp'
    username = 'postgres'
    password = 'pwd'
    dbname = 'gistest'
    to_do(file_path,username,password,dbname)
    print('DONE')

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章