Re: [QGIS-Developer] setDataSourceUri() not working

2022-12-08 Thread Julien Cabieces via QGIS-Developer


Hi,

dataSourceUri is used when creating the dataProvider/vectorlayer and 
there is no way to refresh the provider according to a new uri.

I don't see any other way than recreate a new QgsVectorLayer with the
new dataSource uri, add it to your project instance with addMapLayer and
remove the old one.

Maybe there is a trick to simplify things but I fail to see how.

Regards,
Julien


> Hi Devs,
>
> I'm trying to change the dataSourceUri of an existing layer, but can't 
> get it working. The reason is that my layer should be connected to 
> another view in the same database, because the db should do some 
> calculations for my plugin.
>
> I created a test script for a simple polygon layer in a gpkg, which I 
> attached to this email. The layer "polygons" and both views are all 
> working correctly if you just drag and drop the gpkg in QGIS.
>
> Open the polygons layer and select it. Then run:
>
>
> layer = iface.activeLayer()
> provider = layer.dataProvider()
>
> ds = provider.dataSourceUri()
> print(ds)
>
> fn = ds.split('|layername=')[0]
> new_ds = fn + '|layername=highest_3_view'
> print(new_ds)
>
> provider.setDataSourceUri(new_ds)
>
>
>
>
> The first run displays 2 different dataSourceUris:
> path/to/test_views.gpkg|layername=polygons
> path/to/test_views.gpkg|layername=highest_3_view
>
> The second run displays 2x the same dataSourceUris:
> path/to/test_views.gpkg|layername=polygons
> path/to/test_views.gpkg|layername=highest_3_view
>
> So the string has been changed. But my layer properties still point to 
> path/to/test_views.gpkg|layername=polygons
>
> and the features displayed are all 5 from the polygons layer. (The views 
> have only 3 features)
>
>
>
> Should i do something after setting the new provider? I've been trying 
> many things, including:
> * provider.reloadData()
> * layer.setDataProvider(dataprovider)
> * refreshing/redrawing the layer
>
>
> Hope anyone can help!
> Raymond___
> QGIS-Developer mailing list
> QGIS-Developer@lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


Re: [QGIS-Developer] setDataSourceUri() not working

2022-12-08 Thread Andrea Giudiceandrea via QGIS-Developer

*Raymond Nijssen*
/Wed Dec 7 07:39:50 PST 2022/

I'm trying to change the dataSourceUri of an existing layer


Hi Raymond,
AFAIK you need to use setDataSource [1] directly on the layer an not 
setDataSourceUri on the data provider.


E.g.:

layer.setDataSource(new_ds, layer_name, 'ogr')


Best regards.

Andrea

[1] 
https://qgis.org/pyqgis/master/core/QgsMapLayer.html#qgis.core.QgsMapLayer.setDataSource___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


Re: [QGIS-Developer] setDataSourceUri() not working

2022-12-08 Thread Raymond Nijssen via QGIS-Developer

Thanks Andrea, but that doesn't seem to exist:

AttributeError: 'QgsVectorLayer' object has no attribute 'setDataSourceUri'

Raymond


On 08-12-2022 19:42, Andrea Giudiceandrea via QGIS-Developer wrote:

*Raymond Nijssen*
/Wed Dec 7 07:39:50 PST 2022/

I'm trying to change the dataSourceUri of an existing layer


Hi Raymond,
AFAIK you need to use setDataSource [1] directly on the layer an not 
setDataSourceUri on the data provider.


E.g.:

layer.setDataSource(new_ds, layer_name, 'ogr')


Best regards.

Andrea

[1] 
https://qgis.org/pyqgis/master/core/QgsMapLayer.html#qgis.core.QgsMapLayer.setDataSource


___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


Re: [QGIS-Developer] setDataSourceUri() not working

2022-12-08 Thread Andrea Giudiceandrea via QGIS-Developer

Hi Raymond,
I wrote setDataSource, not setDataSourceUri.

Regards.

Andrea


Il 09/12/2022 08:19, Raymond Nijssen via QGIS-Developer ha scritto:

Thanks Andrea, but that doesn't seem to exist:

AttributeError: 'QgsVectorLayer' object has no attribute 
'setDataSourceUri'


Raymond


On 08-12-2022 19:42, Andrea Giudiceandrea via QGIS-Developer wrote:

*Raymond Nijssen*
/Wed Dec 7 07:39:50 PST 2022/
 


I'm trying to change the dataSourceUri of an existing layer


Hi Raymond,
AFAIK you need to use setDataSource [1] directly on the layer an not 
setDataSourceUri on the data provider.


E.g.:

layer.setDataSource(new_ds, layer_name, 'ogr')


Best regards.

Andrea

[1] 
https://qgis.org/pyqgis/master/core/QgsMapLayer.html#qgis.core.QgsMapLayer.setDataSource


___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


Re: [QGIS-Developer] setDataSourceUri() not working

2022-12-08 Thread Raymond Nijssen via QGIS-Developer

That works!! :O

Thank you so much!!!

Raymond

On 09-12-2022 08:20, Andrea Giudiceandrea via QGIS-Developer wrote:

Hi Raymond,
I wrote setDataSource, not setDataSourceUri.

Regards.

Andrea


Il 09/12/2022 08:19, Raymond Nijssen via QGIS-Developer ha scritto:

Thanks Andrea, but that doesn't seem to exist:

AttributeError: 'QgsVectorLayer' object has no attribute 
'setDataSourceUri'


Raymond


On 08-12-2022 19:42, Andrea Giudiceandrea via QGIS-Developer wrote:

*Raymond Nijssen*
/Wed Dec 7 07:39:50 PST 2022/

I'm trying to change the dataSourceUri of an existing layer


Hi Raymond,
AFAIK you need to use setDataSource [1] directly on the layer an not 
setDataSourceUri on the data provider.


E.g.:

layer.setDataSource(new_ds, layer_name, 'ogr')


Best regards.

Andrea

[1] 
https://qgis.org/pyqgis/master/core/QgsMapLayer.html#qgis.core.QgsMapLayer.setDataSource


___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer