Skip to content

Commit dab3092

Browse files
author
Giovanni Manghi
committedFeb 1, 2015
better and new fixes for processing/ogr import in postgis tools
1 parent 6e94688 commit dab3092

File tree

2 files changed

+68
-14
lines changed

2 files changed

+68
-14
lines changed
 

‎python/plugins/processing/algs/gdal/ogr2ogrtopostgis.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,22 @@
2525

2626
__revision__ = '$Format:%H$'
2727

28+
import os
29+
30+
from PyQt4.QtCore import *
31+
from PyQt4.QtGui import *
32+
33+
from qgis.core import *
34+
2835
from processing.core.parameters import ParameterVector
2936
from processing.core.parameters import ParameterString
3037
from processing.core.parameters import ParameterCrs
3138
from processing.core.parameters import ParameterSelection
3239
from processing.core.parameters import ParameterBoolean
3340
from processing.core.parameters import ParameterExtent
41+
from processing.core.parameters import ParameterTableField
3442

35-
from processing.tools.system import isWindows
43+
from processing.tools.system import *
3644

3745
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
3846
from processing.algs.gdal.GdalUtils import GdalUtils
@@ -53,6 +61,7 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
5361
SCHEMA = 'SCHEMA'
5462
TABLE = 'TABLE'
5563
PK = 'PK'
64+
PRIMARY_KEY = 'PRIMARY_KEY'
5665
GEOCOLUMN = 'GEOCOLUMN'
5766
DIM = 'DIM'
5867
DIMLIST = ['2','3']
@@ -68,6 +77,8 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
6877
LAUNDER = 'LAUNDER'
6978
INDEX = 'INDEX'
7079
SKIPFAILURES = 'SKIPFAILURES'
80+
PRECISION = 'PRECISION'
81+
PROMOTETOMULTI = 'PROMOTETOMULTI'
7182
OPTIONS = 'OPTIONS'
7283

7384
def defineCharacteristics(self):
@@ -76,7 +87,7 @@ def defineCharacteristics(self):
7687
self.addParameter(ParameterVector(self.INPUT_LAYER,
7788
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY], False))
7889
self.addParameter(ParameterSelection(self.GTYPE,
79-
self.tr('Output geometry type'), self.GEOMTYPE, 5))
90+
self.tr('Output geometry type'), self.GEOMTYPE, 0))
8091
self.addParameter(ParameterCrs(self.A_SRS,
8192
self.tr('Assign an output CRS'), ''))
8293
self.addParameter(ParameterCrs(self.T_SRS,
@@ -99,7 +110,9 @@ def defineCharacteristics(self):
99110
self.tr('Table name, leave blank to use input name'),
100111
'', optional=True))
101112
self.addParameter(ParameterString(self.PK,
102-
self.tr('Primary Key'), 'id', optional=True))
113+
self.tr('Primary key (new field)'), 'id', optional=True))
114+
self.addParameter(ParameterTableField(self.PRIMARY_KEY,
115+
self.tr('Primary key (existing field, used if the above option is left empty)'), self.INPUT_LAYER, optional=True))
103116
self.addParameter(ParameterString(self.GEOCOLUMN,
104117
self.tr('Geometry column name'), 'geom', optional=True))
105118
self.addParameter(ParameterSelection(self.DIM,
@@ -133,6 +146,12 @@ def defineCharacteristics(self):
133146
self.tr('Do not create spatial index'), False))
134147
self.addParameter(ParameterBoolean(self.SKIPFAILURES,
135148
self.tr('Continue after a failure, skipping the failed feature'), False))
149+
self.addParameter(ParameterBoolean(self.PROMOTETOMULTI,
150+
self.tr('Promote to Multipart'),
151+
True))
152+
self.addParameter(ParameterBoolean(self.PRECISION,
153+
self.tr('Keep width and precision of input attributes'),
154+
True))
136155
self.addParameter(ParameterString(self.OPTIONS,
137156
self.tr('Additional creation options'), '', optional=True))
138157

@@ -152,6 +171,7 @@ def processAlgorithm(self, progress):
152171
table = unicode(self.getParameterValue(self.TABLE))
153172
pk = unicode(self.getParameterValue(self.PK))
154173
pkstring = "-lco FID="+pk
174+
primary_key = self.getParameterValue(self.PRIMARY_KEY)
155175
geocolumn = unicode(self.getParameterValue(self.GEOCOLUMN))
156176
geocolumnstring = "-lco GEOMETRY_NAME="+geocolumn
157177
dim = self.DIMLIST[self.getParameterValue(self.DIM)]
@@ -172,6 +192,8 @@ def processAlgorithm(self, progress):
172192
index = self.getParameterValue(self.INDEX)
173193
indexstring = "-lco SPATIAL_INDEX=OFF"
174194
skipfailures = self.getParameterValue(self.SKIPFAILURES)
195+
promotetomulti = self.getParameterValue(self.PROMOTETOMULTI)
196+
precision = self.getParameterValue(self.PRECISION)
175197
options = unicode(self.getParameterValue(self.OPTIONS))
176198

177199
arguments = []
@@ -182,9 +204,9 @@ def processAlgorithm(self, progress):
182204
arguments.append('PG:"host='+host)
183205
arguments.append('port='+port)
184206
if len(dbname) > 0:
185-
arguments.append('dbname='+dbname)
207+
arguments.append('dbname='+dbname)
186208
if len(password) > 0:
187-
arguments.append('password='+password)
209+
arguments.append('password='+password)
188210
arguments.append('user='+user+'"')
189211
arguments.append(dimstring)
190212
arguments.append(ogrLayer)
@@ -208,6 +230,8 @@ def processAlgorithm(self, progress):
208230
arguments.append(geocolumnstring)
209231
if len(pk) > 0:
210232
arguments.append(pkstring)
233+
elif primary_key != None:
234+
arguments.append("-lco FID="+primary_key)
211235
if len(table) > 0:
212236
arguments.append('-nln')
213237
arguments.append(table)
@@ -242,6 +266,10 @@ def processAlgorithm(self, progress):
242266
if len(gt) > 0:
243267
arguments.append('-gt')
244268
arguments.append(gt)
269+
if promotetomulti:
270+
arguments.append('-nlt PROMOTE_TO_MULTI')
271+
if precision is False:
272+
arguments.append('-lco PRECISION=NO')
245273
if len(options) > 0:
246274
arguments.append(options)
247275

@@ -252,4 +280,4 @@ def processAlgorithm(self, progress):
252280
else:
253281
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
254282

255-
GdalUtils.runGdal(commands, progress)
283+
GdalUtils.runGdal(commands, progress)

‎python/plugins/processing/algs/gdal/ogr2ogrtopostgislist.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,22 @@
2525

2626
__revision__ = '$Format:%H$'
2727

28-
from PyQt4.QtCore import QSettings
28+
import os
29+
30+
from PyQt4.QtCore import *
31+
from PyQt4.QtGui import *
32+
33+
from qgis.core import *
2934

3035
from processing.core.parameters import ParameterVector
3136
from processing.core.parameters import ParameterString
3237
from processing.core.parameters import ParameterCrs
3338
from processing.core.parameters import ParameterSelection
3439
from processing.core.parameters import ParameterBoolean
3540
from processing.core.parameters import ParameterExtent
41+
from processing.core.parameters import ParameterTableField
3642

37-
from processing.tools.system import isWindows
43+
from processing.tools.system import *
3844

3945
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
4046
from processing.algs.gdal.GdalUtils import GdalUtils
@@ -56,6 +62,7 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
5662
SCHEMA = 'SCHEMA'
5763
TABLE = 'TABLE'
5864
PK = 'PK'
65+
PRIMARY_KEY = 'PRIMARY_KEY'
5966
GEOCOLUMN = 'GEOCOLUMN'
6067
DIM = 'DIM'
6168
DIMLIST = ['2','3']
@@ -71,6 +78,8 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
7178
LAUNDER = 'LAUNDER'
7279
INDEX = 'INDEX'
7380
SKIPFAILURES = 'SKIPFAILURES'
81+
PRECISION = 'PRECISION'
82+
PROMOTETOMULTI = 'PROMOTETOMULTI'
7483
OPTIONS = 'OPTIONS'
7584

7685
def dbConnectionNames(self):
@@ -87,7 +96,7 @@ def defineCharacteristics(self):
8796
self.addParameter(ParameterVector(self.INPUT_LAYER,
8897
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY], False))
8998
self.addParameter(ParameterSelection(self.GTYPE,
90-
self.tr('Output geometry type'), self.GEOMTYPE, 5))
99+
self.tr('Output geometry type'), self.GEOMTYPE, 0))
91100
self.addParameter(ParameterCrs(self.A_SRS,
92101
self.tr('Assign an output CRS'), ''))
93102
self.addParameter(ParameterCrs(self.T_SRS,
@@ -100,7 +109,9 @@ def defineCharacteristics(self):
100109
self.tr('Table name, leave blank to use input name'),
101110
'', optional=True))
102111
self.addParameter(ParameterString(self.PK,
103-
self.tr('Primary key'), 'id', optional=True))
112+
self.tr('Primary key (new field)'), 'id', optional=True))
113+
self.addParameter(ParameterTableField(self.PRIMARY_KEY,
114+
self.tr('Primary key (existing field, used if the above option is left empty)'), self.INPUT_LAYER, optional=True))
104115
self.addParameter(ParameterString(self.GEOCOLUMN,
105116
self.tr('Geometry column name'), 'geom', optional=True))
106117
self.addParameter(ParameterSelection(self.DIM,
@@ -135,6 +146,12 @@ def defineCharacteristics(self):
135146
self.addParameter(ParameterBoolean(self.SKIPFAILURES,
136147
self.tr('Continue after a failure, skipping the failed feature'),
137148
False))
149+
self.addParameter(ParameterBoolean(self.PROMOTETOMULTI,
150+
self.tr('Promote to Multipart'),
151+
True))
152+
self.addParameter(ParameterBoolean(self.PRECISION,
153+
self.tr('Keep width and precision of input attributes'),
154+
True))
138155
self.addParameter(ParameterString(self.OPTIONS,
139156
self.tr('Additional creation options'), '', optional=True))
140157

@@ -151,12 +168,13 @@ def processAlgorithm(self, progress):
151168
ogrLayer = self.ogrConnectionString(inLayer)[1:-1]
152169
ssrs = unicode(self.getParameterValue(self.S_SRS))
153170
tsrs = unicode(self.getParameterValue(self.T_SRS))
154-
asrs = unicode(self.getParameterValue(self.A_SRS))
171+
asrs = unicode(self.getParameterValue(self.A_SRS))
155172
schema = unicode(self.getParameterValue(self.SCHEMA))
156173
schemastring = "-lco SCHEMA="+schema
157174
table = unicode(self.getParameterValue(self.TABLE))
158175
pk = unicode(self.getParameterValue(self.PK))
159176
pkstring = "-lco FID="+pk
177+
primary_key = self.getParameterValue(self.PRIMARY_KEY)
160178
geocolumn = unicode(self.getParameterValue(self.GEOCOLUMN))
161179
geocolumnstring = "-lco GEOMETRY_NAME="+geocolumn
162180
dim = self.DIMLIST[self.getParameterValue(self.DIM)]
@@ -177,6 +195,8 @@ def processAlgorithm(self, progress):
177195
index = self.getParameterValue(self.INDEX)
178196
indexstring = "-lco SPATIAL_INDEX=OFF"
179197
skipfailures = self.getParameterValue(self.SKIPFAILURES)
198+
promotetomulti = self.getParameterValue(self.PROMOTETOMULTI)
199+
precision = self.getParameterValue(self.PRECISION)
180200
options = unicode(self.getParameterValue(self.OPTIONS))
181201

182202
arguments = []
@@ -187,9 +207,9 @@ def processAlgorithm(self, progress):
187207
arguments.append('PG:"host='+host)
188208
arguments.append('port='+port)
189209
if len(dbname) > 0:
190-
arguments.append('dbname='+dbname)
210+
arguments.append('dbname='+dbname)
191211
if len(password) > 0:
192-
arguments.append('password='+password)
212+
arguments.append('password='+password)
193213
arguments.append('user='+user+'"')
194214
arguments.append(dimstring)
195215
arguments.append(ogrLayer)
@@ -213,6 +233,8 @@ def processAlgorithm(self, progress):
213233
arguments.append(geocolumnstring)
214234
if len(pk) > 0:
215235
arguments.append(pkstring)
236+
elif primary_key != None:
237+
arguments.append("-lco FID="+primary_key)
216238
if len(table) > 0:
217239
arguments.append('-nln')
218240
arguments.append(table)
@@ -247,6 +269,10 @@ def processAlgorithm(self, progress):
247269
if len(gt) > 0:
248270
arguments.append('-gt')
249271
arguments.append(gt)
272+
if promotetomulti:
273+
arguments.append('-nlt PROMOTE_TO_MULTI')
274+
if precision is False:
275+
arguments.append('-lco PRECISION=NO')
250276
if len(options) > 0:
251277
arguments.append(options)
252278

@@ -257,4 +283,4 @@ def processAlgorithm(self, progress):
257283
else:
258284
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
259285

260-
GdalUtils.runGdal(commands, progress)
286+
GdalUtils.runGdal(commands, progress)

0 commit comments

Comments
 (0)
Please sign in to comment.