I have a vector layer, which I ma adding it to a subgroup "streets_greoup". My code also save it to the disk. But it still show temporary layer when I close my project and saved feature data lost. Below is my code.

How can I remove this temporary storage symbol and save data in this layer? Data should not removed after closed project. It should save permanently.

project_dir_path = str(QFileDialog.getExistingDirectory(None,"Select Directory"))

project = QgsProject.instance()
project.write(project_dir_path +"/qgs_design_test.qgs")

root = QgsProject.instance().layerTreeRoot()
master_group = root.addGroup('streets_group')
mygroup = root.findGroup("streets_group")  # We assume the group exists

layer_name = 'terminal'
Fl_ou = project_dir_path + '/' + layer_name + ".shp"
mylayer =  QgsVectorLayer("Point", layer_name, "memory")
QgsProject.instance().addMapLayer(mylayer, False)

options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "ESRI Shapefile"
options.overrideGeometryType = QgsWkbTypes.PointZ
QgsVectorFileWriter.writeAsVectorFormatV2(mylayer, Fl_ou, QgsCoordinateTransformContext(), options)

enter image description here

1 Answer 1


Using writeAsVectorFormatV2 is like using "Save as.." or "Extract". Therefore, it doesn't change the layer source. You need to set data source of the layer to just saved source. Use the following lines after writeAsVectorFormatV2:

# set data source
mylayer.setDataSource(Fl_ou, mylayer.name(), "ogr")

# refresh layer symbol, removes the memory indicator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.