-
Notifications
You must be signed in to change notification settings - Fork 2
/
pgimport.py
37 lines (25 loc) · 1.13 KB
/
pgimport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import subprocess
import sys
import os
'''usage: pgimport path/to/fgdb layername database schema.tablename host user pass
This lets you import fgdbs into postgis tables that have views on them.
Uses an intermediate table to minimize downtime'''
fgdb, layer, db, target, host, user, password = sys.argv[1:]
env = os.environ.copy()
env['PGPASSWORD'] = password
sqlconn = 'psql -U%s -h %s -d %s ' % (user, host, db)
try:
targetschema,targettable = target.split(".")
except ValueError:
targetschema = "public"
targettable = target
temptable = "import.%s" % (targettable)
#ogr2ogr in overwrite mode to temp import table
ogrcmd = 'ogr2ogr -skipfailures -overwrite -f "PostgreSQL" PG:"host=%s dbname=%s user=%s password=%s" "%s" "%s" -nln "%s"' \
% (host, db, user, password, fgdb, layer, temptable)
print ogrcmd
subprocess.call(ogrcmd, shell=True)
#log the initial import into the log table, which will trigger postgres to finish the import
sqlcmd = sqlconn + '-c "INSERT INTO import.importlog (targetschema, tablename) VALUES (%s,%s);"' % (targetschema, targettable)
print sqlcmd
subprocess.call(sqlcmd, shell=True, env=env)