[Qgis-user] Maps generation with Python (or Atlas ?)
Benoît Laurent
blaurent at heurisis.eu
Mon May 18 10:26:34 PDT 2015
Thank you for your help.
> Hi,
>
> 2015-05-18 17:51 GMT+02:00 Benoît Laurent <blaurent at heurisis.eu
> <mailto:blaurent at heurisis.eu>>:
>
> Hello,
>
> I would like to generate a series of maps representing tracks with
> stops along them.
>
> For each track, I have got a group of two layers, one for the
> linear, one for the points that have associated information
> (basically name and time of the stops). First, is there a better
> way to achieve this ? For the moment, I have to handle two types
> of layer. It is the beginning of my problems.
>
> As the tracks overlap, I would like to generate a map for each
> track where only the current selected track appears. The others
> would be hidden.
>
> I had a try with Atlas generation. It perfectly works except that
> I can't hide the other tracks.
>
> You can style your coverage layer so that only atlas current feature
> will appear. In categorized style, use an expression like
> $id=$atlasfeatureid. You'll get 0 (false) and 1 (true) when classing.
> Set the 0 features style so that they become invisible or transparent.
It is a little better : with your help, I can highlight the current
linear layer. Actually, I aggregated all my linear layers to constitute
the coverage layer. However, I can't hide the points layers.
>
> I also had a try with a Python script. I managed to iterate over
> the groups of layers but I can't refresh the map canvas during the
> Python script. So, it doesn't work.
>
> A snippet of your code may help but I remember I had to face such a
> problem and it was because after *myatlas.beginRender()*, I forgot to
> set active the export of the atlas, sthg like
> *mycomposition.setAtlasMode(QgsComposition.ExportAtlas)*
>
Actually, I did not use the Atlas generator (maybe a mistake ?). I
wanted to reproduce/modify its behavior. Here is a piece of code
(remember that for each track, there is linear layer and a points layer) :
| # Iterate over groups of pair of layers
for gin range(0,nbGroups-1):
# Display layers of the current group
layer= layers[2*g]
rectTrajet= layer.extent()
legend.setLayerVisible(layer, True)
layer= layers[2*g+1]
rectPtsArr= layer.extent()
legend.setLayerVisible(layer, True)
# extent area
rectGlb= QgsRectangle
xMin= min(rectTrajet.xMinimum(), rectPtsArr.xMinimum())
xMax= max(rectTrajet.xMaximum(), rectPtsArr.xMaximum())
yMin= min(rectTrajet.yMinimum(), rectPtsArr.yMinimum())
yMax= max(rectTrajet.yMaximum(), rectPtsArr.yMaximum())
rectGlb= QgsRectangle(xMin, yMin, xMax, yMax)
self.iface.mapCanvas().setExtent(rectGlb)
self.iface.mapCanvas().zoomToSelected()
self.iface.mapCanvas().updateScale()
self.iface.mapCanvas().zoomByFactor(1.5)
# Nothing works here !!
self.iface.mapCanvas().updateOverview()
self.iface.mapCanvas().refresh()
layers[2*g].triggerRepaint()
layers[2*g+1].triggerRepaint()
# Map composer
composerMap= QgsComposerMap(c,x,y,w,h)
composerMap.setPreviewMode(QgsComposerMap.Render)
composerMap.updateBoundingRect()
composerMap.renderModeUpdateCachedImage()
composerMap.setFrameEnabled(True)
c.addItem(composerMap)
# Launch printing
printer= QtGui.QPrinter()
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
printer.setOutputFileName(nomFichier)
printer.setPaperSize(QSizeF(c.paperWidth(), c.paperHeight()), QtGui.QPrinter.Millimeter)
printer.setFullPage(True)
printer.setColorMode(QtGui.QPrinter.Color)
printer.setResolution(c.printResolution())
pdfPainter= QPainter(printer)
paperRectMM= printer.pageRect(QtGui.QPrinter.Millimeter)
paperRectPixel= printer.pageRect(QtGui.QPrinter.DevicePixel)
c.render(pdfPainter, paperRectPixel, paperRectMM)
pdfPainter.end()
# Remove map composer
c.removeItem(composerMap)
# Hide layers of the current group
legend.setLayerVisible(layers[2*g], False)
legend.setLayerVisible(layers[2*g+1], False)
|
---
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel antivirus Avast.
http://www.avast.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20150518/76d64ea4/attachment.html>
More information about the Qgis-user
mailing list