Skip to content

Commit

Permalink
better and new fixes for processing/ogr import in postgis tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Giovanni Manghi committed Feb 1, 2015
1 parent 6e94688 commit dab3092
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
40 changes: 34 additions & 6 deletions python/plugins/processing/algs/gdal/ogr2ogrtopostgis.py
Expand Up @@ -25,14 +25,22 @@

__revision__ = '$Format:%H$'

import os

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *

from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterCrs
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
from processing.tools.system import *

from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
Expand All @@ -53,6 +61,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 +77,8 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
LAUNDER = 'LAUNDER'
INDEX = 'INDEX'
SKIPFAILURES = 'SKIPFAILURES'
PRECISION = 'PRECISION'
PROMOTETOMULTI = 'PROMOTETOMULTI'
OPTIONS = 'OPTIONS'

def defineCharacteristics(self):
Expand All @@ -76,7 +87,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 +110,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 +146,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 +171,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 +192,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 +204,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 +230,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 +266,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 +280,4 @@ def processAlgorithm(self, progress):
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

GdalUtils.runGdal(commands, progress)
GdalUtils.runGdal(commands, progress)
42 changes: 34 additions & 8 deletions python/plugins/processing/algs/gdal/ogr2ogrtopostgislist.py
Expand Up @@ -25,16 +25,22 @@

__revision__ = '$Format:%H$'

from PyQt4.QtCore import QSettings
import os

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *

from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterCrs
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
from processing.tools.system import *

from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
Expand All @@ -56,6 +62,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 +78,8 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
LAUNDER = 'LAUNDER'
INDEX = 'INDEX'
SKIPFAILURES = 'SKIPFAILURES'
PRECISION = 'PRECISION'
PROMOTETOMULTI = 'PROMOTETOMULTI'
OPTIONS = 'OPTIONS'

def dbConnectionNames(self):
Expand All @@ -87,7 +96,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 +109,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 +146,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 @@ -151,12 +168,13 @@ def processAlgorithm(self, progress):
ogrLayer = self.ogrConnectionString(inLayer)[1:-1]
ssrs = unicode(self.getParameterValue(self.S_SRS))
tsrs = unicode(self.getParameterValue(self.T_SRS))
asrs = unicode(self.getParameterValue(self.A_SRS))
asrs = unicode(self.getParameterValue(self.A_SRS))
schema = unicode(self.getParameterValue(self.SCHEMA))
schemastring = "-lco SCHEMA="+schema
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 +195,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 +207,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 +233,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 +269,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 +283,4 @@ def processAlgorithm(self, progress):
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

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

0 comments on commit dab3092

Please sign in to comment.