0

In QGIS I'd like to add scale to my PyQGIS image exporter. Similar to the Project -> Import/Export -> Export map to image where you can add the scale to the image as in the picture below.

enter image description here

vLayer = QgsProject.instance().mapLayersByName("a_g")[0]
sExtent=vLayer.extent()
sOutputDpi= 300
sZoomScale = 2500
sQSize=QSize(10000, 10000)

settings = QgsMapSettings()
settings.setOutputSize(sQSize)
settings.setExtent(sExtent)
settings.outputDpi = sOutputDpi
settings.scale = sZoomScale

render = QgsMapRendererParallelJob(settings)

def finished():
    img = render.renderedImage()
    img.save(image_location, "png")

render.finished.connect(finished)

render.start()

In the output I'd like to include the scale of the map as well. At the moment the image exporter exports the image scaleless (I think) and only takes note of the setExtent value.

Does anyone know if it's possible to include scale in this script?

1 Answer 1

0

I figured a way out to do it. The extent of the layer vLayer.extent(). was bigger then the QSize of 10000px by 10000px ratio that was specified, I think that's why it scaled itself to fit in this extent.

Putting in a

extent = iface.mapCanvas().extent()

And further

settings.setExtent(extent)

Instead of the vLayer.extent() works.

Just make sure your mapCanvas is on the right scale, and then it works like a charm.

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.