Re: [Qgis-developer] Change the query of a query layer (postgis)

2015-06-17 Thread Olivier Dalang
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 :

> 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
>
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer

Re: [Qgis-developer] Change the query of a query layer (postgis)

2015-06-17 Thread enricofer
Hi Olivier,
In your case, I think that the best way to do this is to create a view from
the sql query and load it as layer, than, when qslider changes, change the
view definition with "ALTER VIEW"  and reload the layer.

Regards,

Enrico Ferreguti

Il giorno 17/giu/2015, alle ore 14:29, olivier [via OSGeo.org] <
ml-node+s1560n5211424...@n6.nabble.com> ha scritto:

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

___
Qgis-developer mailing list
[hidden email] 
http://lists.osgeo.org/mailman/listinfo/qgis-developer

--
 If you reply to this email, your message will be added to the discussion
below:
http://osgeo-org.1560.x6.nabble.com/Change-the-query-of-a-query-layer-postgis-tp5211424.html
 To unsubscribe from Quantum GIS - Developer, click here

.
NAML





--
View this message in context: 
http://osgeo-org.1560.x6.nabble.com/Change-the-query-of-a-query-layer-postgis-tp5211424p5211452.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer

Re: [Qgis-developer] Change the query of a query layer (postgis)

2015-06-17 Thread Olivier Dalang
Hi Enrico,

Hmm I didn't think of that, thanks for the idea ! But I can't use it since
I may have several user at once.

Best,

Olivier

2015-06-17 16:03 GMT+02:00 enricofer :

> Hi Olivier,
> In your case, I think that the best way to do this is to create a view
> from the sql query and load it as layer, than, when qslider changes, change
> the view definition with "ALTER VIEW"  and reload the layer.
>
> Regards,
>
> Enrico Ferreguti
>
> Il giorno 17/giu/2015, alle ore 14:29, olivier [via OSGeo.org] <[hidden
> email] > ha
> scritto:
>
> 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
>
> ___
> Qgis-developer mailing list
> [hidden email] 
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
> --
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://osgeo-org.1560.x6.nabble.com/Change-the-query-of-a-query-layer-postgis-tp5211424.html
>  To unsubscribe from Quantum GIS - Developer, click here.
> NAML
> 
>
>
> --
> View this message in context: Re: Change the query of a query layer
> (postgis)
> 
> Sent from the Quantum GIS - Developer mailing list archive
> 
> at Nabble.com.
>
> ___
> Qgis-developer mailing list
> Qgis-developer@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer

Re: [Qgis-developer] Change the query of a query layer (postgis)

2015-06-17 Thread Enrico Ferreguti
Hi Olivier,
datasource changing is a feature that I really miss and wait from longtime.
Every Qgis new release I checked without success if the properties
datasource string is editable. I recently found the hack you report (
https://geogear.wordpress.som/2015/05/15/changing-qgis-layer-datasource-with-python/)
that I verify to be working up to Wien 2.8.2. Till 2.9 the code crash qgis.
Reading your post I realized that a new QgsVectorLayer method has appeared:
setDatasource. Wow! Perhaps the waiting is going to be over

Regards,

Enrico Ferreguti.

2015-06-17 16:45 GMT+02:00 Olivier Dalang :

> Hi Enrico,
>
> Hmm I didn't think of that, thanks for the idea ! But I can't use it since
> I may have several user at once.
>
> Best,
>
> Olivier
>
> 2015-06-17 16:03 GMT+02:00 enricofer :
>
>> Hi Olivier,
>> In your case, I think that the best way to do this is to create a view
>> from the sql query and load it as layer, than, when qslider changes, change
>> the view definition with "ALTER VIEW"  and reload the layer.
>>
>> Regards,
>>
>> Enrico Ferreguti
>>
>> Il giorno 17/giu/2015, alle ore 14:29, olivier [via OSGeo.org] <[hidden
>> email] > ha
>> scritto:
>>
>> 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
>>
>> ___
>> Qgis-developer mailing list
>> [hidden email] 
>> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>>
>> --
>>  If you reply to this email, your message will be added to the
>> discussion below:
>>
>> http://osgeo-org.1560.x6.nabble.com/Change-the-query-of-a-query-layer-postgis-tp5211424.html
>>  To unsubscribe from Quantum GIS - Developer, click here.
>> NAML
>> 
>>
>>
>> --
>> View this message in context: Re: Change the query of a query layer
>> (postgis)
>> 
>> Sent from the Quantum GIS - Developer mailing list archive
>> 
>> at Nabble.com.
>>
>> ___
>> Qgis-developer mailing list
>> Qgis-developer@lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>>
>
>
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer

Re: [Qgis-developer] Change the query of a query layer (postgis)

2015-06-22 Thread Nyall Dawson
On 18 June 2015 at 00:06, Olivier Dalang  wrote:
> 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...

No, you're right. It was crashy. It's been fixed in master, and I've
also made it now retain the layer's renderer and legend if the
geometry type has not changed.

Hope that helps!
Nyall

>
>
>
>
> 2015-06-17 14:29 GMT+02:00 Olivier Dalang :
>>
>> 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
>
>
>
> ___
> Qgis-developer mailing list
> Qgis-developer@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer