9

My program opens a QGIS project, loads one layer, adds a point on this layer and then stops. My problem is - I don't save the project explicitly, but the added points are always saved (I have to remove them manually).

The question seems to be duplicate of How close project QGIS (* .qgs) without saving (PyQGIS), but the answer from there (project.clear()) - I cannot import qApp from PyQt5.QtGui, but processing the user interface is probably not important there. I just added project.clear() before qgs.exitQgis(), but the project is still saved. It's QGIS version 3.30 on Windows 10.

Commentators requested the code, so here is my code (without all the imports; I am running this with standalone PyQGIS):

# Supply path to qgis install location
QgsApplication.setPrefixPath("C:\OSGeo4W", True)

# Create a reference to the QgsApplication.  Setting the
# second argument to False disables the GUI.
qgs = QgsApplication([], False)

# Load providers
qgs.initQgis()

project.read('C:\\Users\\Tambet\\Desktop\\Barcelona qgis\\planols1.qgs')

layers = QgsProject.instance().mapLayersByName('test')
print(layers[0].name())
layer = layers[0]
caps = layer.dataProvider().capabilities()

if caps & QgsVectorDataProvider.AddFeatures:
    print("Adding feature..")
    feat = QgsFeature(layer.fields())
    feat.setAttributes([0, 'added programatically'])
    feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(431613.6, 4582819.9)))
    res, outFeats = layer.dataProvider().addFeatures([feat])

features = layer.getFeatures()

for feature in features:
    # retrieve every feature with its geometry and attributes
    print("Feature ID: ", feature.id())
    # fetch geometry
    # show some information about the feature geometry
    geom = feature.geometry()
    geomSingleType = QgsWkbTypes.isSingleType(geom.wkbType())
    if geom.type() == QgsWkbTypes.PointGeometry:
        # the geometry type can be of single or multi type
        if geomSingleType:
            x = geom.asPoint()
            print("Point: ", x)

layoutmanager = project.layoutManager()
print(layoutmanager.layouts())
layout = layoutmanager.layoutByName("test1") #Layout name
exporter = QgsLayoutExporter(layout)
print(exporter.exportToPdf("C:\\Users\\Tambet\\eclipse-workspace\\qgis test\\test.pdf", QgsLayoutExporter.PdfExportSettings() ))

project.clear()
qgs.exitQgis()
3
  • Do you use dataProvider to add the point? Could you please share the code you use doing things you mentioned? It is hard to say something without seeing your script. Apr 3, 2023 at 9:19
  • 2
    Your question is not very clear. Editing a layer and saving the project is two different things. It's possible to edit data (and saving) without saving the project itself. Please share more context, like your code ?
    – etrimaille
    Apr 3, 2023 at 9:23
  • I added the Python code. Apr 3, 2023 at 9:32

1 Answer 1

11

As @etrimaille stated, "editing a layer" and "saving the project" are two different things, and you can edit data (and saving) without saving the project itself.

A project is not a container of data, but it contains layers order, styles, source paths etc., not data itself. When you add or edit a feature, you change the data in data source, not project.

In your script, you use layer.dataProvider().addFeatures(). It adds the feature to the data source immediately.

If you want to add the feature (and to see in map canvas) but don't want to save it to the data source, you can start edit mode for the layer using layer.startEditing() without using layer.commitChanges().

In edit mode, you have to use layer.addFeature() instead of layer.dataProvider().addFeatures() as follows:

layer.startEditing()

if caps & QgsVectorDataProvider.AddFeatures:
    ...
    ...


    is_added = layer.addFeature(feat) # is_added: boolean

...

# use commitChanges only when you want to save the changes
# layer.commitChanges()

layer.addFeature makes the changes in memory, it will not be saved to the data source unless layer.commitChanges is run.

9
  • So can I do something to not add the data immediately? Should I make a copy of the data source every time or is there an easier way? Can I add features without touching the dataProvider? Is there any way to get a temporary link to dataProvider or add it as a temporary file? Apr 3, 2023 at 9:45
  • Please check the edited answer. Apr 3, 2023 at 9:46
  • This is too temporary. It does not appear in the feature list afterwards and it does not appear in the PDF file I generate. I should appear in the PDF, yet not being saved with the project. Is this possible? Apr 3, 2023 at 9:59
  • anitagraser.com/… - this example is using a memory data provider. Can I somehow convert it to the memory data provider on the fly? Apr 3, 2023 at 10:05
  • 1
    Yes. Check this answer to duplicate the layer on the fly to work on it temporarily. Apr 3, 2023 at 10:06

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.