Monday, 3 December 2018

Is it possible to take automatically a "screenshot" of my selected feature in QGIS using Python?



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?



Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...