1

I have a vectorlayer that contains a lot of feature, I know that each feature has an unique Id

Each of them has some propertie like coordinateX, coordinateY, color, name

With a QTimer, each 2 sec I want to change the position of one of them by change the coordinate using the QgsGeometry::fromPoint(x,y), i. e. the feature which has the Id=4 and update the Mapcanvas with the updated feature.

How Can i get the feature of a given Id ? And How can i proceed to update the mapcanvas with the change in the feature attributes ?

08/28 :added information extract of my code , I am still working on it to find the solution :

//create point
QgsGeometry MyPoint;
MyPoint.fromPoint(QgsPoint(PosX,PosY));
QgsGeometryMap MyGeometryMap;
QgsFeatureId MyFeatureId=1;

MyGeometryMap.insert(MyFeatureId,MyPoint);

lDataProvider->changeGeometryValues(MyGeometryMap);
lLayer->updateExtents();

the data seems to be correct, PosX and PosY are random double, a qDebug show the value before store into the Geometry and it is correct but the symbol don't move or if you prefer the symbol is not updated

Here is another code but still no change on the screen :

//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")) {
        qDebug() << "Feature Trouve  Id=" << MyFeature.id();
        MyFeatureId=MyFeature.id();
        MyFeature.setGeometry(MyPoint);
        MyFeature.setAttribute(Longitude,PosY );
        MyFeature.setAttribute(Latitude,PosX);
        MyFeature.setValid(true);
        qDebug()<< "TrackId=" << MyFeature.attribute("TrackId") << "  Label=" << MyFeature.attribute("Label")
                << "  PosX = " << MyFeature.attribute("PosX") << "  PosY=" << MyFeature.attribute("PosY");
    }
}

MyGeometryMap.insert(MyFeatureId,MyPoint);
bool lResult;
lResult=lDataProvider->changeGeometryValues(MyGeometryMap);

I try to set the geometry on the feature founded but no change,I've tried getFeatures on the dataprovider still the same result, coordinate change correctly but on the screen the feature don't change

  • Welcome to gis.stackexchange! Please note that a good question on this site is expected to show some degree of research on your part, i.e. what you have tried and - if applicable - code so far. For more info, you can check our faq. – underdark Aug 27 '15 at 20:28
  • ok sorry, tomorrow i add my code – ainardi Aug 27 '15 at 21:04
0

here is a sample of my code

it works great with my application, you can use it , but don't forget to adapt to your project, or application

it is a sample

QgsVectorLayer * lLayer = mMapLayer.value(pTrack);
lLayer->startEditing();
QgsVectorDataProvider * lDataProvider = lLayer->dataProvider();

//calc new random X and Y
PosX = (qrand() % (Xmax - Xmin) + Xmin);
PosY = (qrand() % (Ymin - Ymax) + Ymax);
// init vitesse aleatoire
double lVitesse = qrand() % (30);
// couleur
QString lColor;
// init classification
int lClassif = qrand() % (5);
switch (lClassif) {
case 0:
    lColor = "black";
    break;
case 1:
    lColor = "green";
    break;
case 2:
    lColor = "blue";
    break;
case 3:
    lColor = "orange";
    break;
case 4:
    lColor = "red";
    break;
default:
    lColor = "black";
}
// init cap aleatoire
double lCap;
lCap = qrand() % (360);
//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;

// search the feature for the id
while (MyFeatIter.nextFeature(MyFeature)) {
    qDebug() << "Feature Trouve  Id=" << MyFeature.id();
    MyFeatureId = MyFeature.id();
    MyFeature.setGeometry(*MyPoint);
    MyFeature.setAttribute(Longitude, PosY); // longitude
    MyFeature.setAttribute(Latitude, PosX); // latitude
    MyFeature.setAttribute(Vitesse, lVitesse); // vitesse
    MyFeature.setAttribute(Classification, lClassif); // classif ou type
    MyFeature.setAttribute(Cap, lCap); // cap
    MyFeature.setAttribute(Couleur, lColor); // couleur
    MyFeature.setValid(true);
}
// update the attribute
QgsAttributeMap MyAttributeMap;
//MyAttributeMap.insert(0,MyFeatureId);
MyAttributeMap.insert(lDataProvider->fieldNameIndex("LABEL"),MyFeature.attribute("LABEL"));
MyAttributeMap.insert(lDataProvider->fieldNameIndex("TYPE"),MyFeature.attribute("TYPE"));
MyAttributeMap.insert(lDataProvider->fieldNameIndex("COLOR"),MyFeature.attribute("COLOR"));
MyAttributeMap.insert(lDataProvider->fieldNameIndex("POSX"),MyFeature.attribute("POSX"));
MyAttributeMap.insert(lDataProvider->fieldNameIndex("POSY"),MyFeature.attribute("POSY"));
MyAttributeMap.insert(lDataProvider->fieldNameIndex("CAP"),MyFeature.attribute("CAP"));
MyAttributeMap.insert(lDataProvider->fieldNameIndex("VITESSE"),MyFeature.attribute("VITESSE"));
MyAttributeMap.insert(lDataProvider->fieldNameIndex("TIMESTAMP"),MyFeature.attribute("TIMESTAMP"));
QgsChangedAttributesMap MyChangedAttributesMap;
MyChangedAttributesMap.insert(MyFeatureId, MyAttributeMap);
MyGeometryMap.insert(MyFeatureId, *MyPoint);

bool lResult;
lResult = lDataProvider->changeGeometryValues(MyGeometryMap);
lResult = lDataProvider->changeAttributeValues(MyChangedAttributesMap);
// save all change
lLayer->commitChanges();

createTrackIco(MyFeature, MyFeatureId, true);
// update the track
UpdateTrack(lLayer, MyFeature);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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