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