[QGIS-Developer] pyqgis - layer.saveDefaultStyle() for PostGIS layers

kimaidou kimaidou at gmail.com
Mon Sep 25 07:21:58 PDT 2017


I have tried another method

* first add default style into DB via UI -> it works

* the use console to create a new, not default, style
msg = ''
layer.saveStyleToDatabase( "a new style", "some description", False, None,
msg );

It does not work (not new line created, and msg remains empty. No log in
the database, which means NO query has been sent (I have change postgresql
config to log all requests).

But the following method works
layer.listStylesInDatabase()

And a new line appears in the postgresql log
2017-09-25 16:19:18.248 CEST [8984] mdouchin at qgis LOG:  00000: duration:
0.217 ms  statement: SELECT id,styleName,description FROM layer_styles
WHERE NOT (f_table_catalog='qgis' AND f_table_schema='sup' AND
f_table_name='geo_commune' AND f_geometry_column='geom') ORDER BY
update_time DESC

It seems the method saveStyleToDatabase returns with no error, which can be
caused by 2 reasons
https://qgis.org/api/2.18/qgsvectorlayer_8cpp_source.html#l04347

 4369   this->exportNamedStyle( qmlDocument, msgError );
 4370   if ( !msgError.isNull() )
 4371   {
 4372     return;
 4373   }
 4374   qmlStyle = qmlDocument.toString();
 4375
 4376   this->exportSldStyle( sldDocument, msgError );
 4377   if ( !msgError.isNull() )
 4378   {
 4379     return;
 4380   }

/me investigates

2017-09-25 15:50 GMT+02:00 Jorge Gustavo Pereira Bastos Rocha <
jgr at di.uminho.pt>:

> Hi,
>
> It already happened to me. The first user to save a style became the owner
> of the table. No other users were able to write on the layer_style table,
> because there were DEFAULT PRIVILEGES defined.
>
> Can you check the postgrsql log to see if there is any problem related
> with permissions?
>
> Regards,
>
> Jorge Gustavo
>
> ________________________________________
> De: QGIS-Developer [qgis-developer-bounces at lists.osgeo.org] em nome de
> kimaidou [kimaidou at gmail.com]
> Enviado: 25 de Setembro de 2017 14:23
> Para: Luigi Pirelli
> Cc: qgis-developer at lists.osgeo.org
> Assunto: Re: [QGIS-Developer] pyqgis - layer.saveDefaultStyle() for
> PostGIS     layers
>
> Hi,
>
> @ Tom : I really would like to trust QGIS method instead of managing
> manually different providers.
>
> @ Luigi
> No error message, even worse : i got a message telling everything went
> well ;)
>
> layer.saveDefaultStyle()
> (u'The style dbname=\'qgis\' host=localhost port=5432 user=\'mdouchin\'
> sslmode=disable key=\'ogc_fid\' estimatedmetadata=true srid=2154
> type=MultiPolygon table="sup"."geo_commune" (geom) sql= was updated in the
> database.', True)
>
> But the line is not created in the layer_styles table.
>
> Michaƫl
>
>
> 2017-09-25 14:48 GMT+02:00 Luigi Pirelli <luipir at gmail.com<mailto:luipi
> r at gmail.com>>:
> from the code:
>
> mLayer->saveStyleToDatabase and mLayer->saveDefaultStyle
>
> and in
> QString QgsMapLayer::saveDefaultStyle( bool & theResultFlag )
> {
>   return saveNamedStyle( styleURI(), theResultFlag );
> }
>
> => seems you are doing correct.
>
> any log message?
>
> ******************************************
>
>
> void QgsVectorLayerProperties::saveDefaultStyle_clicked()
> {
>   apply();
>   QString errorMsg;
>   if ( mLayer->dataProvider()->isSaveAndLoadStyleToDBSupported() )
>   {
>     QMessageBox askToUser;
>     askToUser.setText( tr( "Save default style to: " ) );
>     askToUser.setIcon( QMessageBox::Question );
>     askToUser.addButton( tr( "Cancel" ), QMessageBox::RejectRole );
>     askToUser.addButton( tr( "Local database" ), QMessageBox::NoRole );
>     askToUser.addButton( tr( "Datasource database" ), QMessageBox::YesRole
> );
>
>     switch ( askToUser.exec() )
>     {
>       case 0:
>         return;
>       case 2:
>         mLayer->saveStyleToDatabase( "", "", true, "", errorMsg );
>         if ( errorMsg.isNull() )
>         {
>           return;
>         }
>         break;
>       default:
>         break;
>     }
>   }
>
>   bool defaultSavedFlag = false;
>   errorMsg = mLayer->saveDefaultStyle( defaultSavedFlag );
>   if ( !defaultSavedFlag )
>   {
>     QMessageBox::warning( this, tr( "Default Style" ), errorMsg );
>   }
> }
> Luigi Pirelli
>
> ************************************************************
> **************************************
> * Boundless QGIS Support/Development: lpirelli AT boundlessgeo DOT com
> * LinkedIn: https://www.linkedin.com/in/luigipirelli
> * Stackexchange: http://gis.stackexchange.com/users/19667/luigi-pirelli
> * GitHub: https://github.com/luipir
> * Mastering QGIS 2nd Edition:
> * https://www.packtpub.com/big-data-and-business-
> intelligence/mastering-qgis-second-edition
> ************************************************************
> **************************************
>
>
> On 25 September 2017 at 13:44, Tom Chadwin <tom.chadwin at nnpa.org.uk<
> mailto:tom.chadwin at nnpa.org.uk>> wrote:
> > Perhaps you have to do it manually via a PostGIS query matching
> > f_table_catalog, f_table_schema, and f_table_name, and updating styleqml
> and
> > stylesld?
> >
> > Tom
> >
> >
> >
> > -----
> > Buy Pie Spy: Adventures in British pastry 2010-11 on Amazon
> > --
> > Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-Developer-
> f4099106.html
> > _______________________________________________
> > QGIS-Developer mailing list
> > QGIS-Developer at lists.osgeo.org<mailto:QGIS-Developer at lists.osgeo.org>
> > List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20170925/3331a27e/attachment.html>


More information about the QGIS-Developer mailing list