/
unit_utils.py
268 lines (253 loc) · 7.91 KB
/
unit_utils.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import warnings
from collections import namedtuple
from datetime import datetime
UNITS = namedtuple(
"UNITS", "distance time time2 crs", defaults=(None, None, None, None)
)
DISTANCE_UNIT_LIST = [
{"abbr": "km", "conv": 1000, "fullname": "Kilometer"},
{"abbr": "m", "conv": 1, "fullname": "metre"},
{"abbr": "dm", "conv": 0.1, "fullname": "Decimeter"},
{"abbr": "cm", "conv": 0.01, "fullname": "Centimeter"},
{"abbr": "mm", "conv": 0.001, "fullname": "Millimeter"},
{"abbr": "nm", "conv": 1852.0, "fullname": "International Nautical Mile"},
{"abbr": "inch", "conv": 0.0254, "fullname": "International Inch"},
{"abbr": "ft", "conv": 0.3048, "fullname": "International Foot"},
{"abbr": "yd", "conv": 0.9144, "fullname": "International Yard"},
{"abbr": "mi", "conv": 1609.344, "fullname": "International Statute Mile"},
{"abbr": "link", "conv": 0.201168, "fullname": "International Link"},
{"abbr": "chain", "conv": 20.1168, "fullname": "International Chain"},
{"abbr": "fathom", "conv": 1.8288, "fullname": "International Fathom"},
{
"abbr": "british_ft",
"conv": 0.304799471538676,
"fullname": "British foot (Sears 1922)",
"epsg": 9041,
},
{
"abbr": "british_yd",
"conv": 0.914398414616029,
"fullname": "British yard (Sears 1922)",
"epsg": 9040,
},
{
"abbr": "british_chain_sears",
"conv": 20.11677651215526,
"fullname": "British chain (Sears 1922)",
"epsg": 9042,
},
{
"abbr": "british_link_sears",
"conv": 0.20116767651215526,
"fullname": "British link (Sears 1922)",
"epsg": 9043,
},
{"abbr": "sears_yd", "conv": 0.914398414616029, "fullname": "Yard (Sears)"},
{"abbr": "link_sears", "conv": 0.20116767651215526, "fullname": "Link (Sears)"},
{"abbr": "chain_sears", "conv": 20.11677651215526, "fullname": "Chain (Sears)"},
{
"abbr": "british_ft_sears_truncated",
"conv": 0.914398,
"fullname": "British foot (Sears 1922 truncated)",
"epsg": 9300,
},
{
"abbr": "british_chain_sears_truncated",
"conv": 20.11676,
"fullname": "British chain (Sears 1922 truncated)",
"epsg": 9301,
},
{
"abbr": "british_chain_benoit",
"conv": 20.116782494375872,
"fullname": "British chain (Benoit 1895 B)",
"epsg": 9062,
},
{
"abbr": "chain_benoit",
"conv": 20.116782494375872,
"fullname": "Chain (Benoit)",
"epsg": 9062,
},
{
"abbr": "link_benoit",
"conv": 0.20116782494375872,
"fullname": "Link (Benoit)",
"epsg": 9063,
},
{
"abbr": "clarke_yd",
"conv": 0.9143917962,
"fullname": "Clarke's yard",
"epsg": 9037,
},
{
"abbr": "clarke_ft",
"conv": 0.3047972654,
"fullname": "Clarke's Foot",
"epsg": 9005,
},
{
"abbr": "clarke_link",
"conv": 0.201166195164,
"fullname": "Clarke's link",
"epsg": 9039,
},
{
"abbr": "clarke_chain",
"conv": 20.1166195164,
"fullname": "Clarke's chain",
"epsg": 9038,
},
{
"abbr": "british_ft_1936",
"conv": 0.3048007491,
"fullname": "British foot (1936)",
"epsg": 9095,
},
{
"abbr": "gold_coast_ft",
"conv": 0.3047997101815,
"fullname": "Gold Coast foot",
"epsg": 9094,
},
{"abbr": "rod", "conv": 0.1988387815, "fullname": "Rod"},
{"abbr": "furlong", "conv": 201.168, "fullname": "Furlong"},
{
"abbr": "german_m",
"conv": 1.0000135965,
"fullname": "German legal metre",
"epsg": 9031,
},
{"abbr": "survey_in", "conv": 0.0254000508001016, "fullname": "US survey inch"},
{
"abbr": "survey_ft",
"conv": 0.3048006096012192,
"fullname": "US survey foot",
"epsg": 9003,
},
{"abbr": "survey_yd", "conv": 0.9144018288036575, "fullname": "US survey yard"},
{
"abbr": "survey_lk",
"conv": 0.20116840233680463,
"fullname": "US survey link",
"epsg": 9034,
},
{
"abbr": "survey_ch",
"conv": 20.116840233680463,
"fullname": "US survey chain",
"epsg": 9033,
},
{
"abbr": "survey_mi",
"conv": 1609.3472186944373,
"fullname": "US survey mile",
"epsg": 9035,
},
{
"abbr": "indian_yd",
"conv": 0.914398530744441,
"fullname": "Indian Yard",
"epsg": 9084,
},
{
"abbr": "indian_ft",
"conv": 0.3047995104977167,
"fullname": "Indian Foot",
"epsg": 9080,
},
{
"abbr": "indian_ft_1937",
"conv": 0.30479841,
"fullname": "Indian Foot",
"epsg": 9081,
},
{
"abbr": "indian_ft_1962",
"conv": 0.3047996,
"fullname": "Indian Foot",
"epsg": 9082,
},
{
"abbr": "indian_ft_1975",
"conv": 0.3047995,
"fullname": "Indian Foot",
"epsg": 9083,
},
{
"abbr": "deg",
"conv": 1,
"fullname": "degree",
"epsg": 4326,
}, # To allow geodesic conversions
]
TIME_UNIT_LIST = [
{"abbr": "s", "conv": 1, "fullname": "seconds"},
{"abbr": "min", "conv": 60, "fullname": "minutes"},
{"abbr": "h", "conv": 3600, "fullname": "hours"},
{"abbr": "d", "conv": 86400, "fullname": "days"},
{"abbr": "a", "conv": 31557600, "fullname": "years"},
]
def to_unixtime(t):
"""
Return float of total seconds since Unix time.
"""
return (t - datetime(1970, 1, 1, 0, 0, 0)).total_seconds()
def get_conversion(units, crs_units):
"""
Looks up unit conversions in the unit dictionaries
If distance specified, lookup distance and crs conversions and check time
If time specified, lookup time conversion and check if time2 specified
If time2 specified, lookup time2 conversion
Unit conversions default to 1 if not specified
"""
d_conv, t_conv, t2_conv, crs_conv = 1, 1, 1, 1
if isinstance(units, tuple):
units = UNITS(*units)
else:
units = UNITS(units)
if units.distance is not None:
try:
d_conv = [
d["conv"] for d in DISTANCE_UNIT_LIST if d.get("abbr") == units.distance
][0]
except IndexError:
raise ValueError("Invalid distance units!")
else:
try:
crs_conv = [
d["conv"]
for d in DISTANCE_UNIT_LIST
if d.get("fullname") == crs_units
][0]
except (IndexError, AttributeError):
crs_conv = 1
warnings.warn(
"No valid CRS distance units. Computations will "
"assume CRS distance units are meters",
category=MissingCRSWarning,
)
finally:
if units.time is not None:
try:
t_conv = [
t["conv"]
for t in TIME_UNIT_LIST
if t.get("abbr") == units.time
][0]
except IndexError:
raise ValueError("Invalid time units!")
else:
if units.time2 is not None:
try:
t2_conv = [
t["conv"]
for t in TIME_UNIT_LIST
if t.get("abbr") == units.time2
][0]
except IndexError:
raise ValueError("Invalid second time units!")
return UNITS(d_conv, t_conv, t2_conv, crs_conv)
class MissingCRSWarning(UserWarning, ValueError):
pass