1

I would Like to adapt an old code in c++ using the api qgis 1.6 :

const QgsRenderer* lrenderer = pLayer->renderer();
if( lrenderer == NULL )
    lrenderer = new QgsSingleSymbolRenderer( pLayer->geometryType() );
QList<QgsSymbol*> lSymbols      = lrenderer->symbols();
QgsSymbol* lsy = lSymbols.at(0);
lrenderer = new QgsUniqueValueRenderer( QGis::Polygon );
QStringList lListValue; lListValue  << "0" << "3" << "5"<< "10";
for(int i = 0 ; i < lListValue.size() ; i++) {
    QgsSymbol* newsymbol = new QgsSymbol( *lsy);
    newsymbol->setLowerValue( QString::number(i) );
    newsymbol->setFillColor(QColor(115, 182, 239));
    newsymbol->setColor( QColor(125, 137, 140) );
    newsymbol->setLineWidth(0.3);
    ((QgsUniqueValueRenderer*)lrenderer)->insertValue( lListValue.at(i), newsymbol ); //A
}
((QgsUniqueValueRenderer*)lrenderer)->updateSymbolAttributes(); //B
((QgsUniqueValueRenderer*)lrenderer)->setClassificationField(lMapField.value("DRVAL2").toInt() ); //C

This is a qgis api 1.6 c++ code Now I want to do the same thing using the qgis 2.8.2 api c++ I have started to code it :

lrenderer = QgsFeatureRendererV2::defaultRenderer( QGis::Polygon );
QList<QgsSymbolV2*>         lSymbols    = lrenderer->symbols();
QList<QgsSymbolV2*>         lSymbolsToRender    = lrenderer->symbols();
QStringList lListValue;
lListValue  << "0" << "3" << "5"<< "10";

for(int i = 0 ; i < lListValue.size() ; i++) {
    QgsSymbolV2* newsymbol = lSymbols.at(0);
    QgsSymbolLayerV2List  lSymbolLayerList = newsymbol->symbolLayers();
    QgsLineSymbolV2 *MyLine = new QgsLineSymbolV2(lSymbolLayerList);
    MyLine->setWidth(0.3);
    QgsSymbolLayerV2 * lSymbolLayer;
    lSymbolLayer = lSymbolLayerList.at(0);
    lSymbolLayer->setFillColor(QColor(115, 182, 239));
    lSymbolLayer->setColor( QColor(125, 137, 140) );
    newsymbol->insertSymbolLayer(lListValue.at(i).toInt(), lSymbolLayer);
    lSymbolsToRender.append(newsymbol);
}

I don't know how i can code the A, B and C comment of the old code ? Does anyone have sample on how to renderer the deep of the sea with different color, ie level 0 is light blue 10 feet deep is blue and more is dark blue

1

Here is the code that could be used.

The code is base on the reading of a s57 map .

QList<QgsSymbolV2*>         lSymbols    = lrenderer->symbols();
QList<QgsSymbolV2*>         lSymbolsToRender    = lrenderer->symbols();
QStringList lListValue;
lListValue  << "0" << "3" << "5" << "10" << "15" << "20" << "30" << "35" << "40" << "50" << "100" << "200";

QStringList MyColor;
MyColor << "#b4e6ee" << "#a0c8ee" << "#82a0ee" << "#6478ee" << "#646eee" << "#6464ee" << "#5a64ee" << "#505aee" << "#5050ee"<< "#4646ee"<< "#3c46ee"<< "#3c3cee";
QList<QgsRendererCategoryV2> Categories;

qDebug() << "ColorSize =" << QString::number(MyColor.size()) << "   ValueSize=" << QString::number(lListValue.size());
for(int i = 0 ; i < lListValue.size() ; i++) {
    QgsSymbolV2* newsymbol = QgsSymbolV2::defaultSymbol(QGis::Polygon );
    newsymbol->setColor(QColor(MyColor.at(i)));
    qDebug() << "i=" << QString::number(i) << "   listValue=" << lListValue.at(i) << "  color=" <<MyColor.at(i);
    QgsRendererCategoryV2 MyCategorie = QgsRendererCategoryV2(QVariant( lListValue.at(i) ),newsymbol,lListValue.at(i));
    Categories.append(MyCategorie);
}
lrenderer = new QgsCategorizedSymbolRendererV2("DRVAL1",Categories);
pLayer->setRendererV2(lrenderer);

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.