2

I am trying to write function in QGIS via function editor and the problem is to call QGIS built-in functions e.g. azimuth, centroid... etc. When I run my script, I get

'global name 'azimuth' is not defined'. 

The same problem I got while I'm not specified module 'math' to function degree. My function code is on the picture:

here is my function

I imported different libraries, nevertheless function doesn't work. Does anyone have any ideas how to call these functions?

mgri
  • 16,159
  • 6
  • 47
  • 80
Jane
  • 1,066
  • 7
  • 20

1 Answers1

5

You can't run the built-in expressions as they are defined in the Field Calculator framework. This means that any expression like azimuth, @geometry, centroid and so on won't work if you try to use them when building a custom function.

Instead, you need to use the common PyQGIS syntax as you were writing a script from the Python Console or the Processing Toolbox.

mgri
  • 16,159
  • 6
  • 47
  • 80
  • Thanks!! I've found the majority of the Qgis expressions. But azimuth I' m not found yet anywhere – Jane Jun 23 '17 at 13:58
  • You may use azim = first_point.azimuth(second_point), where azim is the azimuth value and first_point and second_point are QgsPoint objects. Here you will find another example with the using of the azimuth function. – mgri Jun 23 '17 at 15:19