<div dir="ltr">Hi Olivier,<div><br></div><div>I have been playing with this and I think you should use the source on the layer and not on the data provider.</div><div>Here is some code (which also modifies the style of the layer)</div><div><a href="https://github.com/QGEP/customizer/blob/master/customizer.py#L129-L141">https://github.com/QGEP/customizer/blob/master/customizer.py#L129-L141</a></div><div><br></div><div>I was doing a layer.reload() after changing the source.</div><div><br></div><div>Hope that helps,</div><div>Denis</div><div><br><div class="gmail_quote"><div dir="ltr">Le mar. 25 sept. 2018 à 19:02, Olivier Dalang <<a href="mailto:olivier.dalang@gmail.com">olivier.dalang@gmail.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Dear List,</div><div><br></div><div>As simple as it sounds, I'm struggling with changing the datasource of layers with python :</div><div><br></div>

<div style="color:rgb(248,248,242);background-color:rgb(39,40,34);font-family:Consolas,"Courier New",monospace;font-weight:normal;font-size:14px;line-height:19px;white-space:pre-wrap"><div><span style="color:rgb(248,248,242)">layer1 </span><span style="color:rgb(249,38,114)">=</span><span style="color:rgb(248,248,242)"> QgsVectorLayer(</span><span style="color:rgb(230,219,116)">'/path/to/a.shp'</span><span style="color:rgb(248,248,242)">)</span></div><div><span style="color:rgb(248,248,242)">QgsProject.instance().addMapLayer(layer1)</span></div><div><span style="color:rgb(248,248,242)">layer1.dataProvider().setDataSourceUri(</span><span style="color:rgb(230,219,116)">'/path/to/b.shp'</span><span style="color:rgb(248,248,242)">)</span></div></div>

</div></div><div><br></div><div>The layer successfully changes on map redraw. However layer1.source() stays to the initial layer, so that the change does not persist across saves (save and reload, and you get a.shp again).</div><div><br>

</div><div>The exact same code with a geopackage doesn't work at all (no changes on map redraw).<br></div><div><br></div><div>

<div style="color:rgb(248,248,242);background-color:rgb(39,40,34);font-family:Consolas,"Courier New",monospace;font-weight:normal;font-size:14px;line-height:19px;white-space:pre-wrap"><div><span style="color:rgb(248,248,242)">layer1 </span><span style="color:rgb(249,38,114)">=</span><span style="color:rgb(248,248,242)"> QgsVectorLayer(</span><span style="color:rgb(230,219,116)">'/path/to/db.gpkg|layer=a'</span><span style="color:rgb(248,248,242)">)</span></div><div><span style="color:rgb(248,248,242)">QgsProject.instance().addMapLayer(layer1)</span></div><div><span style="color:rgb(248,248,242)">layer1.dataProvider().setDataSourceUri (</span><span style="color:rgb(230,219,116)">'/path/to/db.gpkg|layer=b'</span><span style="color:rgb(248,248,242)">) </span></div></div></div><div><div><br></div><div>
<div>There is also an entry in the pyqgis cookbook (<a href="https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html#id1" target="_blank">here</a>,
 scroll down) for QgsVectorLayer. The layer1.source() is correctly updated, but again, it works with shapefiles, but 
not geopackages. And this is for vectors only.<br></div><div><br></div><div><span style="color:rgb(117,113,94)">
</span><div style="color:rgb(248,248,242);background-color:rgb(39,40,34);font-family:Consolas,"Courier New",monospace;font-weight:normal;font-size:14px;line-height:19px;white-space:pre-wrap"><div><span style="color:rgb(248,248,242)">layer1.setDataSource(<span style="color:rgb(230,219,116)">'<span style="color:rgb(230,219,116)">/path/to/b.shp</span></span><span style="color:rgb(230,219,116)">'</span>, </span><span style="color:rgb(230,219,116)">"b"</span><span style="color:rgb(248,248,242)">, </span><span style="color:rgb(230,219,116)">"ogr"</span><span style="color:rgb(248,248,242)">)</span></div></div>

</div><div><br></div><div>Am I hitting a bug ? Or did I get confused somewhere ?</div><div><br></div></div><div>As I feel this is a fairly common need, I'd be very grateful if someone could help me figure out the recommended way to achieve this, for both vectors or rasters. I'll update the pyqgis cookbook in return :-)</div><div><br></div><div>Note that there are workarounds (like replacing with a new layer and copying styles over or writing directly to the .qgs file, but they are not straightforward 
and have limitations).<br></div><div><br></div><div>Thanks !!</div><div><br></div><div>Olivier</div><br><div dir="ltr"><br></div><div dir="ltr"><br></div>

</div></div></div></div>
_______________________________________________<br>
QGIS-Developer mailing list<br>
<a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a></blockquote></div></div></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">





<p class="inbox-inbox-inbox-inbox-p1"><span style="color:rgb(0,0,0);font-family:Verdana,sans-serif;font-size:10pt">Denis Rouzaud</span><br style="color:rgb(0,0,0);font-family:Times;font-size:medium"><a href="mailto:denis@opengis.ch" target="_blank" style="font-family:Times;font-size:medium"><span style="color:rgb(0,0,0);font-family:Verdana,sans-serif;font-size:8pt">denis@opengis.ch</span> </a><br style="color:rgb(0,0,0);font-family:Times;font-size:medium"><span style="color:rgb(0,0,0);font-family:Verdana,sans-serif;font-size:8pt"><a>+41 76 370 21 22</a></span></p><p class="inbox-inbox-inbox-inbox-p1"><span style="color:rgb(0,0,0);font-family:Verdana,sans-serif;font-size:8pt"><a><br></a></span></p></div></div>