-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.py
212 lines (183 loc) · 6.43 KB
/
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
""" This module contains miscellaneous utility functions and classes for creating the OWS webservices. The most useful
functions in the module are probably the date manipulation functions.
"""
from collections import namedtuple
import datetime
from django.core.exceptions import ValidationError
from django.forms import MultipleChoiceField, Field
from django.utils.formats import sanitize_separators
from osgeo import osr
import re
def _from_today(match):
plusminus = match.group(1)
amt = match.group(2)
if plusminus == '-':
return datetime.date.today() - datetime.timedelta(days=int(amt))
else:
return datetime.date.today() + datetime.timedelta(days=int(amt))
def _from_now(match):
def maybeint(i):
if i:
return int(i)
else:
return 0
plusminus = match.group(1)
weeks = maybeint( match.group(2) )
days = maybeint( match.group(3) )
hours = maybeint(match.group(4) )
mins =maybeint(match.group(5) )
seconds = maybeint(match.group(6))
milliseconds = maybeint(match.group(7))
if plusminus == '-':
return datetime.datetime.utcnow() - datetime.timedelta(
weeks=weeks, days=days, hours=hours, mins=mins, seconds=seconds, milliseconds=milliseconds)
def parsetime(t):
"""Parses a time string into a datetime object. This is the function used by parse the dates in an OWS request, so
all OWS requests accept these date formats. ParseTime accepts the following formats:
* '%Y.%m.%d-%H:%M:%S.%f'
* '%Y.%m.%d-%H:%M:%S'
* '%Y.%m.%d-%H:%M'
* '%Y.%m.%d'
* '%Y%m%d%H%M%S%f'
* '%Y%m%d%H%M%S'
* '%Y%m%d%H%M'
* '%Y%m%d'
* '%Y.%m'
* '%Y'
* '%Y.%m.%d-%H:%M:%S.%f'
* '%Y/%m/%d-%H:%M:%S'
* '%Y/%m/%d-%H:%M'
* '%Y/%m/%d'
* '%Y/%m'
* '%Y'
* "now"
* "today"
* "today+${days}"
* "now+${weeks}w${days}d${hours}h${mins}m${seconds}s${millisecs}ms"
:param t: a string in one of the above formats.
:return: a datetime object
"""
timeformats = [
'%Y.%m.%d-%H:%M:%S.%f',
'%Y.%m.%d-%H:%M:%S',
'%Y.%m.%d-%H:%M',
'%Y.%m.%d',
'%Y%m%d%H%M%S%f',
'%Y%m%d%H%M%S',
'%Y%m%d%H%M',
'%Y%m%d',
'%Y.%m',
'%Y',
'%Y.%m.%d-%H:%M:%S.%f',
'%Y/%m/%d-%H:%M:%S',
'%Y/%m/%d-%H:%M',
'%Y/%m/%d',
'%Y/%m',
'%Y'
]
alt_formats = {
'now' : datetime.datetime.utcnow(),
'today' : datetime.date.today(),
}
high_level = [
(re.compile('today(\+|-)([0-9]+)'), _from_today),
(re.compile('now(\+|-)([0-9]+w)?([0-9]+d)?([0-9]+h)?([0-9]+m)?([0-9]+s)?([0-9]+ms)?'), _from_now)
]
if not t:
return None
ret = None
for tf in timeformats:
try:
ret = datetime.datetime.strptime(t, tf)
except:
pass
if not ret and t in alt_formats:
return alt_formats[t]
elif not ret:
for tf, l in high_level:
k = tf.match(t)
if k:
ret = l(k)
if ret:
return ret
else:
raise ValueError('time data does not match any valid format: ' + t)
def create_spatialref(srs, srs_format='srid'):
"""
**Deprecated - use Django's SpatialRef class**. Create an :py:class:`osgeo.osr.SpatialReference` from an srid, wkt,
projection, or epsg code. srs_format should be one of: srid, wkt, proj,
epsg to represent a format in numerical srid form, well-known text, proj4,
or epsg formats.
"""
spatialref = osr.SpatialReference()
if srs_format:
if srs_format == 'srid':
spatialref.ImportFromEPSG(srs)
elif srs_format == 'wkt':
spatialref.ImportFromWkt(srs)
elif srs_format == 'proj':
spatialref.ImportFromProj4(srs)
else:
spatialref.ImportFromEPSG(int(srs.split(':')[1]))
return spatialref
mimetypes = namedtuple("MimeTypes", (
'json', 'jsonp')
)(
json='application/json',
jsonp='text/plain'
)
class CaseInsensitiveDict(dict):
"""
A subclass of :py:class:django.utils.datastructures.MultiValueDict that treats all keys as lower-case strings
"""
def __init__(self, key_to_list_mapping=()):
def fix(pair):
key, value = pair
return key.lower(),value
super(CaseInsensitiveDict, self).__init__([fix(kv) for kv in key_to_list_mapping])
def __getitem__(self, key):
return super(CaseInsensitiveDict, self).__getitem__(key.lower())
def __setitem__(self, key, value):
return super(CaseInsensitiveDict, self).__setitem__(key.lower(), value)
def get(self, key, default=None):
if key not in self:
return default
else:
return self[key]
def getlist(self, key):
if key not in self:
return []
elif isinstance(self[key], list):
return self[key]
elif isinstance(self[key], tuple):
return list(self[key])
else:
return [self[key]]
class MultipleValueField(MultipleChoiceField):
"""A field for pulling in arbitrary lists of strings instead of constraining them by choice"""
def validate(self, value):
if self.required and not value:
raise ValidationError(self.error_messages['required'])
class BBoxField(Field):
"""A field that represents a bounding box in minx,miny,maxx,maxy format - parses the bbox field from an OWS request.
"""
def to_python(self, value):
value = super(BBoxField, self).to_python(value)
if not value:
return -180.0,-90.0,180.0,90.0
try:
lx, ly, ux, uy = value.split(',')
if self.localize:
lx = float(sanitize_separators(lx))
ly = float(sanitize_separators(ly))
ux = float(sanitize_separators(ux))
uy = float(sanitize_separators(uy))
if uy < ly or ux < lx:
raise ValidationError("BBoxes must be in lower-left(x,y), upper-right(x,y) order")
except (ValueError, TypeError):
raise ValidationError("BBoxes must be four floating point values separated by commas")
lx = float(sanitize_separators(lx))
ly = float(sanitize_separators(ly))
ux = float(sanitize_separators(ux))
uy = float(sanitize_separators(uy))
return lx, ly, ux, uy