-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.py
144 lines (118 loc) · 4.96 KB
/
util.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
/***************************************************************************
util - Utility functions and classes.
A QGIS plugin
Overlays a user-definable grid on the map.
-------------------
begin : 2012-05-30
copyright : (C) 2012 by John Donovan
email : [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. *
* *
***************************************************************************/
"""
import math
class QgsVector(object):
"""2D vector class with (almost) the same signature as the QGIS one."""
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
def __neg__(self):
return QgsVector(-self.x, -self.y)
def __mul__(self, scalar):
return QgsVector(self.x * scalar, self.y * scalar)
def __div__(self, scalar):
return QgsVector(self.x, self.y) * (1.0 / scalar)
def __add__(self, v):
return QgsVector(self.x + v.x, self.y + v.y)
# Vector dot product.
def __and__(self, v):
return self.x * v.x + self.y * v.y
def length(self):
return math.sqrt(self.x * self.x + self.y * self.y)
def perpVector(self):
return QgsVector(-self.y, self.x)
def angle(self, v=None):
if v is None:
ang = math.atan2(self.y, self.x)
return ang + 2.0 * math.pi if ang < 0.0 else ang
else:
return v.angle() - self.angle()
def rotateBy(self, rot):
ang = math.atan2(self.y, self.x) + rot
length = self.length()
return QgsVector(length * math.cos(ang), length * math.sin(ang))
def normal(self):
if self.length() == 0.0:
raise
return QgsVector(self.x, self.y) / self.length()
class Angle():
cardinals = ('N', 'E', 'S', 'W')
def __init__(self, degrees):
round(degrees, 6)
self.sign = '-' if degrees < 0.0 else ''
decimal_degrees = math.fabs(degrees) % 360.0
self.angle = -decimal_degrees if degrees < 0.0 else decimal_degrees
self.degrees, self.fracDegrees = divmod(decimal_degrees, 1.0)
self.minutes, self.fracMinutes = divmod(self.fracDegrees * 60.0, 1.0)
self.seconds, self.fracSeconds = divmod(self.fracMinutes * 60.0, 1.0)
def __format__(self, format_spec):
s = ''
b, m, e = format_spec.partition('%')
while e != '':
pad = False
precision = 0
s += b
precPos = 0
while e[precPos].isdigit():
precPos += 1
precString = e[:precPos]
if precString != '':
if precString[0] == '0':
pad = True
precision = int(precString)
spec = e[precPos]
if spec == 'g':
s += self.sign
elif spec == 'D':
if pad:
s += str(int(self.degrees)).zfill(3)
else:
s += str(int(self.degrees))
elif spec == 'd':
s += '{0:f}'.format(self.fracDegrees).ljust(2 + precision, '0')[2:2 + precision]
elif spec == 'M':
if pad:
s += str(int(self.minutes)).zfill(2)
else:
s += str(int(self.minutes))
elif spec == 'm':
s += '{0:f}'.format(self.fracMinutes).ljust(2 + precision, '0')[2:2 + precision]
elif spec == 'S':
if pad:
s += str(int(self.seconds)).zfill(2)
else:
s += str(int(self.seconds))
elif spec == 's':
s += '{0:f}'.format(self.fracSeconds).ljust(2 + precision, '0')[2:2 + precision]
elif spec == 'n':
if self.sign == '':
s += Angle.cardinals[0]
else:
s += Angle.cardinals[2]
elif spec == 'e':
if self.sign == '':
s += Angle.cardinals[1]
else:
s += Angle.cardinals[3]
else:
raise ValueError("Invalid format specifier '%c'" % spec)
b, m, e = e[precPos + 1:].partition('%')
s += b
return s