-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.py
79 lines (62 loc) · 2.54 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
77
78
"""
/***************************************************************************
SOSIexpressions
A QGIS plugin
Expressions related to SOSI
-------------------
begin : 2023-11-22
copyright : (C) 2023 by Morten Sickel
email : [email protected]
Ideas from Ian Turton [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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)
y = float(y)
if x == 0 and y == 0:
return 0
rotate = math.atan2(y,x) / math.pi *180
# This rotation is based on 1,0 is 0 degrees, then goes CCW,
# We want 0 1 to be 0 degrees and then go CW
rotate = 90 - rotate
# The rotation is turned the right way, but the degrees look
# bit funny from -90 to 270
if rotate > 180:
rotate = rotate - 360
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))