Skip to content
Permalink
Browse files
[processing] fixed ogr import to postgis when origin layer is a database
  • Loading branch information
volaya committed Jan 28, 2015
1 parent 97b7406 commit 1eec3b9ecf647e10749091495061d2b6a41b67eb
Showing with 9 additions and 3 deletions.
  1. +9 −3 python/plugins/processing/algs/gdal/OgrAlgorithm.py
@@ -62,8 +62,13 @@ def ogrConnectionString(self, uri):
# user='ktryjh_iuuqef' password='xyqwer' sslmode=disable
# key='gid' estimatedmetadata=true srid=4326 type=MULTIPOLYGON
# table="t4" (geom) sql=
s = re.sub(''' sslmode=.+''', '', unicode(layer.source()))
ogrstr = 'PG:%s' % s
dsUri = QgsDataSourceURI(layer.dataProvider().dataSourceUri())
connInfo = dsUri.connectionInfo()
(success, user, passwd ) = QgsCredentials.instance().get(connInfo, None, None)
if success:
QgsCredentials.instance().put(connInfo, user, passwd)
ogrstr = ("PG:dbname='%s' host='%s' port='%s' user='%s' password='%s'"
% (dsUri.database(), dsUri.host(), dsUri.port(), user, passwd))
else:
ogrstr = unicode(layer.source()).split("|")[0]
return '"' + ogrstr + '"'
@@ -72,7 +77,7 @@ def ogrLayerName(self, uri):
if 'host' in uri:
regex = re.compile('(table=")(.+?)(\.)(.+?)"')
r = regex.search(uri)
return r.groups()[1] + '.' + r.groups()[3]
return '"' + r.groups()[1] + '.' + r.groups()[3] +'"'
elif 'dbname' in uri:
regex = re.compile('(table=")(.+?)"')
r = regex.search(uri)
@@ -83,3 +88,4 @@ def ogrLayerName(self, uri):
return r.groups()[1]
else:
return os.path.basename(os.path.splitext(uri)[0])

8 comments on commit 1eec3b9

@gioman

This comment has been minimized.

Copy link
Contributor

@gioman gioman replied Feb 5, 2015

This commit introduced a regression when using processing/GDAL-OGR modules when input is a postgis layer: when clicking on "run" a new dialog opens and asks for username and password. If entered correctly then this dialogs does not shows anymore.

@volaya

This comment has been minimized.

Copy link
Contributor Author

@volaya volaya replied Feb 5, 2015

How do you suggest doing that? If the connection exists that shouldn't show a dialog, but otherwise, it needs to. Before this commit, I couldn't use this to import from a postgis table into another one, since the password is not in the uri and therefore cannot be passed to ogr

@gioman

This comment has been minimized.

Copy link
Contributor

@gioman gioman replied Feb 5, 2015

Hi Victor, I just tested with the code as it was before this commit and I can indeed import a postgis table into another one (using the ogr "import vector into postgis database" tool): the processing log is

GDAL command:
ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL PG:"host=localhost port=5432 dbname=* password=* user=* " -lco DIM=2 "PG:dbname='*' host=localhost port=5432 user='xxx' password='xxx'" "public"."inputtablename" -overwrite -lco SCHEMA=public -lco GEOMETRY_NAME=geom -lco FID=id -nln outputtablename -spat -120313.585938 -301774.125 106365.046875 -25175.0371094
GDAL command output:
0...10...20...30...40...50...60...70...80...90...100 - done.

@volaya

This comment has been minimized.

Copy link
Contributor Author

@volaya volaya replied Feb 6, 2015

So when you call layer.source(), the returned uri has user and password. I don't have that. I guess you are storing the password of your PostGIS connection, but if you do not have that option selected (I don't have it), the uri will not contain the password and user. Does that make sense to you?

@jef-n

This comment has been minimized.

Copy link
Member

@jef-n jef-n replied Feb 6, 2015

Just quick notes, I didn't look at the full context. Feel free to ignore.

get() should be call repeatedly until credentials are entered that work or the user cancels (success=false). And put() should only be called once the connection works (otherwise subsequent get() calls will return wrong credentials). If the credentials weren't stored it might work to set the environment variable PGUSER and PGPASS to avoid having the credentials in the ogr2ogr call - to avoid having them logged.

@gioman

This comment has been minimized.

Copy link
Contributor

@gioman gioman replied Feb 6, 2015

@volaya yes, iit that case the password is stored in the connection. There is also a version of the tool that asks for connections details instead of using the connections defined in qgis. Anyway with this commit the dialog that asks for the credentials also show when the layer that was added in the project comes from a connection where the username/password are stored in qgis.

@volaya

This comment has been minimized.

Copy link
Contributor Author

@volaya volaya replied Feb 11, 2015

@gioman hmm, but the version that asks for details, it asks for the destination database details, not the origin one. How do you suggest fixing this? I See that the popup is not a good thing, but how can we get the credentials of the PG layer that is used as input? I think that if the layer has those credentials available, the get method that i am calling will return them without showing the dialog (that's how it is working here in the tests that i made), otherwise will show the dialog. Any idea?

@gioman

This comment has been minimized.

Copy link
Contributor

@gioman gioman replied Feb 11, 2015

@volaya right sorry, that tool ask for connection data for output. Anyway I'm a bit lost, I do not have anything against this new dialog, but I don't think it should pop up when the layer being used as input was added using a connection that has both username and password stored in qgis. I tested on both linux and windows and the dialog always show. After it shows the first time and you enter the credentials, then it stops to show. It doesn't show also if you already entered manually the credentials somewhere else in qgis in that specific work session.

Please sign in to comment.