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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
# -*- coding: utf-8 -*-
module = "climate"
resourcename = request.function
#s3_menu(module)
response.menu = s3_menu_dict[module]["menu"]
response.menu.append(s3.menu_help)
response.menu.append(s3.menu_auth)
if s3.menu_admin:
response.menu.append(s3.menu_admin)
ClimateDataPortal = local_import("ClimateDataPortal")
SampleTable = ClimateDataPortal.SampleTable
DSL = local_import("ClimateDataPortal.DSL")
sample_types = SampleTable._SampleTable__types.values()
variable_names = SampleTable._SampleTable__names.keys()
def map_plugin(**client_config):
return ClimateDataPortal.MapPlugin(
env = Storage(globals()),
year_max = 2100,
year_min = 1950,
place_table = climate_place,
client_config = client_config
)
def index():
try:
module_name = deployment_settings.modules[module].name_nice
except:
module_name = T("Climate")
# Include an embedded Overview Map on the index page
config = gis.get_config()
if not deployment_settings.get_security_map() or s3_has_role("MapAdmin"):
catalogue_toolbar = True
else:
catalogue_toolbar = False
search = True
catalogue_layers = False
if config.wmsbrowser_url:
wms_browser = {
"name" : config.wmsbrowser_name,
"url" : config.wmsbrowser_url
}
else:
wms_browser = None
print_service = deployment_settings.get_gis_print_service()
if print_service:
print_tool = {"url": print_service}
else:
print_tool = {}
map = gis.show_map(
lat = 28.5,
lon = 84.1,
zoom = 7,
toolbar = False,
# catalogue_toolbar=catalogue_toolbar, # T/F, top tabs toolbar
wms_browser = wms_browser, # dict
plugins = [
map_plugin(
**request.vars
)
]
)
response.title = module_name
return dict(
module_name=module_name,
map=map
)
month_names = dict(
January=1,
February=2,
March=3,
April=4,
May=5,
June=6,
July=7,
August=8,
September=9,
October=10,
November=11,
December=12
)
for name, number in month_names.items():
month_names[name[:3]] = number
for name, number in month_names.items():
month_names[name.upper()] = number
for name, number in month_names.items():
month_names[name.lower()] = number
def convert_date(default_month):
def converter(year_month):
components = year_month.split("-")
year = int(components[0])
assert 1960 <= year, "year must be >= 1960"
try:
month_value = components[1]
except IndexError:
month = default_month
else:
try:
month = int(month_value)
except TypeError:
month = month_names[month_value]
assert 1 <= month <= 12, "month must be in range 1:12"
return datetime.date(year, month, 1)
return converter
def one_of(options):
def validator(choice):
assert choice in options, "should be one of %s, not '%s'" % (
options,
choice
)
return choice
return validator
aggregation_names = ("Maximum", "Minimum", "Average")
def climate_overlay_data():
kwargs = dict(request.vars)
arguments = {}
errors = []
for kwarg_name, converter in dict(
query_expression = str,
).iteritems():
try:
value = kwargs.pop(kwarg_name)
except KeyError:
errors.append("%s missing" % kwarg_name)
else:
try:
arguments[kwarg_name] = converter(value)
except TypeError:
errors.append("%s is wrong type" % kwarg_name)
except AssertionError, assertion_error:
errors.append("%s: %s" % (kwarg_name, assertion_error))
if kwargs:
errors.append("Unexpected arguments: %s" % kwargs.keys())
if errors:
raise HTTP(400, "<br />".join(errors))
else:
import gluon.contrib.simplejson as JSON
try:
data_path = map_plugin().get_overlay_data(**arguments)
except SyntaxError, syntax_error:
raise HTTP(400, JSON.dumps({
"error": "SyntaxError",
"lineno": syntax_error.lineno,
"offset": syntax_error.offset,
"understood_expression": syntax_error.understood_expression
}))
except (DSL.MeaninglessUnitsException, TypeError), exception:
raise HTTP(400, JSON.dumps({
"error": exception.__class__.__name__,
"analysis": str(exception)
}))
else:
return response.stream(
open(data_path,"rb"),
chunk_size=4096
)
def list_of(converter):
def convert_list(choices):
return map(converter, choices)
return convert_list
def climate_chart():
import gluon.contenttype
data_image_file_path = _climate_chart(gluon.contenttype.contenttype(".png"))
return response.stream(
open(data_image_file_path,"rb"),
chunk_size=4096
)
def _climate_chart(content_type):
kwargs = dict(request.vars)
import gluon.contrib.simplejson as JSON
specs = JSON.loads(kwargs.pop("spec"))
checked_specs = []
for spec in specs:
arguments = {}
errors = []
for name, converter in dict(
query_expression = str,
place_ids = list_of(int)
).iteritems():
try:
value = spec.pop(name)
except KeyError:
errors.append("%s missing" % name)
else:
try:
arguments[name] = converter(value)
except TypeError:
errors.append("%s is wrong type" % name)
except AssertionError, assertion_error:
errors.append("%s: %s" % (name, assertion_error))
if spec:
errors.append("Unexpected arguments: %s" % spec.keys())
checked_specs.append(arguments)
if errors:
raise HTTP(400, "<br />".join(errors))
else:
response.headers["Content-Type"] = content_type
data_image_file_path = map_plugin().render_plots(
specs = checked_specs,
width = int(kwargs.pop("width")),
height = int(kwargs.pop("height"))
)
return data_image_file_path
def climate_chart_download():
data_image_file_path = _climate_chart("application/force-download")
import os
response.headers["Content-disposition"] = (
"attachment; filename=" +
os.path.basename(data_image_file_path)
)
return response.stream(
open(data_image_file_path,"rb"),
chunk_size=4096
)
def chart_popup():
return {}
def buy_data():
return {}
def stations():
"return all station data in JSON format"
stations_strings = []
append = stations_strings.append
extend = stations_strings.extend
for place_row in db(
(db.climate_place.id == db.climate_place_elevation.id) &
(db.climate_place.id == db.climate_place_station_id.id) &
(db.climate_place.id == db.climate_place_station_name.id)
).select(
db.climate_place.id,
db.climate_place.longitude,
db.climate_place.latitude,
db.climate_place_elevation.elevation_metres,
db.climate_place_station_id.station_id,
db.climate_place_station_name.name
):
append(
"".join((
"(", str(place_row.climate_place.id), ",{",
'"longitude":', str(place_row.climate_place.longitude),
',"latitude":', str(place_row.climate_place.latitude),
"}"
))
)
return "[%s]" % ",".join(stations_strings)
def places():
response.headers["Expires"] = "Sat, 25 Dec 2011 20:00:00 GMT"
return response.stream(
open(map_plugin().place_data(),"rb"),
chunk_size=4096
)
# =============================================================================
def sample_table_spec():
output = s3_rest_controller()
return output
# =============================================================================
def station_parameter():
output = s3_rest_controller()
return output
# =============================================================================
def purchase():
def prep(r):
if r.method == "read":
response.s3.rfooter = DIV(
T("Please make your payment in person at the DHM office, or by bank Transfer to:"),
TABLE(
TR("Bank Name","Nepal Rastra Bank"),
TR("Branch Address","Kathmandu Banking Office"),
TR("Branch City","Kathmandu"),
TR("A/c Holder","Dept. of Hydrology and Meteorology"),
TR("Office Code No","27-61-04"),
TR("Office A/c No","KA 1-1-199"),
TR("Revenue Heading No","1-1-07-70")
)
)
return True
response.s3.prep = prep
output = s3_rest_controller()
output["addheader"] = T("Please enter the details of the data you wish to purchase")
return output
# =============================================================================
def save_query():
output = s3_rest_controller()
return output
def request_image():
vars = request.vars
import subprocess
#screenshot_renderer_stdout = StringIO.StringIO()
url = (
"http://%(http_host)s/%(application_name)s/%(controller)s"
"?expression=%(expression)s"
"&filter=%(filter)s"
"&display_mode=print"
) % dict(
vars,
controller = request.controller,
application_name = request.application,
http_host = request.env.http_host,
server_port = request.env.server_port,
)
width = int(vars["width"])
height = int(vars["height"])
# PyQT4 signals don't like not being run in the main thread
# run in a subprocess to give it it's own thread
subprocess_args = (
request.env.applications_parent+"/applications/"+
request.application+"/modules/webkit_url2png.py",
url,
str(width),
str(height)
)
stdoutdata, stderrdata = subprocess.Popen(
subprocess_args,
bufsize = 4096,
stdout = subprocess.PIPE, # StringIO doesn't work
stderr = subprocess.PIPE
).communicate()
# if stderrdata:
# raise Exception(stderrdata)
# else:
response.headers["Content-Type"] = "application/force-download"
response.headers["Content-disposition"] = (
"attachment; filename=MapScreenshot.png"
)
import StringIO
return response.stream(
StringIO.StringIO(stdoutdata),
chunk_size=4096
)
|