I am trying to add / Update / Delete features in a QgsVectorLayer in QGIS.
I'm using this code:
QgsFeatureIds FeatureIds=PointLayer->allFeatureIds();
if (FeatureIds.contains(Id.toInt())) {
QgsFeature feature=PointLayer->getFeature(Id.toInt());
feature.setGeometry(QgsGeometry::fromPointXY(QgsPointXY(longitude,latitude)));
PointLayer->updateFeature(feature);
} else {
QgsFeature feature;
feature.setId(Id.toInt());
feature.setGeometry(QgsGeometry::fromPointXY(QgsPointXY(longitude,latitude)));
PointLayer->dataProvider()->addFeature(feature);
}
and for clearing the layer
PointLayer->dataProvider()->trucate()
Note: I will truncate if the data gets overcrowded that's why I am using truncate().
Initially while adding the features using
PointLayer->dataProvider()->addFeature(feature);
the feature id which I pass is same even after addFeature is called Now after truncating, if I want to add feature then its feature id changes
i.e if I call this code:
PointLayer->dataProvider()->addFeature(feature);
The id looking as if it's an auto-increment number.
How to reset the Id of the feature so that what ever I pass stays which is happening initially.
If solution is available in pyqgis also is ok for me.