Re: [Qgis-user] Maps generation with Python (or Atlas ?)

2015-05-20 Thread Benoît Laurent

*Sorry, I left a mistake in my code:

The comment must be removed for this line:
#for g in range(0,nbGroups-1):

This line must be removed of course:
for g in range(0,2):

Benoît
*


---
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel 
antivirus Avast.
http://www.avast.com
___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-user

Re: [Qgis-user] Maps generation with Python (or Atlas ?)

2015-05-20 Thread Benoît Laurent



Le 20/05/2015 15:55, Bernd Vogelgesang a écrit :

Hi Benoît,

would you mind sharing your hole script as reference for others?

"As reference"... Hum... It would be very presumptuous of me :-).

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 ;) )

"gifted with profound coding abilities in python". Same comment... :-)

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:



*#!/usr/bin/env Python
# encoding: utf-8
"""
Generate pdf maps
Assumptions :
- 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.

- the last group contains raster layers.

"""
from glob import glob
from os import *
from os.path import splitext

from qgis import core

from qgis.core import (QgsComposition,
   QgsComposerMap,
   QgsComposerLabel,
   QgsComposerScaleBar,
   QgsComposerPicture,
   QgsMapLayer,
   QgsRectangle,
QgsComposerArrow,
   QGis)


from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *

import os
import sys
import re
import time

def run_script(iface):
## Generator
genCartes = GenCartes(iface)


## Generates the maps
genCartes.generer()


class GenCartes:
"""
***
Constructeur
***
"""
def __init__(self, iface):
### Initialization using qgis.utils.iface
self.iface = iface

# Attribute of the class
self.composerMap = None

"""Compute an extent of geometry, with given margin (in %)
to be able to show it in the selected map item
Deal with non-square geometries to keep same ratio"""
def getNewExtent(self, geom, margin = None):
# compute coordinates and ratio
new_extent = None
x1, y1, x2, y2 = (0, 0, 0, 0)
geom_rect = geom
geom_ratio = geom_rect.width() / geom_rect.height()
xa1 = geom_rect.xMinimum()
xa2 = geom_rect.xMaximum()
ya1 = geom_rect.yMinimum()
ya2 = geom_rect.yMaximum()

map_rect = self.composerMap.boundingRect()
map_ratio = map_rect.width() / map_rect.height()
# geometry height is too big
if geom_ratio < map_ratio:
y1 = ya1
y2 = ya2
x1 = (xa1 + xa2 + map_ratio * (ya1 - ya2)) / 2.0
x2 = x1 + map_ratio * (ya2 - ya1)
new_extent = core.QgsRectangle(x1, y1, x2, y2)
# geometry width is too big
elif geom_ratio > map_ratio:
x1 = xa1
x2 = xa2
y1 = (ya1 + ya2 + (xa1 - xa2) / map_ratio) / 2.0
y2 = y1 + (xa2 - xa1) / map_ratio
new_extent = core.QgsRectangle(x1, y1, x2, y2)
# same ratio: send geom bounding box
else:
new_extent = geom_rect
# add margin to computed extent
if margin:
new_extent.scale(1 + margin / 100.0)

return new_extent

"""
***
Generate the maps
***
"""
def generer(self):
## Makes the vector layer transparent except raster layers
## Hide labels
layers = self.iface.legendInterface().layers()
for layer in layers:
if layer.type() <> QgsMapLayer.RasterLayer:
layer.rendererV2().symbol().setAlpha(0.3)
layer.setCustomProperty("labeling/enabled", "False")


## Creation of QgsComposition
mapRenderer = self.iface.mapCanvas().mapRenderer()
c = QgsComposition(mapRenderer)

## Format of the maps
c.setPaperSize(420, 297)

Re: [Qgis-user] Maps generation with Python (or Atlas ?)

2015-05-20 Thread Bernd Vogelgesang

Hi Benoît,

would you mind sharing your hole script as reference for others?
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 ;) )


Cheers
Bernd


Am 20.05.2015, 15:16 Uhr, schrieb Benoît Laurent :

I eventually managed to solve my problem. In case somebody encounters  
the same or a similar one, I provide my solution: I used a >Python  
script iterating over the groups. Apparently, it is not possible (or I  
failed) to refresh the mapCanvas during the Python >script.
Instead of using  "legend.setLayerVisible(layer, True)", I played with  
symbols:


layers[2*g].rendererV2().symbol().setWidth(2)
layers[2*g].rendererV2().symbol().setAlpha(1)

There were others problems in my code but the main difficulty lied  
there. Symbols can be updated during Python scripts and it >perfectly  
meets my needs.


Regards,

Benoît



L'absence de virus dans ce courrier électronique a été vérifiée par le  
logiciel antivirus Avast.

www.avast.com








--
Erstellt mit Operas E-Mail-Modul: http://www.opera.com/mail/___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-user

Re: [Qgis-user] Maps generation with Python (or Atlas ?)

2015-05-20 Thread Benoît Laurent
I eventually managed to solve my problem. In case somebody encounters 
the same or a similar one, I provide my solution: I used a Python script 
iterating over the groups. Apparently, it is not possible (or I failed) 
to refresh the mapCanvas during the Python script.


Instead of using |"legend.setLayerVisible(layer,True)", I played with 
symbols:


layers[2*g].rendererV2().symbol().setWidth(2)
layers[2*g].rendererV2().symbol().setAlpha(1)

There were others problems in my code but the main difficulty lied 
there. Symbols can be updated during Python scripts and it perfectly 
meets my needs.


Regards,

Benoît
|


---
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel 
antivirus Avast.
http://www.avast.com
___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-user

Re: [Qgis-user] Maps generation with Python (or Atlas ?)

2015-05-18 Thread Benoît Laurent


Thank you for your help.


Hi,

2015-05-18 17:51 GMT+02:00 Benoît Laurent >:


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
___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-user

Re: [Qgis-user] Maps generation with Python (or Atlas ?)

2015-05-18 Thread DelazJ
Hi,

2015-05-18 17:51 GMT+02:00 Benoît Laurent :

> 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.


> 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)*


> Do others faced a similar problem ? Any help would be appreciated.
>
> Regards,
>
> Benoît
>
>
>
> ---
> L'absence de virus dans ce courrier électronique a été vérifiée par le
> logiciel antivirus Avast.
> http://www.avast.com
>
> ___
> Qgis-user mailing list
> Qgis-user@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-user
___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-user

[Qgis-user] Maps generation with Python (or Atlas ?)

2015-05-18 Thread Benoît Laurent

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.


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.


Do others faced a similar problem ? Any help would be appreciated.

Regards,

Benoît



---
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel 
antivirus Avast.
http://www.avast.com

___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-user