0
点赞
收藏
分享

微信扫一扫

基于python的批量excel坐标数据导入Postgresql空间数据库


有一份csv格式的坐标数据,需要将它导入Postgresql数据库,方便进行后期分析,坐标需要转换为geometry类型。首先我将它转换成了excel,方便python利用库进行读取。

数据概览

数据demo如下:

基于python的批量excel坐标数据导入Postgresql空间数据库_Python

包含一个起点坐标、一个终点坐标、和一个路线轨迹坐标串,用“#”好分割 。

数据库字段和格式如下图所示

基于python的批量excel坐标数据导入Postgresql空间数据库_Postgis_02

Python代码 

import xlrd
import psycopg2
import psycopg2.extras
import datetime

conn = psycopg2.connect(database="bike", user="postgres", password="postgres", host="127.0.0.1", port="5432")
cursor = conn.cursor()
print("Opened database successfully")
# 文件路径
data = xlrd.open_workbook("bike815821.xlsx")
sheet = data.sheet_by_index(0)

for i in range(sheet.nrows):
    tack = str(sheet.cell_value(i, 9))
    tack = tack.replace(",", " ")
    tack = tack.replace("#", ",")
    tack = tack.replace("\\", "")
    tack = tack.replace("n", "")
    print(i)
    date1 = xlrd.xldate_as_tuple(sheet.cell(i, 3).value, 0)
    value1 = str(datetime.datetime(*date1))
    date2 = xlrd.xldate_as_tuple(sheet.cell(i, 6).value, 0)
    value2 = str(datetime.datetime(*date2))
    date3 = xlrd.xldate_as_tuple(sheet.cell(i, 10).value, 0)
    value3 = str(datetime.datetime(*date3))
    point1 = (",st_geometryfromtext('POINT(" + str(sheet.cell_value(i, 4)) + " " + str(
        sheet.cell_value(i, 5)) + ")',4326)")
    point2 = (",st_geometryfromtext('POINT(" + str(sheet.cell_value(i, 7)) + " " + str(
        sheet.cell_value(i, 8)) + ")',4326)")
    line = (",st_geometryfromtext('LINESTRING(" + tack + ")',4326),")
    sql = (
            "insert into travel1(orderid, bikeid, userid, start_time, end_time, start_location, end_location, track, just_date) "
            + "values (" + str(sheet.cell_value(i, 0)) + ", "
            + str(sheet.cell_value(i, 1)) + ","
            + str(sheet.cell_value(i, 2)) + ",'" + value1 + "','"
            + value2 + "'" + point1 + point2 + line
            + "'" + value3 + "');")
    print(sql)
    cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()

结果

大约24万条数据,几分钟就完事了,Python的运行效率还是非常高的。最终结果展示如下图所示:

基于python的批量excel坐标数据导入Postgresql空间数据库_空间数据_03

预览一下 

基于python的批量excel坐标数据导入Postgresql空间数据库_空间数据_04

举报

相关推荐

0 条评论