Python Scripts to ingest a shapefile into a PostgreSQL/PostGIS database utilizing shp2pgsql.exe on windows

前端 未结 1 1687
庸人自扰
庸人自扰 2021-02-10 09:09

I have a PostgreSQL database hosted on a Windows 2008 Server RT Virtual Machine (Yes I know it should be hosted on a Linux VM but this is what my organization has dictated it be

1条回答
  •  再見小時候
    2021-02-10 09:50

    Here are some modifications that should make thing work. Note that it would need further modification if you need to be notified if any of the commands fail. Note that it will fail for more than one shapefile, since a new_shp_table table will already exist until you have further logic to move or rename that table elsewhere, or to load it with a unique name.

    Also, note that PostgreSQL 8.4 will reach it's end-of-life later this year, so you might want to plan to upgrade to a more recent release before it's too late.

    import os, subprocess
    
    # Choose your PostgreSQL version here
    os.environ['PATH'] += r';C:\Program Files (x86)\PostgreSQL\8.4\bin'
    # http://www.postgresql.org/docs/current/static/libpq-envars.html
    os.environ['PGHOST'] = 'localhost'
    os.environ['PGPORT'] = '5432'
    os.environ['PGUSER'] = 'someuser'
    os.environ['PGPASSWORD'] = 'clever password'
    os.environ['PGDATABASE'] = 'geometry_database'
    
    base_dir = r"c:\shape_file_repository"
    full_dir = os.walk(base_dir)
    shapefile_list = []
    for source, dirs, files in full_dir:
        for file_ in files:
            if file_[-3:] == 'shp':
                shapefile_path = os.path.join(base_dir, file_)
                shapefile_list.append(shapefile_path)
    for shape_path in shapefile_list:
        cmds = 'shp2pgsql "' + shape_path + '" new_shp_table | psql '
        subprocess.call(cmds, shell=True)
    

    0 讨论(0)
提交回复
热议问题