better and new fixes for processing/ogr import in postgis tools

This commit is contained in:
Giovanni Manghi 2015-02-01 23:56:10 +00:00
parent 6e9468811e
commit dab3092139
2 changed files with 68 additions and 14 deletions

View File

@ -25,14 +25,22 @@ __copyright__ = '(C) 2012, Victor Olaya'
__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
@ -53,6 +61,7 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
SCHEMA = 'SCHEMA'
TABLE = 'TABLE'
PK = 'PK'
PRIMARY_KEY = 'PRIMARY_KEY'
GEOCOLUMN = 'GEOCOLUMN'
DIM = 'DIM'
DIMLIST = ['2','3']
@ -68,6 +77,8 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
LAUNDER = 'LAUNDER'
INDEX = 'INDEX'
SKIPFAILURES = 'SKIPFAILURES'
PRECISION = 'PRECISION'
PROMOTETOMULTI = 'PROMOTETOMULTI'
OPTIONS = 'OPTIONS'
def defineCharacteristics(self):
@ -76,7 +87,7 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
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,
@ -99,7 +110,9 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
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,
@ -133,6 +146,12 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
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))
@ -152,6 +171,7 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
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)]
@ -172,6 +192,8 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
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 = []
@ -182,9 +204,9 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
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)
@ -208,6 +230,8 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
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)
@ -242,6 +266,10 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
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)
@ -252,4 +280,4 @@ class Ogr2OgrToPostGis(OgrAlgorithm):
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
GdalUtils.runGdal(commands, progress)
GdalUtils.runGdal(commands, progress)

View File

@ -25,7 +25,12 @@ __copyright__ = '(C) 2012, Victor Olaya'
__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
@ -33,8 +38,9 @@ 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
@ -56,6 +62,7 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
SCHEMA = 'SCHEMA'
TABLE = 'TABLE'
PK = 'PK'
PRIMARY_KEY = 'PRIMARY_KEY'
GEOCOLUMN = 'GEOCOLUMN'
DIM = 'DIM'
DIMLIST = ['2','3']
@ -71,6 +78,8 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
LAUNDER = 'LAUNDER'
INDEX = 'INDEX'
SKIPFAILURES = 'SKIPFAILURES'
PRECISION = 'PRECISION'
PROMOTETOMULTI = 'PROMOTETOMULTI'
OPTIONS = 'OPTIONS'
def dbConnectionNames(self):
@ -87,7 +96,7 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
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,
@ -100,7 +109,9 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
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,
@ -135,6 +146,12 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
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))
@ -151,12 +168,13 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
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)]
@ -177,6 +195,8 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
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 = []
@ -187,9 +207,9 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
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)
@ -213,6 +233,8 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
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)
@ -247,6 +269,10 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
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)
@ -257,4 +283,4 @@ class Ogr2OgrToPostGisList(OgrAlgorithm):
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
GdalUtils.runGdal(commands, progress)
GdalUtils.runGdal(commands, progress)