forked from sickel/SOSIexpression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
77 lines (60 loc) · 2.33 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
/***************************************************************************
SOSIexpressions
A QGIS plugin
Expressions related to SOSI
-------------------
begin : 2023-11-22
copyright : (C) 2023 by Morten Sickel
email : morten@sickel.net
Ideas from Ian Turton ianturton@astuntechnology.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
__author__ = 'Morten Sickel'
__date__ = '2023-11-22'
__copyright__ = '(C) 2023 by Morten Sickel'
from qgis.core import (
QgsExpression
)
from qgis.utils import qgsfunction
groupName = "SOSI"
import math
@qgsfunction(args='auto', group='SOSI',usesgeometry = False)
def rotate_from_vector(vectorfield,feature,parent):
"""
From a field that can be None or a vector given as
two space separated floating point numbers, return a rotation
angle
:param vectorfield: Name of the field containing the vector data
:return: Rotation in degrees clockwise
"""
if vectorfield is None:
return(0)
(x,y) = vectorfield.split(" ")
x = float(x)
if x == 0:
rotate = 90
if y == 0:
rotate = 0
y = float(y)
tng = y/x
rotate = math.atan(tng) / math.pi *180
if x < 0:
rotate = rotate + 180
return(rotate)
def registerFunctions(isRegister=True):
t_register = (QgsExpression.registerFunction, lambda f: f)
u_register = (QgsExpression.unregisterFunction, lambda f: f.name())
(funcReg, funcArg) = t_register if isRegister else u_register
g = globals()
l_func = (g[v] for v in g if hasattr(g[v], 'usesGeometry'))
for f in l_func:
funcReg(funcArg(f))