<div dir="ltr">Thanks Victor... I tried a few other ways to load the layer, but none of them seem to work.<div><br></div><div style>Another approach would be to somehow render the layer without loading it to the canvas first. Is it possible to add a layer to a QgsMapRenderer() layer set without it being loaded in the project's QgsMapLayerRegistry? I guess not since it requires a layer id, which I guess is provided by the registry. Is it possible to have more than one QgsMapLayerRegistry? If it is, I could create an output registry for each map... if not, looks like I'll need to make a full plugin.</div>
<div style><br></div><div style>Thanks,</div><div style>Rudi</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Feb 19, 2013 at 1:13 PM, Victor Olaya <span dir="ltr"><<a href="mailto:volayaf@gmail.com" target="_blank">volayaf@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Interacting with the canvas or any other elements in QGIS is not a<br>
good idea from SEXTANTE. It might cause that freezing you mention,<br>
since you are calling GUI methods from a different thread and that<br>
might result in strange behaviour.<br>
<br>
Ideally, a SEXTANTE algorithm should just take data and produce new<br>
data, and then, if it is declared correctly as algorithm output, it<br>
will be loaded automatically by SEXTANTE. I am afraid that your<br>
algorithm might go beyond what SEXTANTE expects....<br>
<br>
Anyway, the problem might be that you are trying to remove the layer<br>
and maybe it is not there since the load method in SEXTANTE does not<br>
add the layer to the canvas, but just open it so SEXTANTE algorithms<br>
can use it if it is not loaded.<br>
<br>
Hope this helps<br>
<br>
2013/2/19 Rudi von Staden <<a href="mailto:rudivs@gmail.com">rudivs@gmail.com</a>>:<br>
<div><div class="h5">> I've put together a Sextante script to automate the production of grid-based<br>
> species distribution maps (png image output) by modifying [1] and [2]. It<br>
> takes a vector layer of point localities (with species name as a field) and<br>
> a grid layer as input, and produces output maps by selecting grids based on<br>
> point localities.<br>
><br>
> For each species it creates a distribution shapefile, which consists of the<br>
> grid polygons where the species occurs. As far as I could tell, this layer<br>
> needs to be loaded to the canvas before it can be included in the layerset<br>
> to be rendered for the output images (is there a better way?). Sextante<br>
> includes a function for loading layers, but not removing them. From what I<br>
> could find,<br>
> "QgsMapLayerRegistry.instance().removeMapLayers(<a href="http://taxon_layer.id" target="_blank">taxon_layer.id</a>())" should<br>
> remove the layer, but it crashes QGIS when I try (current git version). If I<br>
> comment out the line, my canvas gets clogged. Any idea where the problem<br>
> lies? Script below.<br>
><br>
> Thanks,<br>
> Rudi<br>
><br>
> [1] <a href="http://www.qgis.org/pyqgis-cookbook/composer.html#simple-rendering" target="_blank">http://www.qgis.org/pyqgis-cookbook/composer.html#simple-rendering</a><br>
> [2] <a href="http://qgissextante.blogspot.com/2013/01/using-selection-algorithms.html" target="_blank">http://qgissextante.blogspot.com/2013/01/using-selection-algorithms.html</a><br>
><br>
><br>
> #Definition of inputs and outputs<br>
> #==================================<br>
> ##[Scratch]=group<br>
> ##all_localities=vector<br>
> ##taxon_field=field all_localities<br>
> ##africa_map=vector<br>
> ##sa_map=vector<br>
> ##grid_layer=vector<br>
> ##distribution_style_file=file<br>
> ##output=output file<br>
><br>
> #Algorithm body<br>
> #==================================<br>
> from qgis.core import *<br>
> from PyQt4.QtCore import *<br>
> from PyQt4.QtGui import *<br>
> from sextante.core.QGisLayers import QGisLayers<br>
> from sextante.core.SextanteVectorWriter import SextanteVectorWriter<br>
> import tempfile<br>
> import os<br>
><br>
> def print_map(taxon,taxon_shp):<br>
>     #load taxon layer (necessary?)<br>
>     QGisLayers.load(taxon_shp,name = "taxon",style =<br>
> distribution_style_file)<br>
><br>
>     # create image (dimensions 325x299)<br>
>     img = QImage(QSize(325,299), QImage.Format_ARGB32_Premultiplied)<br>
><br>
>     # set image's background color<br>
>     color = QColor(192,192,255)   # blue sea<br>
>     img.fill(color.rgb())<br>
><br>
>     # create painter<br>
>     p = QPainter()<br>
>     p.begin(img)<br>
>     p.setRenderHint(QPainter.Antialiasing)<br>
><br>
>     render = QgsMapRenderer()<br>
><br>
>     # create layer set<br>
>     africa_layer = QGisLayers.getObjectFromUri(africa_map)<br>
>     sa_layer = QGisLayers.getObjectFromUri(sa_map)<br>
>     taxon_layer = QGisLayers.getObjectFromUri(taxon_shp)<br>
><br>
>     lst = []<br>
>     lst.append(<a href="http://taxon_layer.id" target="_blank">taxon_layer.id</a>())<br>
>     lst.append(<a href="http://sa_layer.id" target="_blank">sa_layer.id</a>())<br>
>     lst.append(<a href="http://africa_layer.id" target="_blank">africa_layer.id</a>())<br>
><br>
>     render.setLayerSet(lst)<br>
><br>
>     # set extent (xmin,ymin,xmax,ymax)<br>
>     rect = QgsRectangle(14.75,-36.00,34.00,-21.00)<br>
>     render.setExtent(rect)<br>
><br>
>     # set output size<br>
>     render.setOutputSize(img.size(), img.logicalDpiX())<br>
><br>
>     # do the rendering<br>
>     render.render(p)<br>
>     p.end()<br>
><br>
>     # save image<br>
>     outdir = os.path.dirname(os.path.abspath(output))<br>
>     img.save(os.path.join(outdir,taxon+".png"),"png")<br>
><br>
>     # remove taxon layer from project<br>
>     QgsMapLayerRegistry.instance().removeMapLayers(<a href="http://taxon_layer.id" target="_blank">taxon_layer.id</a>())<br>
> #crashes QGIS<br>
><br>
> tempdir = tempfile.gettempdir()<br>
> taxa = sextante.runalg('qgis:listuniquevalues', all_localities, taxon_field,<br>
> None)['UNIQUE_VALUES'].split(";")<br>
> counter = 0   # limit for testing<br>
> for taxon in taxa:<br>
>     if counter < 10:<br>
>         sextante.runalg('qgis:selectbyattribute', all_localities,<br>
> taxon_field, 0, taxon)<br>
>         sextante.runalg('qgis:selectbylocation', grid_layer, all_localities,<br>
> 0)<br>
>         filename = os.path.join(tempdir,"taxon.shp")    #memory file better?<br>
>         sextante.runalg('qgis:saveselectedfeatures', grid_layer, filename)<br>
>         print_map(taxon,filename)<br>
>         counter+=1<br>
><br>
</div></div>> _______________________________________________<br>
> Qgis-developer mailing list<br>
> <a href="mailto:Qgis-developer@lists.osgeo.org">Qgis-developer@lists.osgeo.org</a><br>
> <a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
><br>
</blockquote></div><br></div>