<div dir="ltr">
















<p class="MsoNormal">Thanks Andreas!</p>

<p class="MsoNormal">Actually I need to do it with a script, because the serial
printing is a part of a much more complex script.</p>

<p class="MsoNormal">It works on QGIS 2.2, but on QGIS 2.4 it drives me crazy…</p>

<p class="MsoNormal">Should I go back to the previous version and forget about the new releases?</p><p class="MsoNormal">Regards,</p><p class="MsoNormal">Andrea</p>

</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 1, 2014 at 12:43 PM, Andreas Neumann <span dir="ltr"><<a href="mailto:a.neumann@carto.net" target="_blank">a.neumann@carto.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div text="#000000" bgcolor="#FFFFFF">
    Hi Andrea,<br>
    <br>
    Are you aware that QGIS already supports serial printing out of the
    box without scripting? I would first test if that works fine for you
    before bothering with scripts. QGIS 2.2 had serial printing in core,
    2.4 has it improved and 2.6 has a lot more improvements. I am pretty
    sure that many (most?) cases can now be handled out of the box.<br>
    <br>
    Andreas<div><div class="h5"><br>
    <br>
    <div>On 01.10.2014 10:54, Andrea Amparore
      wrote:<br>
    </div>
    </div></div><blockquote type="cite"><div><div class="h5">
      <div dir="ltr">
        <div>Dear QGIS users community,</div>
        <div><br>
        </div>
        <div>I'm a new member and I am stuck on a problem since few
          days, maybe someone can help me with this.</div>
        <div><br>
        </div>
        <div>I have developed a stand-alone python script for making
          serial maps using PyQGIS library.</div>
        <div><br>
        </div>
        <div>The maps should follow a specific format that is indicated
          by a template .qpt file.</div>
        <div><br>
        </div>
        <div>This template file is opened and edited with lxml library
          in order to add information about the map (title, date,
          source, etc).</div>
        <div><br>
        </div>
        <div>The modified .qpt is then loaded into a new composition,
          the layers are added and the map exported.</div>
        <div><br>
        </div>
        <div>Everything was working perfectly since I’ve been using QGIS
          2.2. Few days ago I have updated QGIS to the latest version
          2.4 and now the output is sadly empty: map elements are
          displayed (frame, legend, title..), but no layer appears on
          the map.</div>
        <div><br>
        </div>
        <div>The script is correct, because it still run correctly on
          another machine having QGIS 2.2. Is there anything that I have
          to modify for running it with QGIS 2.4?</div>
        <div><br>
        </div>
        <div>This is a very basic version of the script.</div>
        <div><br>
        </div>
        <div>import os</div>
        <div>from qgis.core import *</div>
        <div>from qgis.gui import *</div>
        <div>from PyQt4.QtCore import *</div>
        <div>from PyQt4.QtGui import *</div>
        <div>from PyQt4.QtXml import *</div>
        <div>import lxml.etree as etree</div>
        <div><br>
        </div>
        <div>print "setting prefix"</div>
        <div>QgsApplication.setPrefixPath("/usr", True)</div>
        <div>print "initiating qgis"</div>
        <div>QgsApplication.initQgis()</div>
        <div>print 'creating new app'</div>
        <div>app = QgsApplication([], True)</div>
        <div>#removing old layers</div>
        <div>QgsMapLayerRegistry.instance().removeAllMapLayers()</div>
        <div><br>
        </div>
        <div>script_folder = os.path.dirname(__file__)</div>
        <div>project_folder = os.path.dirname(script_folder)</div>
        <div>output_folder = os.path.join(project_folder, 'map_outputs')</div>
        <div>xml_folder = os.path.join(project_folder,
          'project_outputs')</div>
        <div>shapefile_folder = os.path.join(project_folder,
          'shapefile_folder')</div>
        <div><br>
        </div>
        <div>template_composer = os.path.join(project_folder,
          'basic_composer_template_QGIS24.qpt')</div>
        <div>polyg_shapefile = os.path.join(shapefile_folder,
          'polygon.shp') # crs EPSG:4326 - WGS 84</div>
        <div>point_shapefile = os.path.join(shapefile_folder,
          'point.shp') # crs EPSG:32615 - WGS 84 / UTM zone 15N</div>
        <div><br>
        </div>
        <div>mapname = "Test Map"</div>
        <div>srid = 4326</div>
        <div>provider_name = 'ogr'</div>
        <div>layerset = []</div>
        <div><br>
        </div>
        <div>#add layer 1</div>
        <div>vlayer_name= 'polygon layer'</div>
        <div>vdata_source = polyg_shapefile</div>
        <div>print "Loading EQ buffers"</div>
        <div>layer = QgsVectorLayer(vdata_source, vlayer_name,
          provider_name)</div>
        <div>print "Buffers loaded"</div>
        <div>QgsMapLayerRegistry.instance().addMapLayer(layer)</div>
        <div>layerset.append(<a href="http://layer.id" target="_blank">layer.id</a>())</div>
        <div><br>
        </div>
        <div>#add layer 2</div>
        <div>point_layer_name= 'point layer'</div>
        <div>point_data_source = point_shapefile</div>
        <div>point_layer = QgsVectorLayer(point_data_source,
          point_layer_name, provider_name)</div>
        <div>QgsMapLayerRegistry.instance().addMapLayer(point_layer)</div>
        <div>layerset.append(<a href="http://point_layer.id" target="_blank">point_layer.id</a>())</div>
        <div><br>
        </div>
        <div># Set up the map renderer that will be assigned to the
          composition</div>
        <div>map_renderer = QgsMapRenderer()</div>
        <div>#preparing the map the extent - 3 times wider than the
          polygon layer's extent</div>
        <div>rect = layer.extent()</div>
        <div>rect.scale(3)</div>
        <div># Set the labelling engine for the canvas</div>
        <div>labelling_engine = QgsPalLabeling()</div>
        <div>map_renderer.setLabelingEngine(labelling_engine)</div>
        <div># Enable on the fly CRS transformations</div>
        <div>map_renderer.setProjectionsEnabled(True)</div>
        <div># Now set up the composition</div>
        <div>composition = QgsComposition(map_renderer)</div>
        <div>#set WGS84 as destination crs</div>
        <div>map_projection = QgsCoordinateReferenceSystem(srid,
          QgsCoordinateReferenceSystem.PostgisCrsId)</div>
        <div>map_projection_descr = map_projection.description()</div>
        <div>map_renderer.setDestinationCrs(map_projection)</div>
        <div><br>
        </div>
        <div>#open the composer template and edit it</div>
        <div>with open(template_composer, 'r') as f:</div>
        <div>    tree  = etree.parse(f)</div>
        <div>    #setting extent</div>
        <div>    for elem in tree.iter(tag = 'Extent'):</div>
        <div>        elem.attrib['xmax'] = str(rect.xMaximum())</div>
        <div>        elem.attrib['xmin'] = str(rect.xMinimum())</div>
        <div>        elem.attrib['ymax'] = str(rect.yMaximum())</div>
        <div>        elem.attrib['ymin'] = str(rect.yMinimum())</div>
        <div>    #editing the title</div>
        <div>    for elem in tree.iter(tag = 'ComposerLabel'):</div>
        <div>            for child in elem:</div>
        <div>                if child.tag == 'ComposerItem':</div>
        <div>                    if child.attrib['id'] ==
          "__maintitle__":</div>
        <div>                        elem.attrib['labelText'] = mapname</div>
        <div>    #save the edited composer as a new file</div>
        <div>    new_composer = os.path.join(xml_folder, mapname +
          "_composer.qpt")</div>
        <div>    tree.write(new_composer)</div>
        <div><br>
        </div>
        <div>#open the newly created composer</div>
        <div>new_composerfile = file(new_composer, 'rt')</div>
        <div>new_composer_content = new_composerfile.read()</div>
        <div>new_composerfile.close()</div>
        <div>document = QDomDocument()</div>
        <div>document.setContent(new_composer_content)</div>
        <div>result = composition.loadFromTemplate(document)</div>
        <div><br>
        </div>
        <div># Get the main map canvas on the composition and set the
          layers</div>
        <div>composerMap = composition.getComposerMapById(0)</div>
        <div>composerMap.renderModeUpdateCachedImage()</div>
        <div>map_renderer.setLayerSet(layerset)</div>
        <div><br>
        </div>
        <div>#legend</div>
        <div>legend = QgsComposerLegend(composition)</div>
        <div>legend.model().setLayerSet(map_renderer.layerSet())</div>
        <div>legend.model().setLayerSet</div>
        <div>composition.addItem(legend)</div>
        <div>legend.setItemPosition (25,122)</div>
        <div>legend.setFrameEnabled(True)</div>
        <div>legend.setScale(.7)</div>
        <div><br>
        </div>
        <div>#save image</div>
        <div>print 'saving image'</div>
        <div>image = composition.printPageAsRaster(0)</div>
        <div>image.save(os.path.join(output_folder,mapname) + ".png")</div>
        <div><br>
        </div>
        <div>In this link you can find the 2 shapefiles and the composer
          template that you need for running the script, and the example
          of failed export: <a href="http://we.tl/8y3SrCxlGM" style="font-size:12.8000001907349px;color:rgb(18,148,220);font-family:Helvetica,sans-serif;line-height:20px;text-decoration:none;background-color:rgb(240,244,247)" target="_blank">http://we.tl/8y3SrCxlGM</a></div>
        <div><br>
        </div>
        <div>Thanks in advance.</div>
        <div><br>
        </div>
        <div>Regards,</div>
        <div><br>
        </div>
        <div>Andrea</div>
      </div>
      <br>
      <fieldset></fieldset>
      <br>
      </div></div><pre>_______________________________________________
Qgis-user mailing list
<a href="mailto:Qgis-user@lists.osgeo.org" target="_blank">Qgis-user@lists.osgeo.org</a>
<a href="http://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-user</a></pre>
    </blockquote>
    <br>
  </div>

<br>_______________________________________________<br>
Qgis-user mailing list<br>
<a href="mailto:Qgis-user@lists.osgeo.org">Qgis-user@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-user</a><br></blockquote></div><br></div>