3

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 ?

  • 1
    Did you import the required QGIS modules? Is your PYTHONPATH set to the same python that QGIS uses or another installed version? See stackoverflow.com/questions/8922568/error-no-module-named-qgis and gis.stackexchange.com/questions/22886/… – 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 8: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 8:08
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.