1

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)
        h.setColor(QColor(255,0,0))
        h.setWidth(10)

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

mapSettings = canvas.mapSettings()
mapSettings.setOutputSize(QSize(1600,1100))
mapSettings.setOutputDpi(200)

render = QgsMapRendererSequentialJob(mapSettings)
render.start()
render.waitForFinished()

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

0

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]
couche_parcelle.selectByIds(liste_selec)
#couche_parcelle.selectedFeatureIds()

#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()))
QgsProject.instance().addMapLayer(nouvelle_couche)
nouvelle_couche.setName("parcelle_duplica")

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()
props['outline_color']='255,0,0,255'
props['outline_width']='3'
nouvelle_couche.renderer().setSymbol(QgsFillSymbol.createSimple(props))
nouvelle_couche.triggerRepaint()

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, privacy policy and cookie policy

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