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?

  • Can you run the same "deg = math.degrees...." logic/statement within the QGIS python console without error? – artwork21 Jun 23 '17 at 13:14
  • No, the result is 'NameError: name 'math' is not defined' – Jane Jun 23 '17 at 13:24
4

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.

  • 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

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.