Friday, 2 February 2018

pyqgis - QgsVectorLayer vs QgsVectorDataProvider for addFeatures() function in QGIS


Has the addFeatures function moved between QgsVectorLayer and QgsVectorDataProvider?



Do both work? Is one deprecated. The apis do not provide that info. Nor does a google search.


Which is preferred and why?


What am I misunderstanding here? What is the situation that is changing which means I may need to change this?



Answer



@MatthiasKuhn, one of the core developers of QGIS, has mentioned on his website OPENGIS.ch to avoid using dataProvider() when adding features and instead do it directly on the layer (i.e. layer.addFeature(...)). He lists various reasons to avoid using the data provider:



  • You cannot undo them easily.

  • They generate one request per call what may reduce performance.

  • They do not emit internal signals for map redraws and other refreshes of the user interface.

  • They do not take uncommitted changes into account so the python changes will get overwritten by the user when he commits the layer changes.





You could also ask on the QGIS Mailing List where you could get a more explicit reason.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...