I have a layer in QGIS, and I want to duplicate it through a plugin so I can use the copy of it as I want, without modifying the original.
Of course layer2 = layer1
will not work, because everything that happens to layer2 will also happen to layer1, as it is the same object behind all this.
The only way I found to do it is as such:
QgsVectorFileWriter.writeAsVectorFormat(layer1, r"C:\Users\ABC\AppData\Local\Temp\NewLayer.shp", "utf-8", None, "ESRI Shapefile")
layer2 = QgsVectorLayer("C:\Users\ABC\AppData\Local\Temp\NewLayer.shp", "New vector", "ogr")
#do something with layer2
Is there a simple way to duplicate the layer in memory, without having to write a new file?