12

I am implementing a tool which allows the user to create a profile of a feature (in my case urban parcels). Is it possible to automatically take a screenshot of this feature (displayed in a QGIS window) and to display it in the PDF profile?

The picture should only show the extent of the feature and maybe a WMS-layer that lies beyond the feature. Are there already some tools which you could suggest?

1
  • How can I save a JPG instead of an PNG? QgsMapCanvas.saveAsImage (QString theFileName, QPixmap *QPixmap=0, QString="PNG") I know that I can use "JPG" as last parameter, but I dont know how to define the QPixmap that is excatly the same like the actual map window in qgis. Thanks for help. Micha
    – gismic
    Jan 17, 2013 at 7:08

1 Answer 1

10

kopi,

There are several approaches to a solution, but this may be one of the simplest (depending upon your needs):

Select the feature using QgsVectorLayer

Assuming a reference to your vector layer (vlayer) and the feature's id (fid). See also

vlayer.select(fid)

Zoom to selected feature using QgsInterface

qgis.utils.iface.actionZoomToSelected().trigger()

Deselect feature using QgsVectorLayer

vlayer.deselect(fid)

Save canvas as picture using QgsMapCanvas

qgis.utils.iface.mapCanvas().saveAsImage('/Users/me/Desktop/feature.png')

This approach produces as large a photo as possible, if your window is expanded to full, because saving the canvas is at screen resolution. However, it does not ensure your pictures will be at the same map zoom scale, only relatively the same pixel size. There are other options like QgisInterface::actionPanToSelected() if you need to keep the zoom scale consistent.

8
  • Wow. Thank you for your answer! This works very good!
    – kopi
    Aug 22, 2012 at 20:26
  • 1
    have a look also to the "easy print" plugin, probably not what you need, but it can be useful for inspiration. Aug 23, 2012 at 8:21
  • Is it possible to set the scale before taking the screenshot? And how can I use "actionPanToSelected"? I am trying it this way: qgis.utils.iface.actionPanToSelected(feature which the function has to pan to...), but this returns an error message: "Attritbute Error: QgisInterface object has no attribute actionPanToSelected
    – kopi
    Aug 23, 2012 at 12:44
  • 1
    Try qgis.utils.iface.mapCanvas().zoomScale(scale) to set map scale. actionPanToSelected() doesn't take a parameter, but returns a reference to the action itself (like actionZoomToSelected() above). Use its public slot qgis.utils.iface.actionPanToSelected().trigger() to fire the action, which will handle figuring out what is selected.
    – dakcarto
    Aug 23, 2012 at 16:10
  • 1
    Hmmm. qgis.utils.iface.actionPanToSelected().trigger() works for me under QGIS version 1.8.0. Are you using the latest stable version (1.8.0)? The feature was added back in January, so it's not in version 1.7.x.
    – dakcarto
    Aug 23, 2012 at 17:32

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.