Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1874 from gioman/fix_processing_ogr_postgis_import_2
new fixes for processing/ogr import in postgis tools
  • Loading branch information
alexbruy committed Feb 2, 2015
2 parents db89a04 + de2b3ea commit 9143bd7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
31 changes: 26 additions & 5 deletions python/plugins/processing/algs/gdal/ogr2ogrtopostgis.py
Expand Up @@ -31,6 +31,7 @@
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterExtent
from processing.core.parameters import ParameterTableField

from processing.tools.system import isWindows

Expand All @@ -53,6 +54,7 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
SCHEMA = 'SCHEMA'
TABLE = 'TABLE'
PK = 'PK'
PRIMARY_KEY = 'PRIMARY_KEY'
GEOCOLUMN = 'GEOCOLUMN'
DIM = 'DIM'
DIMLIST = ['2','3']
Expand All @@ -68,6 +70,8 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
LAUNDER = 'LAUNDER'
INDEX = 'INDEX'
SKIPFAILURES = 'SKIPFAILURES'
PRECISION = 'PRECISION'
PROMOTETOMULTI = 'PROMOTETOMULTI'
OPTIONS = 'OPTIONS'

def defineCharacteristics(self):
Expand All @@ -76,7 +80,7 @@ def defineCharacteristics(self):
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY], False))
self.addParameter(ParameterSelection(self.GTYPE,
self.tr('Output geometry type'), self.GEOMTYPE, 5))
self.tr('Output geometry type'), self.GEOMTYPE, 0))
self.addParameter(ParameterCrs(self.A_SRS,
self.tr('Assign an output CRS'), ''))
self.addParameter(ParameterCrs(self.T_SRS,
Expand All @@ -99,7 +103,9 @@ def defineCharacteristics(self):
self.tr('Table name, leave blank to use input name'),
'', optional=True))
self.addParameter(ParameterString(self.PK,
self.tr('Primary Key'), 'id', optional=True))
self.tr('Primary key (new field)'), 'id', optional=True))
self.addParameter(ParameterTableField(self.PRIMARY_KEY,
self.tr('Primary key (existing field, used if the above option is left empty)'), self.INPUT_LAYER, optional=True))
self.addParameter(ParameterString(self.GEOCOLUMN,
self.tr('Geometry column name'), 'geom', optional=True))
self.addParameter(ParameterSelection(self.DIM,
Expand Down Expand Up @@ -133,6 +139,12 @@ def defineCharacteristics(self):
self.tr('Do not create spatial index'), False))
self.addParameter(ParameterBoolean(self.SKIPFAILURES,
self.tr('Continue after a failure, skipping the failed feature'), False))
self.addParameter(ParameterBoolean(self.PROMOTETOMULTI,
self.tr('Promote to Multipart'),
True))
self.addParameter(ParameterBoolean(self.PRECISION,
self.tr('Keep width and precision of input attributes'),
True))
self.addParameter(ParameterString(self.OPTIONS,
self.tr('Additional creation options'), '', optional=True))

Expand All @@ -152,6 +164,7 @@ def processAlgorithm(self, progress):
table = unicode(self.getParameterValue(self.TABLE))
pk = unicode(self.getParameterValue(self.PK))
pkstring = "-lco FID="+pk
primary_key = self.getParameterValue(self.PRIMARY_KEY)
geocolumn = unicode(self.getParameterValue(self.GEOCOLUMN))
geocolumnstring = "-lco GEOMETRY_NAME="+geocolumn
dim = self.DIMLIST[self.getParameterValue(self.DIM)]
Expand All @@ -172,6 +185,8 @@ def processAlgorithm(self, progress):
index = self.getParameterValue(self.INDEX)
indexstring = "-lco SPATIAL_INDEX=OFF"
skipfailures = self.getParameterValue(self.SKIPFAILURES)
promotetomulti = self.getParameterValue(self.PROMOTETOMULTI)
precision = self.getParameterValue(self.PRECISION)
options = unicode(self.getParameterValue(self.OPTIONS))

arguments = []
Expand All @@ -182,9 +197,9 @@ def processAlgorithm(self, progress):
arguments.append('PG:"host='+host)
arguments.append('port='+port)
if len(dbname) > 0:
arguments.append('dbname='+dbname)
arguments.append('dbname='+dbname)
if len(password) > 0:
arguments.append('password='+password)
arguments.append('password='+password)
arguments.append('user='+user+'"')
arguments.append(dimstring)
arguments.append(ogrLayer)
Expand All @@ -208,6 +223,8 @@ def processAlgorithm(self, progress):
arguments.append(geocolumnstring)
if len(pk) > 0:
arguments.append(pkstring)
elif primary_key != None:
arguments.append("-lco FID="+primary_key)
if len(table) > 0:
arguments.append('-nln')
arguments.append(table)
Expand Down Expand Up @@ -242,6 +259,10 @@ def processAlgorithm(self, progress):
if len(gt) > 0:
arguments.append('-gt')
arguments.append(gt)
if promotetomulti:
arguments.append('-nlt PROMOTE_TO_MULTI')
if precision is False:
arguments.append('-lco PRECISION=NO')
if len(options) > 0:
arguments.append(options)

Expand All @@ -252,4 +273,4 @@ def processAlgorithm(self, progress):
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

GdalUtils.runGdal(commands, progress)
GdalUtils.runGdal(commands, progress)
32 changes: 27 additions & 5 deletions python/plugins/processing/algs/gdal/ogr2ogrtopostgislist.py
Expand Up @@ -25,6 +25,7 @@

__revision__ = '$Format:%H$'


from PyQt4.QtCore import QSettings

from processing.core.parameters import ParameterVector
Expand All @@ -33,6 +34,7 @@
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterExtent
from processing.core.parameters import ParameterTableField

from processing.tools.system import isWindows

Expand All @@ -56,6 +58,7 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
SCHEMA = 'SCHEMA'
TABLE = 'TABLE'
PK = 'PK'
PRIMARY_KEY = 'PRIMARY_KEY'
GEOCOLUMN = 'GEOCOLUMN'
DIM = 'DIM'
DIMLIST = ['2','3']
Expand All @@ -71,6 +74,8 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
LAUNDER = 'LAUNDER'
INDEX = 'INDEX'
SKIPFAILURES = 'SKIPFAILURES'
PRECISION = 'PRECISION'
PROMOTETOMULTI = 'PROMOTETOMULTI'
OPTIONS = 'OPTIONS'

def dbConnectionNames(self):
Expand All @@ -87,7 +92,7 @@ def defineCharacteristics(self):
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY], False))
self.addParameter(ParameterSelection(self.GTYPE,
self.tr('Output geometry type'), self.GEOMTYPE, 5))
self.tr('Output geometry type'), self.GEOMTYPE, 0))
self.addParameter(ParameterCrs(self.A_SRS,
self.tr('Assign an output CRS'), ''))
self.addParameter(ParameterCrs(self.T_SRS,
Expand All @@ -100,7 +105,9 @@ def defineCharacteristics(self):
self.tr('Table name, leave blank to use input name'),
'', optional=True))
self.addParameter(ParameterString(self.PK,
self.tr('Primary key'), 'id', optional=True))
self.tr('Primary key (new field)'), 'id', optional=True))
self.addParameter(ParameterTableField(self.PRIMARY_KEY,
self.tr('Primary key (existing field, used if the above option is left empty)'), self.INPUT_LAYER, optional=True))
self.addParameter(ParameterString(self.GEOCOLUMN,
self.tr('Geometry column name'), 'geom', optional=True))
self.addParameter(ParameterSelection(self.DIM,
Expand Down Expand Up @@ -135,6 +142,12 @@ def defineCharacteristics(self):
self.addParameter(ParameterBoolean(self.SKIPFAILURES,
self.tr('Continue after a failure, skipping the failed feature'),
False))
self.addParameter(ParameterBoolean(self.PROMOTETOMULTI,
self.tr('Promote to Multipart'),
True))
self.addParameter(ParameterBoolean(self.PRECISION,
self.tr('Keep width and precision of input attributes'),
True))
self.addParameter(ParameterString(self.OPTIONS,
self.tr('Additional creation options'), '', optional=True))

Expand All @@ -157,6 +170,7 @@ def processAlgorithm(self, progress):
table = unicode(self.getParameterValue(self.TABLE))
pk = unicode(self.getParameterValue(self.PK))
pkstring = "-lco FID="+pk
primary_key = self.getParameterValue(self.PRIMARY_KEY)
geocolumn = unicode(self.getParameterValue(self.GEOCOLUMN))
geocolumnstring = "-lco GEOMETRY_NAME="+geocolumn
dim = self.DIMLIST[self.getParameterValue(self.DIM)]
Expand All @@ -177,6 +191,8 @@ def processAlgorithm(self, progress):
index = self.getParameterValue(self.INDEX)
indexstring = "-lco SPATIAL_INDEX=OFF"
skipfailures = self.getParameterValue(self.SKIPFAILURES)
promotetomulti = self.getParameterValue(self.PROMOTETOMULTI)
precision = self.getParameterValue(self.PRECISION)
options = unicode(self.getParameterValue(self.OPTIONS))

arguments = []
Expand All @@ -187,9 +203,9 @@ def processAlgorithm(self, progress):
arguments.append('PG:"host='+host)
arguments.append('port='+port)
if len(dbname) > 0:
arguments.append('dbname='+dbname)
arguments.append('dbname='+dbname)
if len(password) > 0:
arguments.append('password='+password)
arguments.append('password='+password)
arguments.append('user='+user+'"')
arguments.append(dimstring)
arguments.append(ogrLayer)
Expand All @@ -213,6 +229,8 @@ def processAlgorithm(self, progress):
arguments.append(geocolumnstring)
if len(pk) > 0:
arguments.append(pkstring)
elif primary_key != None:
arguments.append("-lco FID="+primary_key)
if len(table) > 0:
arguments.append('-nln')
arguments.append(table)
Expand Down Expand Up @@ -247,6 +265,10 @@ def processAlgorithm(self, progress):
if len(gt) > 0:
arguments.append('-gt')
arguments.append(gt)
if promotetomulti:
arguments.append('-nlt PROMOTE_TO_MULTI')
if precision is False:
arguments.append('-lco PRECISION=NO')
if len(options) > 0:
arguments.append(options)

Expand All @@ -257,4 +279,4 @@ def processAlgorithm(self, progress):
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

GdalUtils.runGdal(commands, progress)
GdalUtils.runGdal(commands, progress)

0 comments on commit 9143bd7

Please sign in to comment.