On Fri, 14 Jun 2019 at 09:41, Ivan Ivanov <[email protected]> wrote: > > Hi, Nyall, > > I'm developing a plugin with a lot of internal layers, that are being > (re)created on user interaction. > > There are multiple event listeners attached to a layer, as well as passed > around to different functions and might have custom styles. At some point, I > want to replace the layer entirely with another one (result from an > algorithm). > > To solve this, I came up with three solutions, maybe there are more: > One option to achieve this is to copy all the features from the new layer and > put them in the first one, but the layers might be very big and I'm afraid it > would be a problem. > The second option that I'm currently using is to recreate all the event > listeners on the new layer and replace it from the layer tree. This is > okayish and it works. > The third option is to set a new data source, as I thought it's the most > optimal one, however, when the layer has two uids it's practically broken.
Well, this approach will work perfectly fine for everything BUT memory layers. The features from memory layers can't be shared between two layers, so resetting the data source here really doesn't have any meaning. There's just no way to duplicate a memory layer except for copy all the features and adding to a new layer. Nyall > > I got some inspiration from here: > https://github.com/enricofer/changeDataSource/blob/master/setdatasource.py#L175-L206 > directly editing the XML but the result is the same - double ids. I checked > this plugin because I'm sure it used to work in 3.2 (currently having 3.6.3 > on Ubuntu). > > Best, > Ivan > > > > On Fri, 14 Jun 2019 at 01:21, Nyall Dawson <[email protected]> wrote: >> >> On Wed, 12 Jun 2019 at 10:09, Ivan Ivanov <[email protected]> wrote: >> > >> > I fixed it. Sorry for the inconvenience: >> > >> > layer1 = QgsVectorLayer('Polygon?crs=epsg:32612', 'layer2', 'memory') >> > layer2 = QgsVectorLayer('Polygon?crs=epsg:32612', 'layer1', 'memory') >> > >> > print(layer1.source()) >> > print(layer2.source()) >> > layer1.setDataSource(layer2.source(), layer2.name(), >> > layer2.dataProvider().name()) >> > print(layer1.source()) >> >> I agree that the results of this output look odd - but what exactly >> are you trying to achieve here? >> >> Nyall >> >> > >> > On Wed, 12 Jun 2019 at 00:28, Nyall Dawson <[email protected]> wrote: >> >> >> >> On Tue, 11 Jun 2019 at 15:06, Ivan Ivanov <[email protected]> wrote: >> >> > >> >> > Hello, >> >> > >> >> > >> >> > Can you give me some idea why I'm getting two uids in the layer's >> >> > source? And also how I can fix this because now the layer just >> >> > disappears from the map canvas. (all layers are isValid() = True) >> >> > >> >> > oldLayer.source() >> >> > # >> >> > MultiLineString?crs=EPSG:32737&field=fid:integer(0,0)&uid={6cd1f176-9c05-4d2a-961a-3143a8fa422b} >> >> > >> >> > newLayer = processing.run('save:selectedfeatures', { >> >> > 'INPUT': oldLayer, >> >> > 'OUTPUT': 'memory:newLayer' >> >> > ) >> >> > >> >> > oldLayer.setDataSource(newLayer.source(), oldLayer.name(), >> >> > newLayer.dataProvider().name()) >> >> > >> >> > newLayer.source() >> >> > # >> >> > MultiLineString?crs=EPSG:32737&field=fid:integer(0,0)&uid={d36aff88-4248-4ffb-be2f-2ac1891ac26d} >> >> > oldLayer.source() >> >> > >> >> > # WRONG, I would expect the same as above. Also, the uid is completely >> >> > new. >> >> > # >> >> > MultiLineString?crs=EPSG:32737&field=fid:integer(0,0)&uid={d36aff88-4248-4ffb-be2f-2ac1891ac26d}&uid={3422de07-cf38-4249-af55-23c7d9779daf} >> >> >> >> Hi Ivan, >> >> >> >> Your code seems incomplete and has some syntax errors -- maybe it >> >> wasn't copied correctly? Can you repost please? >> >> >> >> Nyall _______________________________________________ QGIS-Developer mailing list [email protected] List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
