1

I am working in c++/qt 4.5 and qgis api c++ 2.8.2 I had to create and empty vectorLayer and add Point I saw in the qgis cookbook that it is possible to create a vector layer in memory ,I did that, and export to a file I did that too and It Works.

Here is , in detailled step , what I want to do :

a- create a vectorlayer in memory

b- add attributes to it

c- add features to it

d- save it in a shp file to be imported in QGIS

I don't know how to add attribute and set value .

is there a specific procedure ?

do you have somme link or some sample ?

1

Here is one solution, perhaps there is other but this one works

first create the layer :

qDebug() << "Create Memory Layer Tracks";
QgsVectorLayer * lVectorLayer = new QgsVectorLayer("Point","TRACKS","memory");
lVectorLayer->isValid();
qDebug() << "Vector valid :" << lVectorLayer->isValid();
QgsVectorDataProvider * lDataProvider = lVectorLayer->dataProvider();
// editing mode
lVectorLayer->startEditing();

// add attribute
QList <QgsField> lMyAttribField;
// cerate fields
lMyAttribField  << QgsField("TrackId",QVariant::Int)
    << QgsField("Label",QVariant::String)   
    << QgsField("Type",QVariant::String)    
    << QgsField("Color",QVariant::String)   
    << QgsField("PosX",QVariant::Double)    
    << QgsField("PosY",QVariant::Double)    
    << QgsField("TimeStamp",QVariant::String);  

    lDataProvider->addAttributes(lMyAttribField);
    // save attributes
   lVectorLayer->commitChanges();

    // add to the map
    QgsMapLayerRegistry::instance()->addMapLayer(lVectorLayer);
    // insert the vector layer into the vectorlayermap
    mMapLayer.insert(pType, lVectorLayer);

Then you can use it to add feature like this :

QgsVectorLayer * lLayer = mMapLayer.value(TRACKS);

if (lLayer == NULL) qDebug() << " lLayer NULL";
else qDebug() << "lLayer Not NULL";

qDebug() << "Vector is Valid ? ::" <<   lLayer->isValid();

QgsVectorDataProvider * lDataProvider;
lDataProvider = lLayer->dataProvider();

//calc random X and Y
PosX =(qrand()%(Xmax-Xmin)+Xmin);
PosY =(qrand()%(Ymin-Ymax)+Ymax);

//create point
QgsGeometry * MyPoint;
MyPoint = QgsGeometry::fromPoint(QgsPoint(PosX,PosY));
QgsFeature MyFeature;
MyFeature.setGeometry(MyPoint);

// set the value
MyFeature.initAttributes(n);
MyFeature.setAttribute(Name,"test");
MyFeature.setAttribute(TrackId , mId) ;
MyFeature.setAttribute(Type , lClassif);
MyFeature.setAttribute(PosY,PosY );
MyFeature.setAttribute(PosX,PosX);
QDateTime MyTime;
MyFeature.setAttribute(Heure , MyTime.currentDateTime().toString("dd.MM.yyyy hh:mm:ss.zzz"));
MyFeature.setValid(true);

QgsFeatureList MyFeatureList;
MyFeatureList.append(MyFeature);
lDataProvider->addFeatures(MyFeatureList);

lLayer->updateExtents();

// Add the Vector Layer to the Layer Registry
QgsMapLayerRegistry::instance()->addMapLayer(lLayer, TRUE);
// Add the Layer to the Layer Set
QgsMapCanvasLayer PointMapCanvasLayer = QgsMapCanvasLayer(lLayer, TRUE);
mpS57->myListForPrint.insert(1,PointMapCanvasLayer);
// set the canvas to the extent of our layer
mpMapCanvas->setExtent(lLayer->extent());
// Set the Map Canvas Layer Set
mpMapCanvas->setLayerSet(mpS57->myListForPrint);
// zoom full canvas
mpMapCanvas->zoomToFullExtent();
mpMapCanvas->refresh();
mpMapCanvas->updateMap();
mpMapCanvas->updateFullExtent();

I hope that this sample help you in the future

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.