2

I'm trying to import a custom function from the python console, but it return the error:

'QgsExpressionFunction' object is not callable

My example function (saved as function.py file in /.qgis2/python/ directory) is:

from qgis.core import *
from qgis.utils import qgsfunction
from qgis.core import QgsExpression
@qgsfunction(args="auto", group='Custom')
def TriangleArea(b,h,feature,parent):
 area = b*h/2.0
 return area
 QgsExpression.registerFunction(TriangleArea) 

To import and execute the function i use:

from function import *
A = TriangleArea(3, 5)
print A

what's wrong?

underdark
84.9k22 gold badges237 silver badges418 bronze badges
asked Aug 25, 2017 at 12:30
2
  • Is your function.py file saved in your /.qgis2/python/ directory? Commented Aug 25, 2017 at 12:36
  • @Joseph, yes it is. Commented Aug 25, 2017 at 12:38

2 Answers 2

2

Try using the following where you can import your TriangleArea() function and then use the method function to run it:

from function import TriangleArea
A = TriangleArea.function(3, 5, None, None)
print A

The feature and parent parameters can be left empty.

Result


Another way is to import your file directly and then call your function:

import function
A = function.TriangleArea.function(3, 5, None, None)
print A

Result


Note: I strongly suggest you call your file something else to avoid confusion with the function method :)

answered Aug 25, 2017 at 13:22
8
  • Sorry it doesn't work. Commented Aug 25, 2017 at 13:41
  • @DomenicoFuoco - Did you restart QGIS? Commented Aug 25, 2017 at 13:42
  • 1
    @DomenicoFuoco - Not necessarily because I used from function import TriangleArea which means I can call the function directly. You could use from function import * and then use A = function.TriangleArea.function(3, 5, None, None). Commented Aug 25, 2017 at 13:52
  • 1
    @DomenicoFuoco - I have edited the post with some screenshots because I think there's a little misunderstanding. Your filename is called function. But there is a method also called function() which is used to run TriangleArea. I strongly suggest you change your filename to something else to avoid confusion. Commented Aug 25, 2017 at 14:12
  • 1
    I'm really sorry I knew badly. I followed your suggestion and the script works perfectly. Thank you Commented Aug 25, 2017 at 14:18
1

With @Joseph 's suggestions I created a function that opens a raster grid and passes the values into a numpy array. The procedure I have adopted is:

from qgis.core import *
from qgis.utils import qgsfunction
from qgis.core import QgsExpression
import numpy as np
@qgsfunction(args="auto", group='Custom')
def OpenGrid(GridPath, feature, parent):
 GridName = QgsRasterLayer(GridPath)
 # Create array 'values_GridName'
 provider = GridName.dataProvider()
 extent = provider.extent()
 rows = GridName.height()
 cols = GridName.width()
 xmin = extent.xMinimum()
 ymax = extent.yMaximum()
 xsize = GridName.rasterUnitsPerPixelX()
 ysize = GridName.rasterUnitsPerPixelY()
 block = provider.block(1, extent, cols, rows)
 values_GridName = [ [] for i in range(rows) ]
 for i in range(rows):
 for j in range(cols):
 block.value(i,j)
 values_GridName[i].append(block.value(i,j))
 #This code line is used to make sure all numbers are float type
 values_GridName = np.array(values_GridName, dtype = float)
 return values_GridName

The function was saved in /.qgis2/python/OpenRlayer.py.

I call the function with the following commands:

from OpenRlayer import OpenGrid 
grid_path = path/to/grid.asc
values_Grid = OpenGrid.function(grid_path, None, None)

This is how I created the array that contains the grid data and now I can do the operations I need

answered Aug 25, 2017 at 22:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.