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?
2 Answers 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.
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
Note: I strongly suggest you call your file something else to avoid confusion with the function
method :)
-
Sorry it doesn't work.Domenico Fuoco– Domenico Fuoco2017年08月25日 13:41:17 +00:00Commented Aug 25, 2017 at 13:41
-
@DomenicoFuoco - Did you restart QGIS?Joseph– Joseph2017年08月25日 13:42:43 +00:00Commented 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 usefrom function import *
and then useA = function.TriangleArea.function(3, 5, None, None)
.Joseph– Joseph2017年08月25日 13:52:18 +00:00Commented 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 runTriangleArea
. I strongly suggest you change your filename to something else to avoid confusion.Joseph– Joseph2017年08月25日 14:12:13 +00:00Commented Aug 25, 2017 at 14:12 -
1I'm really sorry I knew badly. I followed your suggestion and the script works perfectly. Thank youDomenico Fuoco– Domenico Fuoco2017年08月25日 14:18:46 +00:00Commented Aug 25, 2017 at 14:18
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
Explore related questions
See similar questions with these tags.
/.qgis2/python/
directory?