Skip to content

Commit

Permalink
[processing] fixed ogr import to postgis when origin layer is a database
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Jan 28, 2015
1 parent 97b7406 commit 1eec3b9
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions python/plugins/processing/algs/gdal/OgrAlgorithm.py
Expand Up @@ -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 + '"'
Expand All @@ -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)
Expand All @@ -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
Copy link
Contributor

@gioman gioman commented on 1eec3b9 Feb 5, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor Author

@volaya volaya commented on 1eec3b9 Feb 5, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

@gioman gioman commented on 1eec3b9 Feb 5, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor Author

@volaya volaya commented on 1eec3b9 Feb 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

@jef-n jef-n commented on 1eec3b9 Feb 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

@gioman gioman commented on 1eec3b9 Feb 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link
Contributor Author

@volaya volaya commented on 1eec3b9 Feb 11, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link
Contributor

@gioman gioman commented on 1eec3b9 Feb 11, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.