4

I'm trying to write a Python script performing a geoalgorithm. What is surprising me is the following:

  1. I first test the algorithm by QGIS (2.8) interface. In my case, the GRASS interpolator v.surf.idw
  2. I see that the solution is sufficiently good using a certain setting of parameters.
  3. Then, I run the same algorithm with the same parameters, froma Python script. In my case:

    out_ras = processing.runalg("grass:v.surf.idw", vl,12,2,"field_3" ,False, "%f , %f, %f, %f "% (xmin , xmax , ymin , ymax), 0.5, -1, 0.001, fileoutput)
    

where:

  • vl is the point vector layer
  • field_3 is the filed where vlaues to be interpolated
  • fileoutput is the raster file in output
  • (xmin, , xmax , ymin , ymax) are the Extent of my layer

This setting (perfectly working when launched from QGis interface) produices a Nodata value Raster (only 1 cell). It seems that the algorithm does not recognize the vector in input. I've also checked the CRS of the layer (with vl.crs().authid() ) and everything sounds good.

Any help? Any experience in detecting different behaviour of the SAME algorithm run by Python through processing instead of from QGIS UI ?

Joseph
  • 75,746
  • 7
  • 171
  • 282
iaborsi
  • 71
  • 3
  • 1
    Did you import the required QGIS modules? Is your PYTHONPATH set to the same python that QGIS uses or another installed version? See http://stackoverflow.com/questions/8922568/error-no-module-named-qgis and http://gis.stackexchange.com/questions/22886/how-to-import-qgis-core-to-python-app – Tristan Forward Jul 15 '15 at 13:45
  • Welcome to GIS:SE @iaborsi! Could you please edit your question to include a snippet of code instead of a one-liner, such as a couple of terms which you needed to define and the imports you used as this could help potential answerers :) – Joseph Jul 15 '15 at 13:59
  • 1
    I'd suggest you to validate that your point layer is correctly loaded, this way: print vl.isValid(), which should print True. Please tell us if that check is OK. – Germán Carrillo Jul 15 '15 at 14:43
  • Thanks to all for your suggestion. However, the problem is not in loading module or layer. I paste below a snippet of the code. – iaborsi Jul 17 '15 at 08:00
  • 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!'

    fileoutput = "C:\Users\iacopo.borsi\Desktop\rastout.tif"

    ext = vl.extent() xmin = ext.xMinimum() ymin = ext.yMinimum() xmax = ext.xMaximum() ymax = ext.yMaximum() myfield = 'Z'

    – iaborsi Jul 17 '15 at 08:08
  • out_ras = processing.runalg("grass:v.surf.idw", vl , 12 ,2.0, myfield ,False, "%f , %f, %f, %f "% (xmin , xmax , ymin , ymax), 20, -1, 0.001, fileoutput)

    fileInfo = QFileInfo(fileoutput) baseName = fileInfo.baseName() rlayer = QgsRasterLayer(fileoutput, baseName)

    if rlayer.isValid(): QgsMapLayerRegistry.instance().addMapLayer( rlayer ) else: print 'Raster layer is not valid!'

    – iaborsi Jul 17 '15 at 08:08

1 Answers1

2

it seems that my problem was in the GRASS algorithm I was using. Now, I've moved to GDAL algorithm named "Grid - InverseDistance To a Power" and it works. I don't really know what was wrong: my only suspect is that after running grass algorithm I would need to "convert" the grass raster map created to a "readble" raster, to be loaded in QGis correctly. I realized such a guess after reading the processing log file and comparing the log obtained by python-based run with the one obtained after launching the process by the GUI. But this is only a guess... hoping this could help some body else playing with GRASS and processing.

iaborsi
  • 71
  • 3