2

I am working in c++ qt4.8.5 and api qgis 2.8.2 c++. In my application I had to put polygon to define attractive zone. I found a great post in stackexchange (86812). I have based my on this post and on the pyqgis cookbook, but in c++ it seems that it is more difficult. I create a layer in memory with a polygon type, then i create a function to generate ( for the moment ) a random polygon shape with 10 points. There is no error in the build, when i call the function there is nothing on the display. I check the coordinate of the point it is ok.

here is my code

create the polygon layer :

QgsVectorLayer * lVectorLayer = new QgsVectorLayer("Polygon",pType,"memory");
lVectorLayer->isValid();
qDebug() << "Vector valid :" << lVectorLayer->isValid();
// add to the map
QgsMapLayerRegistry::instance()->addMapLayer(lVectorLayer);
// insert the vector layer into the vectorlayermap
mMapLayer.insert(pType, lVectorLayer);
// Add the Layer to the Layer Set
QgsMapCanvasLayer PolygonLayer = QgsMapCanvasLayer(lVectorLayer, TRUE);
mpS57->myListForPrint.insert(1,PolygonLayer);
// Set the Map Canvas Layer Set
mpMapCanvas->setLayerSet(mpS57->myListForPrint);

then the function to create the random polygon :

QgsVectorLayer * lLayer = mMapLayer.value(pType);
if (lLayer == NULL) qDebug() << " lLayer NULL";
else qDebug() << "lLayer Not NULL";

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

QgsVectorDataProvider * lDataProvider = lLayer->dataProvider();

QgsFeature MyPoly = QgsFeature();

QgsPoint lPoint2;
QgsPolyline lpolyline;
QgsPolygon lPolygon;
// origin point
QgsPoint lOrgPoint;
QgsPoint lPoint1;
PosX =(qrand()%(Xmax-Xmin)+Xmin);
PosY =(qrand()%(Ymin-Ymax)+Ymax);
PosX=PosX / 100000;
PosY=PosY/10000;
lOrgPoint = QgsPoint(PosX,PosY);
lPoint1 = lOrgPoint;
// add 10 points
for (int i=0; i<10; i++) {
    //calc new random X and Y
    PosX =(qrand()%(Xmax-Xmin)+Xmin);
    PosY =(qrand()%(Ymin-Ymax)+Ymax);
    PosX=PosX / 100000;
    PosY=PosY/10000;
    lPoint2=QgsPoint(PosX,PosY);
    // create the line
    lpolyline.append(lPoint1);
    lpolyline.append(lPoint2);
    // insert the line in polygon
    lPolygon.append(lpolyline);
    // clear the line
    lpolyline.clear();
    //replace lpoint1 by lpoint2
    lPoint1 = lPoint2;
}
// close the polygon
// create the line
lpolyline.append(lPoint1);
lpolyline.append(lOrgPoint);
// insert the line in polygon
lPolygon.append(lpolyline);
// clear the line
lpolyline.clear();

MyPoly.setGeometry(QgsGeometry::fromPolygon(lPolygon));

QgsFeatureList lPolyList;
lPolyList << MyPoly;
lDataProvider->addFeatures(lPolyList);
lLayer->updateExtents();
QgsMapLayerRegistry::instance()->addMapLayer(lLayer);

as you can see, in c++ is more complex than in python i try to put a list of point in the frompolygon(listofpoint) but there is an building error in c++ we had to respect the type, in this a QgsPolyline.

I don't understand why tehre is nothing on the display there is the update, the layer is added to the map, the feature is added to the dataprovider, everything seems to be right but nothing.

any idea ?

added information 09-10 : I have made other test with a simple line, same result, but with the same list of point if I use the qgsrubberband, the rubberband is displayed fine on the map, the test with the rubberband confirm that the list of point are correct.

1 Answer 1

2

I found the error the first is at the beginning, I add the vectorlayer to the map without fit it with data, so nothing can be displayed because the setgeometry is done after the insert. In fact I add in the mapLayer the vectorlayer too soon so now in a simply way I have this: create the vector change data and geometry add to the layer render

Here is a new code that Works :

// ajout ligne
QgsVectorLayer * lVectorLayer2 = new QgsVectorLayer("LineString?crs=epsg:4326",pType,"memory");
lVectorLayer2->isValid();

QgsPolyline lPolyLine;
QgsPolygon MyPolygon;
QgsPoint lPoint1=QgsPoint(5.90607,43.109);
QgsPoint lPoint2=QgsPoint(5.92016,43.109);
QgsPoint lPoint3=QgsPoint(5.92016,43.1041);
QgsPoint lPoint4=QgsPoint(5.90607,43.1041);

lPolyLine << lPoint1 << lPoint2  << lPoint2 << lPoint3 << lPoint3 << lPoint4 << lPoint4 << lPoint1;
MyPolygon << lPolyLine;

QgsVectorDataProvider *lDataProvider = lVectorLayer2->dataProvider();

QgsGeometry * lPoly;
lPoly = QgsGeometry::fromPolyline(lPolyLine);

QgsFeature lFeature;
lFeature.setGeometry(lPoly);

QgsFeatureList lFeatureList;
lFeatureList.append(lFeature);
lDataProvider->addFeatures(lFeatureList);
lVectorLayer2->updateExtents();

// ajout à la map
QgsMapLayerRegistry::instance()->addMapLayer(lVectorLayer2,TRUE);
// insert the vector layer into the vectorlayermap
mMapLayer.insert(pType, lVectorLayer2);
// Add the Layer to the Layer Set
mpS57->myListForPrint.insert(0,QgsMapCanvasLayer(lVectorLayer2, TRUE));

mpMapCanvas->setExtent(lVectorLayer2->extent());
// Set the Map Canvas Layer Set
mpMapCanvas->setLayerSet(mpS57->myListForPrint);

// zoom full canvas
mpMapCanvas->zoomToFullExtent();
mpMapCanvas->refresh();
mpMapCanvas->updateMap();
mpMapCanvas->updateFullExtent();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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