Skip to content
Permalink
Browse files

Merge pull request #3118 from arnaud-morvan/processing_gdal_postgis_c…

…redentials

[processing] support postgis service parameter and credentials input
  • Loading branch information
alexbruy committed Jun 14, 2016
2 parents bbbc9d0 + 8ddae27 commit aa9fe9ecd06764cda37e2cf941f152d6f5b22a58
@@ -36,6 +36,7 @@
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils

from processing.tools.postgis import uri_from_name, GeoDB
from processing.tools.system import isWindows
from processing.tools.vector import ogrConnectionString, ogrLayerName

@@ -64,6 +65,10 @@ class Ogr2OgrTableToPostGisList(GdalAlgorithm):
PRECISION = 'PRECISION'
OPTIONS = 'OPTIONS'

def __init__(self):
GdalAlgorithm.__init__(self)
self.processing = False

def dbConnectionNames(self):
settings = QSettings()
settings.beginGroup('/PostgreSQL/connections/')
@@ -112,15 +117,18 @@ def defineCharacteristics(self):
self.addParameter(ParameterString(self.OPTIONS,
self.tr('Additional creation options'), '', optional=True))

def processAlgorithm(self, progress):
self.processing = True
GdalAlgorithm.processAlgorithm(self, progress)
self.processing = False

def getConsoleCommands(self):
connection = self.DB_CONNECTIONS[self.getParameterValue(self.DATABASE)]
settings = QSettings()
mySettings = '/PostgreSQL/connections/' + connection
dbname = settings.value(mySettings + '/database')
user = settings.value(mySettings + '/username')
host = settings.value(mySettings + '/host')
port = settings.value(mySettings + '/port')
password = settings.value(mySettings + '/password')
uri = uri_from_name(connection)
if self.processing:
# to get credentials input when needed
uri = GeoDB(uri=uri).uri

inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = ogrConnectionString(inLayer)[1:-1]
shapeEncoding = self.getParameterValue(self.SHAPE_ENCODING)
@@ -150,19 +158,11 @@ def getConsoleCommands(self):
arguments.append('"' + shapeEncoding + '"')
arguments.append('-f')
arguments.append('PostgreSQL')
arguments.append('PG:"host=')
arguments.append(host)
arguments.append('port=')
arguments.append(port)
if len(dbname) > 0:
arguments.append('dbname=' + dbname)
if len(password) > 0:
arguments.append('password=' + password)
if len(schema) > 0:
arguments.append('active_schema=' + schema)
else:
arguments.append('active_schema=public')
arguments.append('user=' + user + '"')
arguments.append('PG:"')
for token in uri.connectionInfo(self.processing).split(' '):
arguments.append(token)
arguments.append('active_schema={}'.format(schema or 'public'))
arguments.append('"')
arguments.append(ogrLayer)
arguments.append('-nlt NONE')
arguments.append(ogrLayerName(inLayer))
@@ -39,6 +39,7 @@
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils

from processing.tools.postgis import uri_from_name, GeoDB
from processing.tools.system import isWindows
from processing.tools.vector import ogrConnectionString, ogrLayerName

@@ -81,6 +82,10 @@ class Ogr2OgrToPostGisList(GdalAlgorithm):
PROMOTETOMULTI = 'PROMOTETOMULTI'
OPTIONS = 'OPTIONS'

def __init__(self):
GdalAlgorithm.__init__(self)
self.processing = False

def dbConnectionNames(self):
settings = QSettings()
settings.beginGroup('/PostgreSQL/connections/')
@@ -156,15 +161,18 @@ def defineCharacteristics(self):
self.addParameter(ParameterString(self.OPTIONS,
self.tr('Additional creation options'), '', optional=True))

def processAlgorithm(self, progress):
self.processing = True
GdalAlgorithm.processAlgorithm(self, progress)
self.processing = False

def getConsoleCommands(self):
connection = self.DB_CONNECTIONS[self.getParameterValue(self.DATABASE)]
settings = QSettings()
mySettings = '/PostgreSQL/connections/' + connection
dbname = settings.value(mySettings + '/database')
user = settings.value(mySettings + '/username')
host = settings.value(mySettings + '/host')
port = settings.value(mySettings + '/port')
password = settings.value(mySettings + '/password')
uri = uri_from_name(connection)
if self.processing:
# to get credentials input when needed
uri = GeoDB(uri=uri).uri

inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = ogrConnectionString(inLayer)[1:-1]
shapeEncoding = self.getParameterValue(self.SHAPE_ENCODING)
@@ -208,17 +216,11 @@ def getConsoleCommands(self):
arguments.append('"' + shapeEncoding + '"')
arguments.append('-f')
arguments.append('PostgreSQL')
arguments.append('PG:"host=' + host)
arguments.append('port=' + port)
if len(dbname) > 0:
arguments.append('dbname=' + dbname)
if len(password) > 0:
arguments.append('password=' + password)
if len(schema) > 0:
arguments.append('active_schema=' + schema)
else:
arguments.append('active_schema=public')
arguments.append('user=' + user + '"')
arguments.append('PG:"')
for token in uri.connectionInfo(self.processing).split(' '):
arguments.append(token)
arguments.append('active_schema={}'.format(schema or 'public'))
arguments.append('"')
arguments.append(dimstring)
arguments.append(ogrLayer)
arguments.append(ogrLayerName(inLayer))
@@ -86,6 +86,8 @@ def defineCharacteristics(self):

def processAlgorithm(self, progress):
connection = self.DB_CONNECTIONS[self.getParameterValue(self.DATABASE)]
db = postgis.GeoDB.from_name(connection)

schema = self.getParameterValue(self.SCHEMA)
overwrite = self.getParameterValue(self.OVERWRITE)
createIndex = self.getParameterValue(self.CREATEINDEX)
@@ -94,17 +96,6 @@ def processAlgorithm(self, progress):
forceSinglePart = self.getParameterValue(self.FORCE_SINGLEPART)
primaryKeyField = self.getParameterValue(self.PRIMARY_KEY)
encoding = self.getParameterValue(self.ENCODING)
settings = QSettings()
mySettings = '/PostgreSQL/connections/' + connection
try:
database = settings.value(mySettings + '/database')
username = settings.value(mySettings + '/username')
host = settings.value(mySettings + '/host')
port = settings.value(mySettings + '/port', type=int)
password = settings.value(mySettings + '/password')
except Exception as e:
raise GeoAlgorithmExecutionException(
self.tr('Wrong database connection name: %s' % connection))

layerUri = self.getParameterValue(self.INPUT)
layer = dataobjects.getObjectFromUri(layerUri)
@@ -115,13 +106,6 @@ def processAlgorithm(self, progress):
table = table.replace(' ', '').lower()[0:62]
providerName = 'postgres'

try:
db = postgis.GeoDB(host=host, port=port, dbname=database,
user=username, passwd=password)
except postgis.DbError as e:
raise GeoAlgorithmExecutionException(
self.tr("Couldn't connect to database:\n%s") % unicode(e))

geomColumn = self.getParameterValue(self.GEOMETRY_COLUMN)
if not geomColumn:
geomColumn = 'the_geom'
@@ -141,8 +125,7 @@ def processAlgorithm(self, progress):
if not layer.hasGeometryType():
geomColumn = None

uri = QgsDataSourceURI()
uri.setConnection(host, unicode(port), database, username, password)
uri = db.uri
if primaryKeyField:
uri.setDataSource(schema, table, geomColumn, '', primaryKeyField)
else:
@@ -46,24 +46,7 @@ def defineCharacteristics(self):

def processAlgorithm(self, progress):
connection = self.getParameterValue(self.DATABASE)
settings = QSettings()
mySettings = '/PostgreSQL/connections/' + connection
try:
database = settings.value(mySettings + '/database')
username = settings.value(mySettings + '/username')
host = settings.value(mySettings + '/host')
port = settings.value(mySettings + '/port', type=int)
password = settings.value(mySettings + '/password')
except Exception as e:
raise GeoAlgorithmExecutionException(
self.tr('Wrong database connection name: %s' % connection))
try:
self.db = postgis.GeoDB(host=host, port=port,
dbname=database, user=username, passwd=password)
except postgis.DbError as e:
raise GeoAlgorithmExecutionException(
self.tr("Couldn't connect to database:\n%s") % unicode(e))

self.db = postgis.GeoDB.from_name(connection)
sql = self.getParameterValue(self.SQL).replace('\n', ' ')
try:
self.db._exec_sql_and_commit(unicode(sql))
@@ -100,23 +100,10 @@ def __init__(self, connection):
def populateSchemas(self):
if self.childCount() != 0:
return
settings = QSettings()
connSettings = '/PostgreSQL/connections/' + self.connection
database = settings.value(connSettings + '/database')
user = settings.value(connSettings + '/username')
host = settings.value(connSettings + '/host')
port = settings.value(connSettings + '/port')
passwd = settings.value(connSettings + '/password')
uri = QgsDataSourceURI()
uri.setConnection(host, str(port), database, user, passwd)
connInfo = uri.connectionInfo()
(success, user, passwd) = QgsCredentials.instance().get(connInfo, None, None)
if success:
QgsCredentials.instance().put(connInfo, user, passwd)
geodb = GeoDB(host, int(port), database, user, passwd)
schemas = geodb.list_schemas()
for oid, name, owner, perms in schemas:
item = QTreeWidgetItem()
item.setText(0, name)
item.setIcon(0, self.schemaIcon)
self.addChild(item)
geodb = GeoDB.from_name(self.connection)
schemas = geodb.list_schemas()
for oid, name, owner, perms in schemas:
item = QTreeWidgetItem()
item.setText(0, name)
item.setIcon(0, self.schemaIcon)
self.addChild(item)

0 comments on commit aa9fe9e

Please sign in to comment.
You can’t perform that action at this time.