1

I am working in c++/ QT4.8.5, with qgis api c++ 2.8.2

I have to display a dynamic icon for a feature. some of the data change periodically and in order to display the change i need a svg that can get the data I don't want to have a collection a svg file because there too possibilities At this point i need only two parameters but the number of parameter will be more My feature has two attributes speed and color each change the speed determine the size of the arrow in different color The color is set in the feature, it is the result of an algorythm. there is, for the moment , eigth colors and 3 type of speed ( slow, medium , fast )

do you have any idea on how can i proceed to create file an apply it on my symbol ?

  • 1
    It's fantastic that you want to share your knowledge with the community! But the primary format for Stack Exchange is to ask a question which can then be answered. At the moment, the question is in the title and not in the post. Consider editing your question to include a situation where you need a solution and then move the solution you provided into the answer section. This would be most useful to others with similar problems :) – Joseph Sep 3 '15 at 15:06
  • ok for the future i will apply your suggestion, a question or a situation and then post a solution.thx – ainardi Sep 3 '15 at 18:54
  • I have edited the post now i describe a situation and the answer is posted – ainardi Sep 3 '15 at 19:04
1

Here is a solution for whose want to create specific svg file depending on the feature data Language c++ / QT4.8.5, qgis api c++ 2.8.2

A svg file contains xml code In this exemple we put a specific color and change the use based on the 'vitesse' parameter of the feature This sample code draw 3 differents arrows : small medium and large ina 30x30 pixels matrix

here the function :

void createTrackIco(QgsFeature pFeature, int pTrackId,bool pUpdateIco)
{

    QString lColorIco;
    QString lUseIco;
    double lVitesse;

    if (pUpdateIco) {
        // get the feature data
        lColorIco = pFeature.attribute("COLOR").toString();
        lVitesse = pFeature.attribute("VITESSE").toDouble();
        if (lVitesse<10)
            lUseIco="lent";
        else if (lVitesse<20)
            lUseIco="moyen";
        else
            lUseIco="rapide";
        qDebug() << "Update Icone  Type =" << lUseIco << "  couleur =" << lColorIco  << " vitesse =" << lVitesse;

    }
    else {
        // creation type icone par defaut
        lColorIco = "black";
        lUseIco = "lent";
    }

    QString fileXmlName = "../data/Icones/iconeS52/track_"+QString::number(pTrackId)+".svg";
    QFile fileXml(fileXmlName);

     // cretae the file. 
        if(!fileXml.open(QFile::WriteOnly | QFile::Text))
            qDebug()<< "error";
        QXmlStreamWriter writer(&fileXml);
       writer.setAutoFormatting(true);
        writer.setCodec("iso-8859-1");
        // Insert xml codification norm  :
        writer.writeStartDocument();

        // root of the file XML
        writer.writeStartElement("svg");
        writer.writeAttribute("width","30");
        writer.writeAttribute("height","30");
        writer.writeAttribute("xmlns","http://www.w3.org/2000/svg");

        // Add element
        writer.writeStartElement("defs");

        // small
        writer.writeStartElement("g");
        writer.writeAttribute("id","lent");
        writer.writeStartElement("line");
        writer.writeAttribute("x1","0");
        writer.writeAttribute("y1","0");
        writer.writeAttribute("x2","15");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");
        // end of line
        writer.writeEndElement();
        writer.writeStartElement("line");
        writer.writeAttribute("x1","0");
        writer.writeAttribute("y1","30");
        writer.writeAttribute("x2","15");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");
        // end of line
        writer.writeEndElement();
        writer.writeStartElement("line");
        writer.writeAttribute("x1","15");
        writer.writeAttribute("y1","15");
        writer.writeAttribute("x2","21");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");
        // end of line
        writer.writeEndElement();
        writer.writeStartElement("line");
        writer.writeAttribute("x1","15");
        writer.writeAttribute("y1","10");
        writer.writeAttribute("x2","20");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");
        // end of line
        writer.writeEndElement();
        writer.writeStartElement("line");
        writer.writeAttribute("x1","15");
        writer.writeAttribute("y1","20");
        writer.writeAttribute("x2","20");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");
        // end of line
        writer.writeEndElement();
        // end of  g tag
        writer.writeEndElement();
        // medium
        writer.writeStartElement("g");
        writer.writeAttribute("id","moyen");
        writer.writeStartElement("line");
        writer.writeAttribute("x1","0");
        writer.writeAttribute("y1","0");
        writer.writeAttribute("x2","15");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");

        writer.writeEndElement();
        writer.writeStartElement("line");
        writer.writeAttribute("x1","0");
        writer.writeAttribute("y1","30");
        writer.writeAttribute("x2","15");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");

        writer.writeEndElement();
        writer.writeStartElement("line");
        writer.writeAttribute("x1","15");
        writer.writeAttribute("y1","15");
        writer.writeAttribute("x2","25");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");

        writer.writeEndElement();
        writer.writeStartElement("line");
        writer.writeAttribute("x1","15");
        writer.writeAttribute("y1","10");
        writer.writeAttribute("x2","25");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");
        // end of line
        writer.writeEndElement();
        writer.writeStartElement("line");
        writer.writeAttribute("x1","15");
        writer.writeAttribute("y1","20");
        writer.writeAttribute("x2","25");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");
        // end of line
        writer.writeEndElement();
        // end of g tag
        writer.writeEndElement();

        // large
        writer.writeStartElement("g");
        writer.writeAttribute("id","rapide");
        writer.writeStartElement("line");
        writer.writeAttribute("x1","0");
        writer.writeAttribute("y1","0");
        writer.writeAttribute("x2","15");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");
        // end of line
        writer.writeEndElement();
        writer.writeStartElement("line");
        writer.writeAttribute("x1","0");
        writer.writeAttribute("y1","30");
        writer.writeAttribute("x2","15");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");
        // end of line
        writer.writeEndElement();
        writer.writeStartElement("line");
        writer.writeAttribute("x1","15");
        writer.writeAttribute("y1","15");
        writer.writeAttribute("x2","30");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");
        // end of line
        writer.writeEndElement();
        writer.writeStartElement("line");
        writer.writeAttribute("x1","15");
        writer.writeAttribute("y1","10");
        writer.writeAttribute("x2","30");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");
        // end of line
        writer.writeEndElement();
        writer.writeStartElement("line");
        writer.writeAttribute("x1","15");
        writer.writeAttribute("y1","20");
        writer.writeAttribute("x2","30");
        writer.writeAttribute("y2","15");
        writer.writeAttribute("style","stroke:"+lColorIco+"; stroke-width:2");
        // end of line
        writer.writeEndElement();
        // end of g tag
        writer.writeEndElement();
        // end of  defs tag
        writer.writeEndElement();
        // start use
        writer.writeStartElement("use");
        writer.writeAttribute("xlink:href","#"+lUseIco);
        writer.writeAttribute("x","0");
        writer.writeAttribute("y","0");
        // end of svg tag
        writer.writeEndElement();

        // write data  to XML file
        writer.writeEndDocument();

        //close the file and close root element
        fileXml.close();
}

you can explore this sample, improve it, or just for help Enjoy.

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.