1

I am using QGIS 2.18.13 and inside my plugin, I call a templet which the should create a new map as a PDF. The code I use for that is as follows:

from PyQt4.QtXml import *
templet = "C:\\my_templet\\templet.qpt"
template_file = file(templet, 'rt')
template_content = template_file.read()
template_file.close()

maprender = iface.mapCanvas().mapRenderer()
composition = QgsComposition(maprender)

docu = QDomDocument()
docu.setContent(template_content)
composition.loadFromTemplate(docu)

map_report = composition.getComposerMapById(0)
map_report.zoomToExtent(maprender.extent())
#map_report.updateCachedImage() # added for testing did not change anyting

composition.refreshItems()
composition.exportAsPDF("C:\\my_folder\mypdf.pdf")

The map is created with the correct layout, but all layers, which have a rule-based styling are not shown. Neither are labels which have rule-based labeling. When changing the style to a categorized styling. The layers are shown correctly, which means the coordinatesystem is not to blame.

Reading the documentation of the QgsComposition class didn't yield any insights for me.

Question:

How to make the rule-based layers appear in my final PDF without changing them into categorized styling?

Additional Info:

When I use the same templet to create a map manually, all layers are shown correctly and as expected.

As requested: enter image description here

Or I simply style according to the scale. Both cases dont show in the map.

Update:

The error with rule-based styling seems to arise from the setting of the scale. When I dont set a scale, the layer style and labels will be displayed correctly. So I assume that I miss to the composer which zoom it has.

  • can you give an example of the rules you are using? – Ian Turton Nov 13 '17 at 13:42
  • I wonder if it requires slightly longer to process rule-based styles than categorised (and graduated). In which case, perhaps you can introduce a timer for a second or two before you initiate the exportAsPDF method? – Joseph Nov 13 '17 at 13:44
  • @IanTurton added an image to illustrate an example rule. – LaughU Nov 13 '17 at 13:51
  • 1
    @Joseph. This might worth a try. I will update if succeeded or not – LaughU Nov 13 '17 at 13:51
  • 1
    @Joseph I think I found the issue. It has to do with the scale value of the rule. A rule, which does not have a scale component will be displayed. I will update the question accordingly – LaughU Nov 13 '17 at 13:59
1

I figured out how to solve this and what caused the problem. For further reference, I will not only outline they solution but also my way, which led me there.

The solution:

I've had set my standard CRS in the option to a projected one (EPSG 25832) so every time QGIS was started it had the "right" projection from the beginning. The problem seems to be, that QGIS still assumes a geographic coordinate system (maybe WGS 84 which is the default) and thus calculates the map scale, which seems to come from the extent().height() and extent().width() as degrees rather than meters.This lead to extremely low scale values (many hundreds of millions to one). The solution is thus fairly simply: I've set the default CRS under settings -> options -> CRS to WGS 84 EPSG 4326 and after restarting everything went fine after changing back to the right CRS.

The way of finding my problem:

The first idea, it might have to do something with the scale came me, when @Ian Turton pointed me to the rule. When letting the scale factor out the layer seems to be displayed. So I checked the scale and the related values of my composition with

map_report = composition.getComposerMapById(0)
print "Scale: %s, height: %s, width: %s" %(map_report.scale(), maprender.extent().height(), maprender.extent().width())

This showed "correct" values for width and height but extremely large values for the scale (out of bounds for the scales defined in the rules). So I set the scale with report.setNewScale(int(25000)) which worked but somehow only showed one label and the scale bar denoted 0.02 meters. I then changed the OTF coordinate system once --> printed my map with a good result and changed the CRS back to the original one and printed again. This time the scale had reasonable values. This led me to the conclusion that the problem arises from the CRS.

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.