[Qgis-developer] Change the query of a query layer (postgis)
Olivier Dalang
olivier.dalang at gmail.com
Wed Jun 17 07:06:54 PDT 2015
Actually it seems the layer.setDataSource() method works, sorry for the
noise !
But then it resets the style of the layer to default (uniform random
color), as if the layer was freshly added...
I tried this :
# get values
oldRenderer = layer.rendererV2().clone()
oldDataSource = layer.dataProvider().dataSourceUri()
newDataSource =
re.sub('\/\*\*\/[0-9.]*\/\*\*\/','/**/'+str(buffer)+'/**/',oldDataSource)
# set values
layer.setDataSource( newDataSource, layer.name(),
layer.dataProvider().name() )
layer.setRendererV2( oldRenderer )
But it crashes because of the last line, when setting the rendererV2.
This workaround works :
# get values
oldRendererXML = layer.rendererV2().save( QDomDocument() )
oldDataSource = layer.dataProvider().dataSourceUri()
newDataSource =
re.sub('\/\*\*\/[0-9.]*\/\*\*\/','/**/'+str(date)+'/**/',oldDataSource)
# set values
layer.setDataSource( newDataSource, layer.name(),
layer.dataProvider().name() )
layer.setRendererV2( QgsFeatureRendererV2.load(oldRendererXML) )
The whole thing is slow though when compared to layer.setSubsetString(...),
not allowing for nice realtime feedback for the user.
>From looking into the source, it seems the QgsVectorLayer::setDataSource
method considers the layer is freshly added: it resets the CRS, it
creates/loads a rendererV2, it sets the legend, and it connects some
signals, all of which probably explains why it's so slow.
Would it be possible to change that method to to update the dataSource of
an existing layer ? Or to add a changeDataSource(...) method ?
Thanks !
Olivier
2015-06-17 14:29 GMT+02:00 Olivier Dalang <olivier.dalang at gmail.com>:
> Hi !
>
> Is it possible to change the query of a postgis query layer in python
> without reloading the project ?
>
> The goal is to have a parameter in my query that I'd be able to change
> with a slider.
>
> I tried the QgsVectorLayer.setDataSource() method, but it makes QGis crash.
> I tried the workaround described here (
> http://gis.stackexchange.com/questions/62610/changing-data-source-of-layer-in-qgis
> ), writing and reading the XML file, but it also makes QGis crash.
>
> If not possible, is there a serious limitation behind this, or is the
> feature just missing/bugged ?
>
> Example :
>
> I have this query loaded as a layer:
>
> "SELECT id, name, ST_Buffer(geom,25) as geom FROM my_table"
>
> But I want to be able to change the hardcoded buffer from 25 to some value
> from a QSlider.
>
> Thanks!
>
> Olivier
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20150617/39832fc0/attachment.html>
More information about the Qgis-developer
mailing list