-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateTasks.py
More file actions
353 lines (302 loc) · 12.5 KB
/
createTasks.py
File metadata and controls
353 lines (302 loc) · 12.5 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Citizen Cyberscience Centre
#
# 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 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import urllib2
import json
import datetime
import gdal
from gdalconst import *
from optparse import OptionParser
def frange(x, y, step):
"""Float Range Generator function"""
while x < y:
yield x
x += step
def delete_app(api_url, api_key, id):
"""
Deletes the application.
:arg integer id: The ID of the application
:returns: True if the application has been deleted
:rtype: boolean
"""
request = urllib2.Request(api_url + '/api/app/' + str(id) + '?api_key=' + api_key)
request.get_method = lambda: 'DELETE'
if (urllib2.urlopen(request).getcode() == 204):
return True
else:
return False
def update_app(api_url , api_key, id, name = None):
"""
Updates the name of the application
:arg integer id: The ID of the application
:arg string name: The new name for the application
:returns: True if the application has been updated
:rtype: boolean
"""
data = dict(id = id, name = name)
data = json.dumps(data)
request = urllib2.Request(api_url + '/api/app/' + str(id) + '?api_key=' + api_key)
request.add_data(data)
request.add_header('Content-type', 'application/json')
request.get_method = lambda: 'PUT'
if (urllib2.urlopen(request).getcode() == 200):
return True
else:
return False
def update_template(api_url, api_key, app='filtering'):
"""
Update tasks template and long description for the application
:arg string app: Application short_name in PyBossa.
:returns: True when the template has been updated.
:rtype: boolean
"""
request = urllib2.Request('%s/api/app?short_name=%s' %
(api_url, app))
request.add_header('Content-type', 'application/json')
res = urllib2.urlopen(request).read()
res = json.loads(res)
res = res[0]
if res.get('short_name'):
# Re-read the template
file = open('template.html')
text = file.read()
file.close()
# Re-read the long_description
file = open('long_description.html')
long_desc = file.read()
file.close()
info = dict(thumbnail=res['info']['thumbnail'], task_presenter=text)
data = dict(id=res['id'], name=res['name'],
short_name=res['short_name'],
description=res['description'], hidden=res['hidden'],
long_description=long_desc,
info=info)
data = json.dumps(data)
request = urllib2.Request(api_url + '/api/app/' + str(res['id']) + \
'?api_key=' + api_key)
request.add_data(data)
request.add_header('Content-type', 'application/json')
request.get_method = lambda: 'PUT'
if (urllib2.urlopen(request).getcode() == 200):
return True
else:
return False
else:
return False
def update_tasks(api_url, api_key, app='filtering'):
"""
Update tasks question
:arg string app: Application short_name in PyBossa.
:returns: True when the template has been updated.
:rtype: boolean
"""
request = urllib2.Request('%s/api/app?short_name=%s' %
(api_url, app))
request.add_header('Content-type', 'application/json')
res = urllib2.urlopen(request).read()
res = json.loads(res)
app = res[0]
if app.get('short_name'):
request = urllib2.Request('%s/api/task?app_id=%s&limit=%s' %
(api_url, app['id'],'1000'))
request.add_header('Content-type', 'application/json')
res = urllib2.urlopen(request).read()
tasks = json.loads(res)
for t in tasks:
t['info']['question']=u'Is this area too cloudy (more than 50% of the tile)?'
data = dict(info=t['info'],app_id=t['app_id'])
data = json.dumps(data)
request = urllib2.Request(api_url + '/api/task/' + str(t['id']) + \
'?api_key=' + api_key)
request.add_data(data)
request.add_header('Content-type', 'application/json')
request.get_method = lambda: 'PUT'
if (urllib2.urlopen(request).getcode() != 200):
return False
else:
print "TASK %s updated" % (t['id'])
else:
return False
def create_app(api_url , api_key, name=None, short_name=None, description=None,\
template="template.html", tut="tutorial.html"):
"""
Creates the application.
:arg string name: The application name.
:arg string short_name: The slug application name.
:arg string description: A short description of the application.
:returns: Application ID or 0 in case of error.
:rtype: integer
"""
print('Creating app')
name = u'Filter cloudy areas' # Name with a typo
short_name = u'filtering'
description = u'Is this area too cloudy (more than 50% of the tile)?'
# JSON Blob to present the tasks for this app to the users
# First we read the template:
file = open(template)
text = file.read()
file.close()
file = open(tut)
text_tutorial = file.read()
file.close()
info = dict (thumbnail="http://img36.imageshack.us/img36/285/cloudyr.jpg",
task_presenter = text, tutorial = text_tutorial)
data = dict(name = name, short_name = short_name, description = description,
hidden = 0, info = info)
data = json.dumps(data)
# Checking which apps have been already registered in the DB
apps = json.loads(urllib2.urlopen(api_url + '/api/app' + '?api_key=' + api_key).read())
for app in apps:
if app['short_name'] == short_name:
print('{app_name} app is already registered in the DB'.format(app_name = name))
print('Deleting it!')
if (delete_app(api_url, api_key, app['id'])): print "Application deleted!"
print("The application is not registered in PyBOSSA. Creating it...")
# Setting the POST action
request = urllib2.Request(api_url + '/api/app?api_key=' + api_key )
request.add_data(data)
request.add_header('Content-type', 'application/json')
# Create the app in PyBOSSA
output = json.loads(urllib2.urlopen(request).read())
if (output['id'] != None):
print("Done!")
return output['id']
else:
print("Error creating the application")
return 0
def create_task(api_url , api_key, app_id, tile):
"""
Creates tasks for the application
:arg integer app_id: Application ID in PyBossa.
:returns: Task ID in PyBossa.
:rtype: integer
"""
# Process the tile
dataset = gdal.Open(tile, GA_ReadOnly)
print 'Driver: %s' % dataset.GetDriver().ShortName
print 'Size is %s x %s y' % (dataset.RasterXSize, dataset.RasterYSize)
width = dataset.RasterXSize
height = dataset.RasterYSize
print 'Projection is: %s' % dataset.GetProjection()
# From http://osgeo-org.1560.n6.nabble.com/get-corner-coordinates-from-gdalopen-td3749527.html
gt = dataset.GetGeoTransform()
minX = gt[0]
minY = gt[3] + width*gt[4] + height*gt[5]
maxX = gt[0] + width*gt[1] + height*gt[2]
maxY = gt[3]
bounds = [minX, minY, maxX, maxY]
print 'Bounds: %s' % bounds
# Compute the restrictedExtents for the tiles:
numTiles = 16
x = 0
y = 0
stepX = ((maxX - minX)/numTiles)
stepY = ((maxY - minY)/numTiles)
extents = []
for x in frange(minX, maxX, stepX ):
for y in frange(minY, maxY, stepY):
extents.append([x,y, x + stepX, y + stepY])
# Create a task per extent
for e in extents:
# Data for the tasks
t = dict (name = tile.rstrip(),
bounds = bounds,
restrictedExtent = e,
projection = dataset.GetProjection(),
width = width,
height = height
)
info = dict (tile=t, question=u'Is this area too cloudy (more than 50% of the tile)?')
data = dict (app_id = app_id, state = 0, info = info, calibration = 0, priority_0 = 0)
data = json.dumps(data)
# Setting the POST action
request = urllib2.Request(api_url + '/api/task' + '?api_key=' + api_key)
request.add_data(data)
request.add_header('Content-type', 'application/json')
# Create the task
output = json.loads(urllib2.urlopen(request).read())
if (output['id'] == None):
return False
def get_tiles(file):
"""
Gets tiles from a file
:arg string file: File name that has all the tiles names
:returns: A list of tiles.
:rtype: list
"""
file = open(file)
tiles = file.readlines()
file.close()
return tiles
import sys
if __name__ == "__main__":
# Arguments for the application
usage = "usage: %prog [options]"
parser = OptionParser(usage)
parser.add_option("-s", "--server", dest="api_url", help="PyBossa URL http://domain.com/", metavar="URL")
parser.add_option("-k", "--api-key", dest="api_key", help="PyBossa User API-KEY to interact with PyBossa", metavar="API-KEY")
parser.add_option("-p", "--template", dest="template", help="PyBossa HTML+JS template for application presenter", metavar="TEMPLATE")
parser.add_option("-b", "--tutorial", dest="tutorial", help="App tutorial template for application presenter", metavar="TUTORIAL")
parser.add_option("-t", "--tiles", dest="tiles", help="File with the name of the tiles", metavar="TILES")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose")
# Create App
parser.add_option("-a", "--create-app", action="store_true",
dest="create_app",
help="Create the application",
metavar="CREATE-APP")
# Update template for tasks and long_description for app
parser.add_option("-u", "--update-template", action="store_true",
dest="update_template",
help="Update Tasks template",
metavar="UPDATE-TEMPLATE"
)
# Update tasks question
parser.add_option("-q", "--update-tasks", action="store_true",
dest="update_tasks",
help="Update Tasks question",
metavar="UPDATE-TASKS"
)
(options, args) = parser.parse_args()
if not options.api_url:
options.api_url = 'http://localhost:5000'
if not options.api_key:
parser.error("You must supply an API-KEY to create an applicationa and tasks in PyBossa")
if not options.template:
print("Using default template: template.html")
options.template = "template.html"
if not options.tutorial:
print("Using default tutorial template: tutorial.html")
options.tutorial = "tutorial.html"
if not options.tiles:
parser.error("You must supply a file name with the tiles")
if (options.verbose):
print('Running against PyBosssa instance at: %s' % options.api_url)
print('Using API-KEY: %s' % options.api_key)
if options.update_template:
print "Updating app template"
update_template(options.api_url, options.api_key)
if options.update_tasks:
print "Updating task question"
update_tasks(options.api_url, options.api_key)
if options.create_app:
app_id = create_app(options.api_url, options.api_key,\
template = options.template, tut = options.tutorial)
tiles = get_tiles(options.tiles)
#for tile in tiles:
#create_task(options.api_url, options.api_key, app_id, tile)
create_task(options.api_url, options.api_key, app_id, 'demo.tif')
if not options.create_app and not options.update_template:
parser.error("Please check --help or -h for the available options")