[Qgis-user] Fail to show markers in a vector layer
Simone Melis
simone.melis at leonardo-si.it
Thu Jan 11 08:27:02 PST 2024
Hi,
I'm Simone Melis and I'm sw engineer in an Italian company.
I' m using QGis API 3.26 on Ubuntu 20.04, to create an application GUI
using Qt 5.15.2
I created a vector layer and I can show correctly a georeferenced pdf
image.
Then I added another transparent layer (vector layer) and I tried to
add markers in lat,long points.
Using QgsSimpleMarkerSymbolLayer I can inserted correctly all markers of
the same shape and color. Then I tried to add markers with different
shapes and colors on the same layers; using QgsSimpleMarkerSymbolLayer I
show only one kind of marker. Browsing on internet I found an example to
use QgsCategorizedSymbolRenderer. I tried to implement it in my code but
no markers shown. Can you suggest a correct way to do this?
Thanks,
Simone
Here code I implemented:
doublelatitude[4]={44.095003,44.095103,44.095203,44.094811};
doublelongitude[4]={9.858660,9.859060,9.859460,9.859034};
//
//QgsCategorizedSymbolRenderer
//
transparentLayerBSPRX->dataProvider()->addAttributes({QgsField("Category",QVariant::String),
QgsField("Name",QVariant::String),
QgsField("Latitude",QVariant::Double),
QgsField("Longitude",QVariant::Double),
QgsField("Type",QVariant::String)});
//Setupthedefaultsymbol
QgsSimpleMarkerSymbolLayer*BSPRX_SymbolLayer=newQgsSimpleMarkerSymbolLayer();
BSPRX_SymbolLayer->setShape(QgsSimpleMarkerSymbolLayer::Circle);
BSPRX_SymbolLayer->setSize(5.0);
BSPRX_SymbolLayer->setColor(Qt::blue);
QgsSymbol*def_symbol=QgsSymbol::defaultSymbol(QgsWkbTypes::PointGeometry);
def_symbol->changeSymbolLayer(0,BSPRX_SymbolLayer);
for(inti=0;i<4;i++)//Correctedloopcondition
{
QgsFeaturefeature(transparentLayerBSPRX->dataProvider()->fields());
if(i<1)
{
//Createanewfeature
feature.setAttribute("Category","A");
feature.setAttribute("Name","RX");
feature.setAttribute("Latitude",latitude[i]);
feature.setAttribute("Longitude",longitude[i]);
feature.setAttribute("Type","BSPRX");
def_symbol->symbolLayers()[0]->setColor(Qt::red);
}
else
{
//Createanewfeature
feature.setAttribute("Category","B");
feature.setAttribute("Name","TX");
feature.setAttribute("Latitude",latitude[i]);
feature.setAttribute("Longitude",longitude[i]);
feature.setAttribute("Type","BSPTX");
def_symbol->symbolLayers()[0]->setColor(Qt::blue);
}
//Addthefeaturetothelayer
transparentLayerBSPRX->dataProvider()->addFeatures(QgsFeatureList()<<feature);
transparentLayerBSPRX->addAttribute(*newQgsField("color",QVariant::String));
qDebug()<<"FeatureAttributes:"<<feature.attributes();
}
QgsCategorizedSymbolRenderer*categorizedRenderer=newQgsCategorizedSymbolRenderer("Category");
QgsRendererCategorybsprxCategory("A",def_symbol->clone(),"RX",true);
categorizedRenderer->addCategory(bsprxCategory);
QgsRendererCategorybsptxCategory("B",def_symbol->clone(),"TX",true);
categorizedRenderer->addCategory(bsptxCategory);
//Printsymbolsfordebugging
QList<QgsRendererCategory>categories=categorizedRenderer->categories();
for(constQgsRendererCategory&category:categories)
{
qDebug()<<"Category:"<<category.label()<<"Symbol:"<<category.symbol()->color();
}
//Applytherenderertothelayer
transparentLayerBSPRX->setRenderer(categorizedRenderer);
--
--
Questa email è stata esaminata alla ricerca di virus dal software antivirus Avast.
www.avast.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20240111/9a37e42b/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: FirmaPosta_MelisSimone.jpg
Type: image/jpeg
Size: 19509 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20240111/9a37e42b/attachment-0001.jpg>
More information about the QGIS-User
mailing list