I'm attempting to create a plugin in QGIS 3 in order to zoom on a selected feature and get the map picture exported in .jpg format.

I've been able to perform the zoom and selection of my selection, and now I want to add some color border to the selected feature using QgsHightlight :

def highlight_features():
    for i in couche_parcelles.selectedFeatures():
        h = QgsHighlight(canvas,i.geometry(),couche_parcelles)

#================================Map Creation==================================
#project = QgsProject.instance()

mapSettings = canvas.mapSettings()

render = QgsMapRendererSequentialJob(mapSettings)

dir = r"C:\Users\[...]\Desktop"

img = render.renderedImage()
img.save(dir + "/" + "selection.jpg",'jpg')

It does make a pretty highlight border on QGIS software like this :

QGIS map highlight But then, once I get the map rendered, here's what I get generated by the script :

Map extraction from script

My question is, how can I get the highlight extracted with my selection on the map ? Because I'm not sure it has something to do with my canvas and I don't know if it's possible to embed my QgsHighlight in my canvas either

I've already seen this answer : Setting selection color transparent and border color red using PyQGIS? Which helped me to get the highlight on my selection borders but I can't figure out how to integrate it in my map rendering

1 Answer 1


Alright came up with another solution :

Instead of using QgsHighlight in order to get a border out of the selected feature, I've created a layer of my selected feature :

couche_parcelle = QgsProject.instance().mapLayersByName('3_parcelles_aix.shp')[0]
expr = "\"(attribute_table_column)\"='(fixed value)'"
selec = QgsExpression(expr)
selec_couche = couche_parcelle.getFeatures(QgsFeatureRequest(selec))
liste_selec = [i.id() for i in selec_couche]

#Once the feature is selected, I grab the selected feature and create a temporary layer in memory
nouvelle_couche = couche_parcelle.materialize(QgsFeatureRequest().setFilterFids(couche_parcelle.selectedFeatureIds()))

Once done, I take the properties of my new layer nouvelle_couche in a variable I'll call props:

props = nouvelle_couche.renderer().symbol().symbolLayer(0).properties()

Finally, it appears and gets on the generated map rendering, it's been tough but now it works

Your Answer

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

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