3

I'm writing a plugin for QGIS 2.2.0 which should render a pdf file with a map and some text.

The code is working under Linux (current Arch) and Windows XP, but it's not working on Windows 7 or 8. Output PDF on Win 7/8 has all labels, but no map, only blank space.

I don't know if it's relevant, but I want to render only raster layers.

The plugin is to be runned from QGIS, so it has access to self.iface variable, but I can also use standalone QgsMapRenderer.

What am I missing? Please help me! ;)

Here is the code I use:

mapRenderer = self.iface.mapCanvas().mapRenderer()
mapRenderer.setLayerSet(input_map_layers) #list of map layers id's
mapRenderer.setOutputUnits(QgsMapRenderer.Millimeters)
mapRenderer.setOutputSize(QSize(map_width_on_print_mm, 
map_height_on_print_mm), 300)
mapRenderer.setExtent(rect)

c = QgsComposition(mapRenderer)
c.setPlotStyle(QgsComposition.Print)
c.setPaperSize(210,297)

composerMap = QgsComposerMap(c, 14.7026,113.85,map_width_on_print_mm,map_height_on_print_mm)
composerMap.setFrameEnabled(True)
composerMap.setFrameOutlineWidth(0.5)
c.addItem(composerMap)

printer = QPrinter()
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(unicode(input_data_fields['output_pdf_filename']))
printer.setPaperSize(QSizeF(c.paperWidth(), c.paperHeight()), 
QPrinter.Millimeter)
printer.setFullPage(True)
printer.setColorMode(QPrinter.Color)
printer.setResolution(c.printResolution())

pdfPainter = QPainter(printer)
paperRectMM = printer.pageRect(QPrinter.Millimeter)
paperRectPixel = printer.pageRect(QPrinter.DevicePixel)
c.render(pdfPainter, paperRectPixel, paperRectMM)
pdfPainter.end()

2 Answers 2

2

Ok, problem solved.

Your hint with the extent setting put me on a right track.

It turns out that I had a total mess with coordinate reference systems beetween layers and projects. QGIS was reprojecting everything on the fly, but I had to double check everything to get it right in the end.

So after all there is no problem with rendering on Win 7/8, it was just my problems with coordinates.

To solve the issue I had to check every time which crs I have in one layer and convert the extent to destination crs (used in map canvas).

This code does the trick:

map_centroid_point_layer_crs = desired_layer.crs()
destination_crs = mapRenderer.destinationCrs()
xform = QgsCoordinateTransform(map_centroid_point_layer_crs, destination_crs)
map_centroid_point_in_dest_crs = xform.transform(map_centroid_point)

# here is the code for calculating the extent
map_width_on_print_mm = 182
map_height_on_print_mm = 160

scale = 2000
map_x_min = map_x - (((map_width_on_print_mm * scale) / 1000.0) / 2.0)
map_x_max = map_x + (((map_width_on_print_mm * scale) / 1000.0) / 2.0)
map_y_min = map_y - (((map_height_on_print_mm * scale) / 1000.0) / 2.0)
map_y_max = map_y + (((map_height_on_print_mm * scale) / 1000.0) / 2.0)

rect = QgsRectangle(map_x_min, map_y_min, map_x_max, map_y_max)

Thanks again for your help!

1

Try explicitly setting composerMap's extent. ie,

composerMap.setNewExtent(rect)
3
  • Thanks, I've added your line to the code. At the beggining nothing has changed, but I've opened a few different projects to test and for some (2) of them rendering the map worked, but for most of tested (5) not. I've also commented the line you've provided and nothing changes (still those 2 works, but majority not). On Linux still everything works. I guess maybe it's something with image caching? PS. Should I repost answers from QGIS maillist also here?
    – user30013
    May 9, 2014 at 8:43
  • On those project on which the map is not rendered it looks like the rendering is not giving a blank image, but not giving any at all, but it's only guessing.
    – user30013
    May 9, 2014 at 8:45
  • (moving this comment to a new answer)
    – user30013
    May 10, 2014 at 18:56

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.