2

I wanted to call kriging algorithm from a python plugin and displays it's output on QGIS desktop.

I have successfully generated the output of the kriging algorithm by unable to find the steps which will help to display the output on QGIS desktop as far as I know there is no help in document(I would be happy if anyone can find and suggest me.)

Can anyone suggest me what should I do to solve this problem. Below is the lines which I have used to generate the output. OS:UBUNTU

import processing
Output = processing.runalg('saga:ordinarykriging', 
                           "/home/harsh/Documents/IIC/Krigging_test/demo.shp", #POINTS <ParameterVector>
                           "cost", #FIELD <parameters from POINTS>
                           0, #TQUALITY <ParameterSelection>, [0] standard deviation
                           True, #LOG <ParameterBoolean>
                           True, #BLOCK <ParameterBoolean>
                           100,  #DBLOCK <ParameterNumber>
                           -1,   #VAR_MAXDIST <ParameterNumber>
                           100,  #VAR_NCLASSES <ParameterNumber>
                           1,    #VAR_NSKIP <ParameterNumber>
                           "a + b * x", #VAR_MODEL <ParameterString>
                           "0, 1, 0, 1", #OUTPUT_EXTENT <ParameterExtent>
                           100.0, #TARGET_USER_SIZE <ParameterNumber>
                           0, #TARGET_USER_FITS <ParameterSelection>, [0] nodes
                           0, #SEARCH_RANGE <ParameterSelection>, [0] local
                           1000, #SEARCH_RADIUS <ParameterNumber>
                           0, #SEARCH_POINTS_ALL <ParameterSelection>,  [0] maximum number of nearest points 
                           4, #SEARCH_POINTS_MIN <ParameterNumber>
                           20, #SEARCH_POINTS_MAX <ParameterNumber>
                           0, #SEARCH_DIRECTION <ParameterSelection>, [0] all directions
                           None, #PREDICTION <OutputRaster>
                           None) #VARIANCE <OutputRaster>

print Output
{'VARIANCE': u'/tmp/processing5f15099df76849648e0f59eea0f3d625/00565712f1274a6082cad84e1a5a944c/VARIANCE.sdat', 'PREDICTION': u'/tmp/processing5f15099df76849648e0f59eea0f3d625/32a911a50c3e480cbf9418a8593d38ca/PREDICTION.sdat'}

enter image description here

[Solved]

ext = QgsVectorLayer( '/home/harsh/Documents/IIC/Krigging_test/demo.shp', '', 'ogr' ).extent()
        xmin = ext.xMinimum()
        xmax = ext.xMaximum()
        ymin = ext.yMinimum()
        ymax = ext.yMaximum()
        coords = "%f,%f,%f,%f" %(xmin, xmax, ymin, ymax) # this is a string that stores the coordinates

        Output = processing.runalg('saga:ordinarykriging', "/home/harsh/Documents/IIC/Krigging_test/demo.shp", "Z", 0, True, True, 100, -1, 100, 1, "a + b * x", coords,100.0,0,0,1000, 0, 4,20,0, None, None)
        print Output
        prediction = QgsRasterLayer(Output['PREDICTION'],
                                    'prediction')

        QgsMapLayerRegistry.instance().addMapLayer(prediction)

        variance = QgsRasterLayer(Output['VARIANCE'],
                      'variance')

        QgsMapLayerRegistry.instance().addMapLayer(variance)
1

Try this code complement:

.
.
.
variance = QgsRasterLayer(Output['VARIANCE'],
                          'variance')

QgsMapLayerRegistry.instance().addMapLayer(variance)

prediction = QgsRasterLayer(Output['PREDICTION'],
                            'prediction')

QgsMapLayerRegistry.instance().addMapLayer(prediction)
  • Thanks for the edit and quick answers. But in the output, it creates the layers(variance and prediction) with the value nan nan and there is no change in the desktop screen. drive.google.com/drive/folders/… In this, I have attached the image of the expected output and the actual output generated please have a look – user730119 Dec 11 '17 at 17:07
  • Your suggestion helped me to solve the problem thanks. Sorry for updating it late :) . – user730119 Dec 23 '17 at 14:05

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.