<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<br>
<br>
<div class="moz-cite-prefix">Le 20/05/2015 15:55, Bernd Vogelgesang
a écrit :<br>
</div>
<blockquote cite="mid:op.xyxsybmol2i25i@vogelbe-esprimo-p2560"
type="cite">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<style type="text/css">body { font-family:'DejaVu Sans Mono'; font-size:12px}</style>
<div>Hi Benoît,</div>
<div><br>
</div>
<div>would you mind sharing your hole script as reference for
others?</div>
</blockquote>
"As reference"... Hum... It would be very presumptuous of me :-).<br>
<br>
<blockquote cite="mid:op.xyxsybmol2i25i@vogelbe-esprimo-p2560"
type="cite">
<div>I think you touched a common problem which isn't covered with
built-in functions yet, and not everyone is gifted with profound
coding abilities in python (like me ;) )</div>
</blockquote>
"gifted with profound coding abilities in python". Same comment...
:-)<br>
<br>
However, I simplified the code a little and translated my comments
in English. By the way, the picture inclusion does not work. If
someone can help me on that point, it would be nice. If it helps,
here is my code:<br>
<br>
<br>
<font face="Arial"><b>#!/usr/bin/env Python<br>
# encoding: utf-8<br>
"""<br>
Generate pdf maps<br>
Assumptions :<br>
- the QGis project contains a set of groups of 2
layers, the first one just containting a linear feature, the
second containing points along the track.<br>
- the last group contains raster layers.<br>
<br>
"""<br>
from glob import glob<br>
from os import *<br>
from os.path import splitext<br>
<br>
from qgis import core<br>
<br>
from qgis.core import (QgsComposition,<br>
QgsComposerMap,<br>
QgsComposerLabel,<br>
QgsComposerScaleBar,<br>
QgsComposerPicture,<br>
QgsMapLayer,<br>
QgsRectangle,<br>
QgsComposerArrow,<br>
QGis)<br>
<br>
<br>
from PyQt4 import QtGui, QtCore<br>
from PyQt4.QtCore import *<br>
from PyQt4.QtGui import *<br>
<br>
import os<br>
import sys<br>
import re<br>
import time<br>
<br>
def run_script(iface):<br>
## Generator<br>
genCartes = GenCartes(iface)<br>
<br>
<br>
## Generates the maps<br>
genCartes.generer()<br>
<br>
<br>
class GenCartes:<br>
"""<br>
***************************************************************************************************************************************************<br>
Constructeur<br>
***************************************************************************************************************************************************<br>
"""<br>
def __init__(self, iface):<br>
### Initialization using qgis.utils.iface<br>
self.iface = iface<br>
<br>
# Attribute of the class<br>
self.composerMap = None<br>
<br>
"""Compute an extent of geometry, with given margin (in
%)<br>
to be able to show it in the selected map item<br>
Deal with non-square geometries to keep same ratio"""<br>
def getNewExtent(self, geom, margin = None):<br>
# compute coordinates and ratio<br>
new_extent = None<br>
x1, y1, x2, y2 = (0, 0, 0, 0)<br>
geom_rect = geom<br>
geom_ratio = geom_rect.width() /
geom_rect.height()<br>
xa1 = geom_rect.xMinimum()<br>
xa2 = geom_rect.xMaximum()<br>
ya1 = geom_rect.yMinimum()<br>
ya2 = geom_rect.yMaximum()<br>
<br>
map_rect = self.composerMap.boundingRect()<br>
map_ratio = map_rect.width() / map_rect.height()<br>
# geometry height is too big<br>
if geom_ratio < map_ratio:<br>
y1 = ya1<br>
y2 = ya2<br>
x1 = (xa1 + xa2 + map_ratio * (ya1 - ya2)) /
2.0<br>
x2 = x1 + map_ratio * (ya2 - ya1)<br>
new_extent = core.QgsRectangle(x1, y1, x2,
y2)<br>
# geometry width is too big<br>
elif geom_ratio > map_ratio:<br>
x1 = xa1<br>
x2 = xa2<br>
y1 = (ya1 + ya2 + (xa1 - xa2) / map_ratio) /
2.0<br>
y2 = y1 + (xa2 - xa1) / map_ratio<br>
new_extent = core.QgsRectangle(x1, y1, x2,
y2)<br>
# same ratio: send geom bounding box<br>
else:<br>
new_extent = geom_rect<br>
# add margin to computed extent<br>
if margin:<br>
new_extent.scale(1 + margin / 100.0)<br>
<br>
return new_extent<br>
<br>
"""<br>
***************************************************************************************************************************************************<br>
Generate the maps<br>
***************************************************************************************************************************************************<br>
"""<br>
def generer(self):<br>
## Makes the vector layer transparent except
raster layers<br>
## Hide labels<br>
layers = self.iface.legendInterface().layers()<br>
for layer in layers:<br>
if layer.type() <>
QgsMapLayer.RasterLayer:<br>
layer.rendererV2().symbol().setAlpha(0.3)<br>
layer.setCustomProperty("labeling/enabled", "False")<br>
<br>
<br>
## Creation of QgsComposition<br>
mapRenderer =
self.iface.mapCanvas().mapRenderer() <br>
c = QgsComposition(mapRenderer)<br>
<br>
## Format of the maps<br>
c.setPaperSize(420, 297)<br>
c.setPlotStyle(QgsComposition.Print)<br>
<br>
## Margins<br>
margeBas = 5<br>
margeHaut = 5<br>
margeGauche = 5<br>
margeDroite = 5<br>
margeHautCarte = 0<br>
margeBasCarte = 50<br>
margeSsCarte = 5<br>
<br>
## Composer map<br>
x, y = margeGauche, margeHaut<br>
w, h = c.paperWidth()-margeGauche-margeDroite,
c.paperHeight()-margeBasCarte-margeHaut<br>
self.composerMap = QgsComposerMap(c,x,y,w,h)<br>
self.composerMap.setPreviewMode(QgsComposerMap.Cache)<br>
c.addItem(self.composerMap)<br>
<br>
## Title<br>
clTitre = QgsComposerLabel(c)<br>
clTitre.setItemPosition(margeGauche,
c.paperHeight() - margeBasCarte + margeSsCarte)<br>
clTitre.setFont(QFont( "Helvetica", 24,
QFont.Bold ))<br>
c.addItem(clTitre)<br>
<br>
## Logo file<br>
## Does not appear !! I don't know why<br>
nomFichierLogo =
QtGui.QFileDialog.getOpenFileName(None, u"Choose a logo :",
'C:/', "Image Files (*.png *.jpg *.bmp)")<br>
if nomFichierLogo:<br>
itemLogo = QgsComposerPicture(c)<br>
itemLogo.setItemPosition(c.paperWidth()
- margeDroite - 30, c.paperHeight() + margeSsCarte + 20, 30, 30)<br>
itemLogo.setPicturePath(nomFichierLogo)<br>
itemLogo.updateItem()<br>
c.addItem(itemLogo)<br>
<br>
## Output folder<br>
nomRep =
QtGui.QFileDialog.getExistingDirectory(None, u"Choose an output
folder :", 'C:/', QtGui.QFileDialog.ShowDirsOnly)<br>
<br>
<br>
# Printer<br>
printer = QtGui.QPrinter()<br>
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)<br>
printer.setPaperSize(QSizeF(c.paperWidth(),
c.paperHeight()), QtGui.QPrinter.Millimeter)<br>
printer.setFullPage(True)<br>
printer.setColorMode(QtGui.QPrinter.Color)<br>
printer.setResolution(c.printResolution())<br>
<br>
<br>
## Iterate over groups<br>
legend = self.iface.legendInterface()<br>
groups = legend.groupLayerRelationship()<br>
layers = legend.layers()<br>
<br>
nbGroups = len(groups)<br>
nbLayers = len(layers)<br>
<br>
if nbLayers > 0 and len(nomRep) > 0 :<br>
<br>
# Last group containts rasters =>
nbGroups - 1<br>
g = 0<br>
#for g in range(0,nbGroups-1):<br>
for g in range(0,2):<br>
<br>
# File name<br>
nomCircuit = groups[g][0]<br>
nomCircuit =
re.sub("[A-Za-z._\-]", '', nomCircuit)<br>
print nomCircuit<br>
<br>
nomFichier = nomRep + "\\" +
nomCircuit + ".pdf"<br>
<br>
if not
os.path.isfile(nomFichier):<br>
<br>
## Update title<br>
clTitre.setText("Track "
+ nomCircuit)<br>
clTitre.adjustSizeToText()<br>
<br>
<br>
# Update transparency
and labels<br>
# NB : do not use
setLayerTransparency, problem with it I don't know why<br>
layers[2*g].rendererV2().symbol().setWidth(2)<br>
layers[2*g].rendererV2().symbol().setAlpha(1)<br>
layers[2*g+1].setCustomProperty("labeling/enabled", "True")<br>
layers[2*g+1].rendererV2().symbol().setAlpha(1)<br>
<br>
rectTrajet =
layers[2*g].extent() # linear<br>
rectPtsArr =
layers[2*g+1].extent() # points<br>
<br>
# extent definition<br>
rectGlb = QgsRectangle<br>
xMin =
min(rectTrajet.xMinimum(), rectPtsArr.xMinimum())<br>
xMax =
max(rectTrajet.xMaximum(), rectPtsArr.xMaximum())<br>
yMin =
min(rectTrajet.yMinimum(), rectPtsArr.yMinimum())<br>
yMax =
max(rectTrajet.yMaximum(), rectPtsArr.yMaximum())<br>
rectGlb =
QgsRectangle(xMin, yMin, xMax, yMax)<br>
<br>
self.composerMap.setNewExtent(self.getNewExtent(rectGlb, 10))<br>
self.composerMap.cache()<br>
<br>
<br>
# Filename<br>
printer.setOutputFileName(nomFichier)<br>
<br>
# Launch printer<br>
pdfPainter =
QPainter(printer)<br>
paperRectMM =
printer.pageRect(QtGui.QPrinter.Millimeter)<br>
paperRectPixel =
printer.pageRect(QtGui.QPrinter.DevicePixel)<br>
c.render(pdfPainter,
paperRectPixel, paperRectMM)<br>
pdfPainter.end()<br>
<br>
<br>
# Layers back to their
initial state<br>
layers[2*g].rendererV2().symbol().setWidth(1)<br>
layers[2*g].rendererV2().symbol().setAlpha(0.3)<br>
layers[2*g+1].rendererV2().symbol().setAlpha(0.3)<br>
layers[2*g+1].setCustomProperty("labeling/enabled", "False")<br>
<br>
</b></font><br>
<br>
<br /><br />
<hr style='border:none; color:#909090; background-color:#B0B0B0; height: 1px; width: 99%;' />
<table style='border-collapse:collapse;border:none;'>
<tr>
<td style='border:none;padding:0px 15px 0px 8px'>
<a href="http://www.avast.com/">
<img border=0 src="http://static.avast.com/emails/avast-mail-stamp.png" />
</a>
</td>
<td>
<p style='color:#3d4d5a; font-family:"Calibri","Verdana","Arial","Helvetica"; font-size:12pt;'>
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel antivirus Avast.
<br><a href="http://www.avast.com/">www.avast.com</a>
</p>
</td>
</tr>
</table>
<br />
</body>
</html>