1
+ # -*- coding: utf-8 -*-
2
+
3
+ """
4
+ ***************************************************************************
5
+ ogr2ogrtabletopostgislist.py
6
+ ---------------------
7
+ Date : November 2012
8
+ Copyright : (C) 2012 by Victor Olaya
9
+ Email : volayaf at gmail dot com
10
+ ***************************************************************************
11
+ * *
12
+ * This program is free software; you can redistribute it and/or modify *
13
+ * it under the terms of the GNU General Public License as published by *
14
+ * the Free Software Foundation; either version 2 of the License, or *
15
+ * (at your option) any later version. *
16
+ * *
17
+ ***************************************************************************
18
+ """
19
+
20
+ __author__ = 'Victor Olaya'
21
+ __date__ = 'November 2012'
22
+ __copyright__ = '(C) 2012, Victor Olaya'
23
+
24
+ # This will get replaced with a git SHA1 when you do a git archive
25
+
26
+ __revision__ = '$Format:%H$'
27
+
28
+ from PyQt4 .QtCore import QSettings
29
+
30
+ from processing .core .parameters import ParameterVector
31
+ from processing .core .parameters import ParameterString
32
+ from processing .core .parameters import ParameterTable
33
+ from processing .core .parameters import ParameterCrs
34
+ from processing .core .parameters import ParameterSelection
35
+ from processing .core .parameters import ParameterBoolean
36
+ from processing .core .parameters import ParameterExtent
37
+ from processing .core .parameters import ParameterTableField
38
+
39
+ from processing .tools .system import isWindows
40
+
41
+ from processing .algs .gdal .OgrAlgorithm import OgrAlgorithm
42
+ from processing .algs .gdal .GdalUtils import GdalUtils
43
+
44
+ class Ogr2OgrTableToPostGisList (OgrAlgorithm ):
45
+
46
+ DATABASE = 'DATABASE'
47
+ INPUT_LAYER = 'INPUT_LAYER'
48
+ HOST = 'HOST'
49
+ PORT = 'PORT'
50
+ USER = 'USER'
51
+ DBNAME = 'DBNAME'
52
+ PASSWORD = 'PASSWORD'
53
+ SCHEMA = 'SCHEMA'
54
+ TABLE = 'TABLE'
55
+ PK = 'PK'
56
+ PRIMARY_KEY = 'PRIMARY_KEY'
57
+ WHERE = 'WHERE'
58
+ GT = 'GT'
59
+ OVERWRITE = 'OVERWRITE'
60
+ APPEND = 'APPEND'
61
+ ADDFIELDS = 'ADDFIELDS'
62
+ LAUNDER = 'LAUNDER'
63
+ SKIPFAILURES = 'SKIPFAILURES'
64
+ PRECISION = 'PRECISION'
65
+ OPTIONS = 'OPTIONS'
66
+
67
+ def dbConnectionNames (self ):
68
+ settings = QSettings ()
69
+ settings .beginGroup ('/PostgreSQL/connections/' )
70
+ return settings .childGroups ()
71
+
72
+ def defineCharacteristics (self ):
73
+ self .name = 'Import layer/table as table into PostGIS database (available connections)'
74
+ self .group = '[OGR] Miscellaneous'
75
+ self .DB_CONNECTIONS = self .dbConnectionNames ()
76
+ self .addParameter (ParameterSelection (self .DATABASE ,
77
+ self .tr ('Database (connection name)' ), self .DB_CONNECTIONS ))
78
+ self .addParameter (ParameterTable (self .INPUT_LAYER ,
79
+ self .tr ('Input layer' )))
80
+ self .addParameter (ParameterString (self .SCHEMA ,
81
+ self .tr ('Schema name' ), 'public' , optional = True ))
82
+ self .addParameter (ParameterString (self .TABLE ,
83
+ self .tr ('Table name, leave blank to use input name' ),
84
+ '' , optional = True ))
85
+ self .addParameter (ParameterString (self .PK ,
86
+ self .tr ('Primary key' ), 'id' , optional = True ))
87
+ self .addParameter (ParameterTableField (self .PRIMARY_KEY ,
88
+ self .tr ('Primary key (existing field, used if the above option is left empty)' ), self .INPUT_LAYER , optional = True ))
89
+ self .addParameter (ParameterString (self .WHERE ,
90
+ self .tr ('Select features using a SQL "WHERE" statement (Ex: column="value")' ),
91
+ '' , optional = True ))
92
+ self .addParameter (ParameterString (self .GT ,
93
+ self .tr ('Group N features per transaction (Default: 20000)' ),
94
+ '' , optional = True ))
95
+ self .addParameter (ParameterBoolean (self .OVERWRITE ,
96
+ self .tr ('Overwrite existing table' ), True ))
97
+ self .addParameter (ParameterBoolean (self .APPEND ,
98
+ self .tr ('Append to existing table' ), False ))
99
+ self .addParameter (ParameterBoolean (self .ADDFIELDS ,
100
+ self .tr ('Append and add new fields to existing table' ), False ))
101
+ self .addParameter (ParameterBoolean (self .LAUNDER ,
102
+ self .tr ('Do not launder columns/table names' ), False ))
103
+ self .addParameter (ParameterBoolean (self .SKIPFAILURES ,
104
+ self .tr ('Continue after a failure, skipping the failed record' ),
105
+ False ))
106
+ self .addParameter (ParameterBoolean (self .PRECISION ,
107
+ self .tr ('Keep width and precision of input attributes' ),
108
+ True ))
109
+ self .addParameter (ParameterString (self .OPTIONS ,
110
+ self .tr ('Additional creation options' ), '' , optional = True ))
111
+
112
+ def processAlgorithm (self , progress ):
113
+ connection = self .DB_CONNECTIONS [self .getParameterValue (self .DATABASE )]
114
+ settings = QSettings ()
115
+ mySettings = '/PostgreSQL/connections/' + connection
116
+ dbname = settings .value (mySettings + '/database' )
117
+ user = settings .value (mySettings + '/username' )
118
+ host = settings .value (mySettings + '/host' )
119
+ port = settings .value (mySettings + '/port' )
120
+ password = settings .value (mySettings + '/password' )
121
+ inLayer = self .getParameterValue (self .INPUT_LAYER )
122
+ ogrLayer = self .ogrConnectionString (inLayer )[1 :- 1 ]
123
+ schema = unicode (self .getParameterValue (self .SCHEMA ))
124
+ schemastring = "-lco SCHEMA=" + schema
125
+ table = unicode (self .getParameterValue (self .TABLE ))
126
+ pk = unicode (self .getParameterValue (self .PK ))
127
+ pkstring = "-lco FID=" + pk
128
+ primary_key = self .getParameterValue (self .PRIMARY_KEY )
129
+ where = unicode (self .getParameterValue (self .WHERE ))
130
+ wherestring = "-where '" + where + "'"
131
+ gt = unicode (self .getParameterValue (self .GT ))
132
+ overwrite = self .getParameterValue (self .OVERWRITE )
133
+ append = self .getParameterValue (self .APPEND )
134
+ addfields = self .getParameterValue (self .ADDFIELDS )
135
+ launder = self .getParameterValue (self .LAUNDER )
136
+ launderstring = "-lco LAUNDER=NO"
137
+ skipfailures = self .getParameterValue (self .SKIPFAILURES )
138
+ precision = self .getParameterValue (self .PRECISION )
139
+ options = unicode (self .getParameterValue (self .OPTIONS ))
140
+
141
+ arguments = []
142
+ arguments .append ('-progress' )
143
+ arguments .append ('--config PG_USE_COPY YES' )
144
+ arguments .append ('-f' )
145
+ arguments .append ('PostgreSQL' )
146
+ arguments .append ('PG:"host=' )
147
+ arguments .append (host )
148
+ arguments .append ('port=' )
149
+ arguments .append (port )
150
+ if len (dbname ) > 0 :
151
+ arguments .append ('dbname=' + dbname )
152
+ if len (password ) > 0 :
153
+ arguments .append ('password=' + password )
154
+ arguments .append ('user=' + user + '"' )
155
+ arguments .append (ogrLayer )
156
+ arguments .append ('-nlt NONE' )
157
+ arguments .append (self .ogrLayerName (inLayer ))
158
+ if launder :
159
+ arguments .append (launderstring )
160
+ if append :
161
+ arguments .append ('-append' )
162
+ if addfields :
163
+ arguments .append ('-addfields' )
164
+ if overwrite :
165
+ arguments .append ('-overwrite' )
166
+ if len (schema ) > 0 :
167
+ arguments .append (schemastring )
168
+ if len (pk ) > 0 :
169
+ arguments .append (pkstring )
170
+ elif primary_key != None :
171
+ arguments .append ("-lco FID=" + primary_key )
172
+ if len (table ) > 0 :
173
+ arguments .append ('-nln' )
174
+ arguments .append (table )
175
+ if skipfailures :
176
+ arguments .append ('-skipfailures' )
177
+ if where :
178
+ arguments .append (wherestring )
179
+ if len (gt ) > 0 :
180
+ arguments .append ('-gt' )
181
+ arguments .append (gt )
182
+ if precision is False :
183
+ arguments .append ('-lco PRECISION=NO' )
184
+ if len (options ) > 0 :
185
+ arguments .append (options )
186
+
187
+ commands = []
188
+ if isWindows ():
189
+ commands = ['cmd.exe' , '/C ' , 'ogr2ogr.exe' ,
190
+ GdalUtils .escapeAndJoin (arguments )]
191
+ else :
192
+ commands = ['ogr2ogr' , GdalUtils .escapeAndJoin (arguments )]
193
+
194
+ GdalUtils .runGdal (commands , progress )
0 commit comments