I am working in c++/QT 4.5 with the QGis 2.8.2 API. I had to change the geometry of features in a layer each second. Each second new data comes from the Ethernet connection, the application gets the data and calls a function with parameters.
It looks like a real time tracker.
It works only if I put lLayer->startEditing
and lLayer->commitChanges
but it was too slow.
Here is my code (just an extract. Complete code is too long for the post.)
QgsVectorLayer * lLayer = mMapLayer.value(TRACKS);
lLayer->startEditing();
QgsVectorDataProvider * lDataProvider;
lDataProvider = lLayer->dataProvider();
//calc new random X and Y
PosX =(qrand()%(Xmax-Xmin)+Xmin);
PosY =(qrand()%(Ymin-Ymax)+Ymax);
//create point
QgsGeometry *MyPoint;
MyPoint=QgsGeometry::fromPoint(QgsPoint(PosX,PosY));
QgsGeometryMap MyGeometryMap;
//get the feature of the specify Id
QgsFeatureIterator MyFeatIter;
MyFeatIter = lDataProvider->getFeatures();
QgsFeature MyFeature;
QgsFeatureId MyFeatureId;
while ( MyFeatIter.nextFeature(MyFeature)) {
if (pTrackId == MyFeature.attribute("TrackId")) {
MyFeatureId=MyFeature.id();
MyFeature.setGeometry(*MyPoint);
MyFeature.setAttribute(Longitude,PosY);
MyFeature.setAttribute(Latitude,PosX);
}
}
MyGeometryMap.insert(MyFeatureId,*MyPoint);
bool lResult;
lResult=lDataProvider->changeGeometryValues(MyGeometryMap);
lLayer->commitChanges();
With the startEditing
and commitChanges
it works, but very slow.
Without the startEditing
and commitChanges
, the coordinate seems to be modified, but on the layer the feature doesn't move.