5

I'm trying to set the extent of a square feature to the canvas, that works in QGIS, but when I print the canvas into a PDF, it always gives me some empty area, below is the screenshot of QGIS canvas having feature extent:

enter image description here

and below is the resulting PDF having empty spaces:

enter image description here

the pyqgis code used to get these results:

from qgis.core import *  

mr=iface.mapCanvas().mapRenderer()  
composition = QgsComposition(mr)
composition.setPaperSize(220, 220)
composerMap=QgsComposerMap(composition,0,0,composition.paperWidth(),composition.paperHeight())
composition.addItem(composerMap)

layer =  QgsVectorLayer('Polygon?crs=EPSG:4326', 'poly' , "memory")
pr = layer.dataProvider() 
poly = QgsFeature()
poly.setGeometry(QgsGeometry.fromWkt("POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))"))
pr.addFeatures([poly])
layer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayers([layer])
bb =  poly.geometry().boundingBox()
iface.mapCanvas().setExtent(bb)
iface.mapCanvas().refresh()
composition.refreshItems()
composition.exportAsPDF('test.pdf')
  • Check the boundingbox values. It might whisper some info. Are there the coords, you want? – procma Sep 16 '15 at 8:59
  • your square output is going onto a default page size in the pdf. which is probably letter size and is a rectangle. It will be up to you to manipulate the pdf output page and the juxtposition of data on that output. there are controls for location centered, top, bottom, left, right. Or you can manipulate the page size and create a custom square page or rotate to landscape. Lots of options. – Brad Nesom Sep 16 '15 at 14:32
  • 1
    So just to clarify, you want to print the rectangle one a pdf page, so that it fills the hole A4 page? I had a similar Problem like this for my Masterthesis with QGIS. What i did was. Create a new print composer. There add a new Map, which will have the rectangle. Then click on "move item content" and move the rectangle in the Maparea to the center. Then go to the Item properties of the maparea and play with the scale. I hope I got you question right. – Benjamin Sep 18 '15 at 11:43
  • 1
    My suspicion goes to the composer map extent. The composerMap might not have the extent you expect as it has been created before setting the map extent (check with composerMap.extent().asWktPolygon()). If this is the case you could probably fix it with running composerMap.zoomToExtent(bb)or even composerMap.setNewExtent(bb) after iface.mapCanvas().refresh(). – chkaiser Sep 19 '15 at 10:48
  • 1
    After experimenting with your code, I found that the aspect ratio seems to depend on the shape and size of the canvas panel in the QGIS GUI. Try making it as square as possible (by dragging splitters, adding in panels, removing or reordering toolbars etc). Not the code suggestion you were after, but might help. – Steven Kay Sep 19 '15 at 22:33
5
+50

The following code should work:

from qgis.core import *

# create an in-memory layer and add it to the QGIS project
layer =  QgsVectorLayer('Polygon?crs=EPSG:4326', 'poly' , "memory")
pr = layer.dataProvider() 
poly = QgsFeature()
poly.setGeometry(QgsGeometry.fromWkt("POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))"))
pr.addFeatures([poly])
layer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayers([layer])

# set map canvas extent
bb =  poly.geometry().boundingBox()
iface.mapCanvas().setExtent(bb)

mr = iface.mapCanvas().mapRenderer()
composition = QgsComposition(mr)
composition.setPaperSize(220, 220)
composition.setPlotStyle(QgsComposition.Print)
composerMap = QgsComposerMap(
    composition, 0, 0, 
    composition.paperWidth(), composition.paperHeight()
)
composerMap.zoomToExtent(bb)
composition.addItem(composerMap)
composition.exportAsPDF('test.pdf')

There are 2 main differences:

  1. In your code, the layer was not included in the map composer. One way for solving this is to create the layer before creating the map composer. Obviously you could manage this part somehow otherwise you would not have anything in your PDF.

  2. You need to set the extent of the composer map manually to fit the rectangle with composerMap.zoomToExtent(bb).

The resulting PDF is as follows:

enter image description here

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.