Skip to content

Commit

Permalink
Port GDAL Ogr2OgrPostgis to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 13, 2017
1 parent 479106a commit 1a56487
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 127 deletions.
239 changes: 120 additions & 119 deletions python/plugins/processing/algs/gdal/ogr2ogrtopostgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@

__revision__ = '$Format:%H$'

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 qgis.core import (QgsProcessingParameterFeatureSource,
QgsProcessingParameterString,
QgsProcessingParameterEnum,
QgsProcessingParameterCrs,
QgsProcessingParameterField,
QgsProcessingParameterExtent,
QgsProcessingParameterBoolean,
QgsProcessingUtils)

from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
Expand All @@ -43,7 +44,7 @@

class Ogr2OgrToPostGis(GdalAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
INPUT = 'INPUT'
SHAPE_ENCODING = 'SHAPE_ENCODING'
GTYPE = 'GTYPE'
GEOMTYPE = ['', 'NONE', 'GEOMETRY', 'POINT', 'LINESTRING', 'POLYGON', 'GEOMETRYCOLLECTION', 'MULTIPOINT', 'MULTIPOLYGON', 'MULTILINESTRING']
Expand Down Expand Up @@ -82,78 +83,78 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer')))
self.addParameter(ParameterString(self.SHAPE_ENCODING,
self.tr('Shape encoding'), "", optional=True))
self.addParameter(ParameterSelection(self.GTYPE,
self.tr('Output geometry type'), self.GEOMTYPE, 0))
self.addParameter(ParameterCrs(self.A_SRS,
self.tr('Assign an output CRS'), '', optional=False))
self.addParameter(ParameterCrs(self.T_SRS,
self.tr('Reproject to this CRS on output '), '', optional=True))
self.addParameter(ParameterCrs(self.S_SRS,
self.tr('Override source CRS'), '', optional=True))
self.addParameter(ParameterString(self.HOST,
self.tr('Host'), 'localhost', optional=True))
self.addParameter(ParameterString(self.PORT,
self.tr('Port'), '5432', optional=True))
self.addParameter(ParameterString(self.USER,
self.tr('Username'), '', optional=True))
self.addParameter(ParameterString(self.DBNAME,
self.tr('Database name'), '', optional=True))
self.addParameter(ParameterString(self.PASSWORD,
self.tr('Password'), '', optional=True))
self.addParameter(ParameterString(self.SCHEMA,
self.tr('Schema name'), 'public', optional=True))
self.addParameter(ParameterString(self.TABLE,
self.tr('Table name, leave blank to use input name'),
'', optional=True))
self.addParameter(ParameterString(self.PK,
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,
self.tr('Vector dimensions'), self.DIMLIST, 0))
self.addParameter(ParameterString(self.SIMPLIFY,
self.tr('Distance tolerance for simplification'),
'', optional=True))
self.addParameter(ParameterString(self.SEGMENTIZE,
self.tr('Maximum distance between 2 nodes (densification)'),
'', optional=True))
self.addParameter(ParameterExtent(self.SPAT,
self.tr('Select features by extent (defined in input layer CRS)')))
self.addParameter(ParameterBoolean(self.CLIP,
self.tr('Clip the input layer using the above (rectangle) extent'),
False))
self.addParameter(ParameterString(self.WHERE,
self.tr('Select features using a SQL "WHERE" statement (Ex: column=\'value\')'),
'', optional=True))
self.addParameter(ParameterString(self.GT,
self.tr('Group N features per transaction (Default: 20000)'),
'', optional=True))
self.addParameter(ParameterBoolean(self.OVERWRITE,
self.tr('Overwrite existing table'), True))
self.addParameter(ParameterBoolean(self.APPEND,
self.tr('Append to existing table'), False))
self.addParameter(ParameterBoolean(self.ADDFIELDS,
self.tr('Append and add new fields to existing table'), False))
self.addParameter(ParameterBoolean(self.LAUNDER,
self.tr('Do not launder columns/table names'), False))
self.addParameter(ParameterBoolean(self.INDEX,
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))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer')))
self.addParameter(QgsProcessingParameterString(self.SHAPE_ENCODING,
self.tr('Shape encoding'), "", optional=True))
self.addParameter(QgsProcessingParameterEnum(self.GTYPE,
self.tr('Output geometry type'), options=self.GEOMTYPE, defaultValue=0))
self.addParameter(QgsProcessingParameterCrs(self.A_SRS,
self.tr('Assign an output CRS'), defaultValue='', optional=False))
self.addParameter(QgsProcessingParameterCrs(self.T_SRS,
self.tr('Reproject to this CRS on output '), defaultValue='', optional=True))
self.addParameter(QgsProcessingParameterCrs(self.S_SRS,
self.tr('Override source CRS'), defaultValue='', optional=True))
self.addParameter(QgsProcessingParameterString(self.HOST,
self.tr('Host'), defaultValue='localhost', optional=True))
self.addParameter(QgsProcessingParameterString(self.PORT,
self.tr('Port'), defaultValue='5432', optional=True))
self.addParameter(QgsProcessingParameterString(self.USER,
self.tr('Username'), defaultValue='', optional=True))
self.addParameter(QgsProcessingParameterString(self.DBNAME,
self.tr('Database name'), defaultValue='', optional=True))
self.addParameter(QgsProcessingParameterString(self.PASSWORD,
self.tr('Password'), defaultValue='', optional=True))
self.addParameter(QgsProcessingParameterString(self.SCHEMA,
self.tr('Schema name'), defaultValue='public', optional=True))
self.addParameter(QgsProcessingParameterString(self.TABLE,
self.tr('Table name, leave blank to use input name'),
defaultValue='', optional=True))
self.addParameter(QgsProcessingParameterString(self.PK,
self.tr('Primary key (new field)'), defaultValue='id', optional=True))
self.addParameter(QgsProcessingParameterField(self.PRIMARY_KEY,
self.tr('Primary key (existing field, used if the above option is left empty)'), parentLayerParameterName=self.INPUT, optional=True))
self.addParameter(QgsProcessingParameterString(self.GEOCOLUMN,
self.tr('Geometry column name'), defaultValue='geom', optional=True))
self.addParameter(QgsProcessingParameterEnum(self.DIM,
self.tr('Vector dimensions'), options=self.DIMLIST, defaultValue=0))
self.addParameter(QgsProcessingParameterString(self.SIMPLIFY,
self.tr('Distance tolerance for simplification'),
defaultValue='', optional=True))
self.addParameter(QgsProcessingParameterString(self.SEGMENTIZE,
self.tr('Maximum distance between 2 nodes (densification)'),
defaultValue='', optional=True))
self.addParameter(QgsProcessingParameterExtent(self.SPAT,
self.tr('Select features by extent (defined in input layer CRS)')))
self.addParameter(QgsProcessingParameterBoolean(self.CLIP,
self.tr('Clip the input layer using the above (rectangle) extent'),
defaultValue=False))
self.addParameter(QgsProcessingParameterString(self.WHERE,
self.tr('Select features using a SQL "WHERE" statement (Ex: column=\'value\')'),
defaultValue='', optional=True))
self.addParameter(QgsProcessingParameterString(self.GT,
self.tr('Group N features per transaction (Default: 20000)'),
defaultValue='', optional=True))
self.addParameter(QgsProcessingParameterBoolean(self.OVERWRITE,
self.tr('Overwrite existing table'), defaultValue=True))
self.addParameter(QgsProcessingParameterBoolean(self.APPEND,
self.tr('Append to existing table'), defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(self.ADDFIELDS,
self.tr('Append and add new fields to existing table'), defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(self.LAUNDER,
self.tr('Do not launder columns/table names'), defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(self.INDEX,
self.tr('Do not create spatial index'), defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(self.SKIPFAILURES,
self.tr('Continue after a failure, skipping the failed feature'), defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(self.PROMOTETOMULTI,
self.tr('Promote to Multipart'),
defaultValue=True))
self.addParameter(QgsProcessingParameterBoolean(self.PRECISION,
self.tr('Keep width and precision of input attributes'),
defaultValue=True))
self.addParameter(QgsProcessingParameterString(self.OPTIONS,
self.tr('Additional creation options'), defaultValue='', optional=True))

def name(self):
return 'importvectorintopostgisdatabasenewconnection'
Expand All @@ -164,13 +165,13 @@ def displayName(self):
def group(self):
return self.tr('Vector miscellaneous')

def getConnectionString(self, parameters):
host = self.getParameterValue(self.HOST)
port = self.getParameterValue(self.PORT)
user = self.getParameterValue(self.USER)
dbname = self.getParameterValue(self.DBNAME)
password = self.getParameterValue(self.PASSWORD)
schema = self.getParameterValue(self.SCHEMA)
def getConnectionString(self, parameters, context):
host = self.parameterAsString(parameters, self.HOST, context)
port = self.parameterAsString(parameters, self.PORT, context)
user = self.parameterAsString(parameters, self.USER, context)
dbname = self.parameterAsString(parameters, self.DBNAME, context)
password = self.parameterAsString(parameters, self.PASSWORD, context)
schema = self.parameterAsString(parameters, self.SCHEMA, context)
arguments = []
if host:
arguments.append('host=' + host)
Expand All @@ -187,41 +188,41 @@ def getConnectionString(self, parameters):
return GdalUtils.escapeAndJoin(arguments)

def getConsoleCommands(self, parameters, context, feedback):
inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = ogrConnectionString(inLayer, context)[1:-1]
shapeEncoding = self.getParameterValue(self.SHAPE_ENCODING)
ssrs = str(self.getParameterValue(self.S_SRS))
tsrs = str(self.getParameterValue(self.T_SRS))
asrs = str(self.getParameterValue(self.A_SRS))
table = str(self.getParameterValue(self.TABLE))
schema = str(self.getParameterValue(self.SCHEMA))
pk = str(self.getParameterValue(self.PK))
inLayer = self.parameterAsSource(parameters, self.INPUT, context)
ogrLayer, layername = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback)
shapeEncoding = self.parameterAsString(parameters, self.SHAPE_ENCODING, context)
ssrs = self.parameterAsCrs(parameters, self.S_SRS, context)
tsrs = self.parameterAsCrs(parameters, self.T_SRS, context)
asrs = self.parameterAsCrs(parameters, self.A_SRS, context)
table = self.parameterAsString(parameters, self.TABLE, context)
schema = self.parameterAsString(parameters, self.SCHEMA, context)
pk = self.parameterAsString(parameters, self.PK, context)
pkstring = "-lco FID=" + pk
primary_key = self.getParameterValue(self.PRIMARY_KEY)
geocolumn = str(self.getParameterValue(self.GEOCOLUMN))
primary_key = self.parameterAsString(parameters, self.PRIMARY_KEY, context)
geocolumn = self.parameterAsString(parameters, self.GEOCOLUMN, context)
geocolumnstring = "-lco GEOMETRY_NAME=" + geocolumn
dim = self.DIMLIST[self.getParameterValue(self.DIM)]
dim = self.DIMLIST[self.parameterAsEnum(parameters, self.DIM, context)]
dimstring = "-lco DIM=" + dim
simplify = str(self.getParameterValue(self.SIMPLIFY))
segmentize = str(self.getParameterValue(self.SEGMENTIZE))
spat = self.getParameterValue(self.SPAT)
if not spat:
spat = QgsProcessingUtils.combineLayerExtents([inLayer])
clip = self.getParameterValue(self.CLIP)
where = str(self.getParameterValue(self.WHERE))
simplify = self.parameterAsString(parameters, self.SIMPLIFY, context)
segmentize = self.parameterAsString(parameters, self.SEGMENTIZE, context)
spat = self.parameterAsExtent(parameters, self.SPAT, context)
if spat.isNull():
spat = inLayer.sourceExtent()
clip = self.parameterAsBool(parameters, self.CLIP, context)
where = self.parameterAsString(parameters, self.WHERE, context)
wherestring = '-where "' + where + '"'
gt = str(self.getParameterValue(self.GT))
overwrite = self.getParameterValue(self.OVERWRITE)
append = self.getParameterValue(self.APPEND)
addfields = self.getParameterValue(self.ADDFIELDS)
launder = self.getParameterValue(self.LAUNDER)
gt = self.parameterAsString(parameters, self.GT, context)
overwrite = self.parameterAsBool(parameters, self.OVERWRITE, context)
append = self.parameterAsBool(parameters, self.APPEND, context)
addfields = self.parameterAsBool(parameters, self.ADDFIELDS, context)
launder = self.parameterAsBool(parameters, self.LAUNDER, context)
launderstring = "-lco LAUNDER=NO"
index = self.getParameterValue(self.INDEX)
index = self.parameterAsBool(parameters, self.INDEX, context)
indexstring = "-lco SPATIAL_INDEX=OFF"
skipfailures = self.getParameterValue(self.SKIPFAILURES)
promotetomulti = self.getParameterValue(self.PROMOTETOMULTI)
precision = self.getParameterValue(self.PRECISION)
options = str(self.getParameterValue(self.OPTIONS))
skipfailures = self.parameterAsBool(parameters, self.SKIPFAILURES, context)
promotetomulti = self.parameterAsBool(parameters, self.PROMOTETOMULTI, context)
precision = self.parameterAsBool(parameters, self.PRECISION, context)
options = self.parameterAsString(parameters, self.OPTIONS, context)

arguments = []
arguments.append('-progress')
Expand All @@ -233,7 +234,7 @@ def getConsoleCommands(self, parameters, context, feedback):
arguments.append('-f')
arguments.append('PostgreSQL')
arguments.append('PG:"')
arguments.append(self.getConnectionString(parameters))
arguments.append(self.getConnectionString(parameters, context))
arguments.append('"')
arguments.append(dimstring)
arguments.append(ogrLayer)
Expand All @@ -248,9 +249,9 @@ def getConsoleCommands(self, parameters, context, feedback):
arguments.append('-addfields')
if overwrite:
arguments.append('-overwrite')
if len(self.GEOMTYPE[self.getParameterValue(self.GTYPE)]) > 0:
if len(self.GEOMTYPE[self.parameterAsEnum(parameters, self.GTYPE, context)]) > 0:
arguments.append('-nlt')
arguments.append(self.GEOMTYPE[self.getParameterValue(self.GTYPE)])
arguments.append(self.GEOMTYPE[self.parameterAsEnum(parameters, self.GTYPE, context)])
if len(geocolumn) > 0:
arguments.append(geocolumnstring)
if len(pk) > 0:
Expand Down
Loading

0 comments on commit 1a56487

Please sign in to comment.