1

I need to show several SVG icons (more than 388 icons) with different name on a single layer(if possible on memory layer). I am using QGis 2.8 and qt4.8.6 The icons represent different kind of entities such as: plane, radar, weapons and many more... They will be added to the layer at run time based on some preconditions. I've tried using categorized symboling but it only works for different symbols for different values, correct me if I am wrong. This is what I've tried so far and also tried categorized symboling but can't display the icons the way i want them to be displayed.

  `QString IcoType1 = "Path to my icons";
    QString src = "D:/NYC_MUSEUMS_GEO/NYC_MUSEUMS_GEO.shp";
    QgsVectorLayer *layer2 = new QgsVectorLayer(src,"Symbol Layer","ogr");

    QStringList MyPropertyName;
    MyPropertyName << "fill" << "name" << "outline" << "outline-width" << "size";
    // list of corresponding value 1
    QStringList MyPropertyValue;
    MyPropertyValue << "#f3e54d" << lBoyIcoType1 << "#000000" << "0" << "20";
    QgsStringMap PropertyMap;
    for (int i =0; i < MyBoyPropertyName.size(); i++){
        PropertyMap.insert(MyPropertyName.at(i),MyPropertyValue.at(i));
    }

    QgsSymbolLayerV2* MySymbolLayer = QgsSvgMarkerSymbolLayerV2::create(PropertyMap);

    layer2->rendererV2()->symbols(QgsRenderContext())[0]->changeSymbolLayer(0,MySymbolLayer);
    layer2->rendererV2()->symbols(QgsRenderContext())[0]->appendSymbolLayer(MySymbolLayer);
    QgsMapLayerRegistry::instance()->addMapLayer(layer2,TRUE);
    QgsMapCanvas *canvas = new QgsMapCanvas(this);
    QList<QgsMapCanvasLayer> layers;
    layers.append(QgsMapCanvasLayer(layer2,TRUE));
    canvas->setLayerSet(layers);
    canvas->setExtent(layer2->extent());
    showMaximized();
    setCentralWidget(canvas);
`
0

Instead of using layer for the svg icons, I used QgsSvgAnnotationItem to drow directly on the canvas.Below is the description I've found from QGis API Documentation. QgsSvgAnnotationItem Inheritance diagram

An annotation item can be either placed either on screen corrdinates or on map coordinates. It may reference a feature and displays that associatiation with a balloon like appearance.

And Here is a sample code that works for my need as described in the question section. In the main window constructor paste the code below and try to see the result

QString iconPath = "Asset 4.svg";//Change it to your svg path
QgsMapCanvas *canvas = new QgsMapCanvas(this);
QgsSvgAnnotationItem *svg_annotation = new QgsSvgAnnotationItem(canvas);
QgsPoint point(0,0);
svg_annotation->setMapPosition(point);
svg_annotation->setFrameSize(QSizeF(100, 100));
//svg_annotation->setFrameColor(QColor(0, 255, 0));//I wanted to use my svg color so no need to set it here
//svg_annotation->setFrameBackgroundColor(QColor(128, 128, 128));//no need in my case
svg_annotation->setFilePath(iconPath );
svg_annotation->setOffsetFromReferencePoint(QPointF(0,0));//if you need to display your svg some points away from the position you set, you can give it any number you want here
canvas->refresh();

QString layerPath = "ne_10m_populated_places.shp";//change it to your vector layer path
QgsVectorLayer *layer2 = new QgsVectorLayer(layerPath ,"Symbol Layer","ogr");
QgsMapLayerRegistry::instance()->addMapLayer(layer2,TRUE);
canvas->setCrsTransformEnabled(TRUE);
canvas->setDestinationCrs(layer2->crs());
QList<QgsMapCanvasLayer> layers;
layers.append(QgsMapCanvasLayer(layer2,TRUE));
canvas->setLayerSet(layers);
canvas->setExtent(layer2->extent());
canvas->antiAliasingEnabled();
showMaximized();
setCentralWidget(canvas);

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.