[Qgis-developer] help for processing algorithms

Victor Olaya volayaf at gmail.com
Tue Jul 21 06:33:55 PDT 2015


Iacopo

What do you mean by "it fails"? do you see any error? or it just
doesn't add any layer to your QGIS project?

You have to use Processing.runandload if you want that. Otherwise, the
runalg method returns a dictionary in which keys are output names, and
values are ouput paths where your outputs have been generated, and you
have to load them manually into QGIS

Hope this helps




2015-07-21 14:57 GMT+02:00 Iacopo Borsi <iacopo.borsi at tea-group.com>:
> Hi,
>
> I’m new to this list, so I don’t know if this is the right place to post my
> issue…. Sorry for that.
>
>
>
> However, my problem is about running processing algorithm directly from a
> python script.
>
> I’m using QGis 2.8 under Windows 7, and python correctly installed under
> Qgis directory.
>
>
>
> This is the problem: if I run a GRASS algorithm using the GUI, everything is
> ok.
>
> The same algorithm, with the same source input file and applying the same
> parameters… it fails when called by processing through a (very simple!)
> Python script.
>
>
>
> Now, after several tests, I’m also using the point shape file embedded under
> processing/test folder (just to be sure of integrity of input file).
>
> If you see below, I’m also looking at processing.log, and what is surprising
> is that logs seem exactly the same !!!
>
>
>
> I post below the short code I’m using and the log file I got.
>
> Again, I remark that the algorithm run from GUI is perfectly working :-0
>
>
>
> Any help or suggestion?
>
>
>
> Thank you so much in advance.
>
> Iacopo
>
>
>
> My python code to run Inverse Distance Squared Weighting interpolator
> (GRASS).
>
>
>
> ---
>
> from PyQt4.QtGui import *
>
> from qgis.core import *
>
> from qgis.gui import *
>
> from PyQt4.QtCore import *
>
> import processing
>
>
>
> # Define the data source
>
> vl = iface.activeLayer()
>
> if vl.isValid() :
>
>     print 'Layer is valid!'
>
>
>
> # Extent
>
> ext = vl.extent()
>
> xmin = ext.xMinimum()
>
> ymin = ext.yMinimum()
>
> xmax = ext.xMaximum()
>
> ymax = ext.yMaximum()
>
>
>
> # Name of the field
>
> myfield = 'PT_NUM_A'
>
>
>
> # Run Grass alg
>
> out_ras = processing.runalg("grass:v.surf.idw", vl , 12 ,2.0, myfield ,
> False,   "%f , %f, %f, %f "% (xmin , xmax , ymin , ymax), 10.0, -1, 0.001,
> None)
>
>
>
> # retrieve the raster output and load it in Qgis:
>
> fileoutput= out_ras['output']
>
> fileInfo = QFileInfo(fileoutput)
>
> baseName = fileInfo.baseName()
>
> rlayer = QgsRasterLayer(fileoutput, baseName)
>
> if rlayer.isValid():
>
>     QgsMapLayerRegistry.instance().addMapLayer( rlayer )
>
> else:
>
>      print 'Raster layer is not valid!'
>
>
>
> ****
>
> Log info I got from processing.log
>
> ---
>
> Using GUI:
>
> ---
>
> ALGORITHM|Tue Jul 21 2015
> 14:39:01|processing.runalg("grass:v.surf.idw","C:\\Users\\iacopo.borsi\\.qgis2\\python\\plugins\\processing\\tests\\data\\points.shp",12,2,"PT_NUM_A",False,"270778.60198,270855.745301,4458921.97814,4458983.8488",10,-1,0.0001,None)
>
> INFO|Tue Jul 21 2015 14:39:01|GRASS execution commands|g.proj -c
> proj4="+proj=utm +zone=30 +ellps=intl +towgs84=-87,-98,-121,0,0,0,0 +units=m
> +no_defs"|v.in.ogr min_area=0.0001 snap=-1
> dsn="C:\Users\iacopo.borsi\.qgis2\python\plugins\processing\tests\data"
> layer=points output=tmp14374823414715 --overwrite -o|g.region n=4458983.8488
> s=4458921.97814 e=270855.745301 w=270778.60198 res=10|v.surf.idw
> input=tmp14374823414715 npoints="12" power="2" column="PT_NUM_A"
> output=outputbcda77408a344d7a98a6046195dfd47f --overwrite|g.region
> rast=outputbcda77408a344d7a98a6046195dfd47f|r.out.gdal -c
> createopt="TFW=YES,COMPRESS=LZW"
> input=outputbcda77408a344d7a98a6046195dfd47f
> output="C:\Users\IACOPO~1.BOR\AppData\Local\Temp\processing\03d92082eed7415b98b492b4eed62250\output.tif"
>
>
>
> ---
>
> Using my script, launched by Python Console within QGis:
>
> ---
>
> ALGORITHM|Tue Jul 21 2015
> 14:39:44|processing.runalg("grass:v.surf.idw","C:\\Users\\iacopo.borsi\\.qgis2\\python\\plugins\\processing\\tests\\data\\points.shp",12,2,"PT_NUM_A",False,"270778.601980
> , 270855.745301, 4458921.978139, 4458983.848803 ",10,-1,0.0001,None)
>
> INFO|Tue Jul 21 2015 14:39:44|GRASS execution commands|g.proj -c
> proj4="+proj=utm +zone=30 +ellps=intl +towgs84=-87,-98,-121,0,0,0,0 +units=m
> +no_defs"|v.in.ogr min_area=0.001 snap=-1
> dsn="C:\Users\iacopo.borsi\.qgis2\python\plugins\processing\tests\data"
> layer=points output=tmp14374823847716 --overwrite -o|g.region n=
> 4458983.848803  s= 4458921.978139 e= 270855.745301 w=270778.601980
> res=10|v.surf.idw input=tmp14374823847716 npoints="12" power="2"
> column="PT_NUM_A" output=output86e01c9844a44af89394d73b4f384b5e
> --overwrite|g.region rast=output86e01c9844a44af89394d73b4f384b5e|r.out.gdal
> -c createopt="TFW=YES,COMPRESS=LZW"
> input=output86e01c9844a44af89394d73b4f384b5e
> output="C:\Users\IACOPO~1.BOR\AppData\Local\Temp\processing\96562ff5412640439c7dd19dffe60ac0\output.tif"
>
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the Qgis-developer mailing list