-
Notifications
You must be signed in to change notification settings - Fork 1
/
createTasks.py
268 lines (245 loc) · 10.4 KB
/
createTasks.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Citizen Cyberscience Centre, Swiss TPH
#
# 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 json
import shapefile
import logging
from argparse import ArgumentParser
from ConfigParser import RawConfigParser
import requests
import pbclient
import simplekml
import time
from matplotlib.path import Path
from matplotlib.transforms import Bbox
def check_api_error(api_response):
"""Check if returned API response contains an error"""
if type(api_response) == dict and (api_response.get('status') == 'failed'):
print(api_response)
raise requests.exceptions.HTTPError
def format_error(module, error):
"""Format the error for the given module"""
logging.error(module)
# Beautify JSON error
if type(error) == list:
print "Application not found"
else:
print json.dumps(error, sort_keys=True, indent=4, separators=(',', ': '))
exit(1)
def polygon_file_to_path(filename):
area_polygon = None
if filename.endswith(".json"):
#TODO: use proper structure
area_outline = json.load(open(filename))
area_outline_vertices = []
for point in area_outline:
area_outline_vertices.append(point)
area_polygon = Path(area_outline_vertices)
if filename.endswith(".shp"):
#TODO: not yet working
sf = shapefile.Reader(filename)
shapes = sf.shapes()
area_polygon = Path(sf.shape(0).points)
return area_polygon
def create_tasks(app_config, submit_tasks):
config = RawConfigParser()
config.read(args.task_config)
response = pbclient.find_app(short_name=app_config['short_name'])
app = response[0]
app_id = app.id
#polygon around area to be tasked, as list of (lat, long) lists
area_polygon = polygon_file_to_path(config.get("area", "polygon_file"))
extent = area_polygon.get_extents().get_points()
#The northern, southern, western, and eastern bounds of the area to work on.
nb = extent[1][0]
wb = extent[0][1]
sb = extent[0][0]
eb = extent[1][1]
shared_style = simplekml.Style()
shared_style.iconstyle.color = "ff0000ff"
shared_style.labelstyle.scale = 0.5
shared_style.iconstyle.scale = 0.5
kml = simplekml.Kml()
pnt = kml.newpoint(name="NW")
pnt.coords = [(wb, nb)]
pnt.style = shared_style
pnt = kml.newpoint(name="NE")
pnt.coords = [(eb, nb)]
pnt.style = shared_style
pnt = kml.newpoint(name="SW")
pnt.coords = [(wb, sb)]
pnt.style = shared_style
pnt = kml.newpoint(name="SE")
pnt.coords = [(eb, sb)]
pnt.style = shared_style
#Size of the tasks, into how many rows and columns should the area be divided.
task_cols = int(config.get("tasksize", "task_cols"))
task_rows = int(config.get("tasksize", "task_rows"))
boundary = float(config.get("tasksize", "boundary"))
ns_step = (sb - nb) / task_rows
ns_boundary = ns_step * boundary
we_step = (eb - wb) / task_cols
we_boundary = we_step * boundary
task_counter = 0
if submit_tasks:
res = requests.get(args.server + '/api/app')
remaining_requests = int(res.headers['x-ratelimit-remaining'])
print("Remaining requests: " + str(remaining_requests))
else:
remaining_requests = 99999
for col in range(task_cols):
wbr = wb + col * we_step
ebr = wb + (col + 1) * we_step
for row in range(task_rows):
while remaining_requests < 10:
time.sleep(60)
res = requests.get(args.server + '/api/app')
remaining_requests = int(res.headers['x-ratelimit-remaining'])
print(remaining_requests)
nbc = nb + row * ns_step
sbc = nb + (row + 1) * ns_step
if area_polygon.intersects_bbox(Bbox([[nbc, wbr], [sbc, ebr]])):
if submit_tasks:
task_info = dict(question=app_config['question'], n_answers=config.get("meta", "n_answers"),
westbound=wbr, eastbound=ebr, northbound=nbc, southbound=sbc,
westmapbound=wbr - we_boundary, eastmapbound=ebr + we_boundary,
northmapbound=nbc - ns_boundary, southmapbound=sbc + ns_boundary,
location=str(row) + "_" + str(col), batch=config.get("meta", "batch_name"))
response = pbclient.create_task(app_id, task_info)
check_api_error(response)
remaining_requests -= 1
pnt = kml.newpoint(name=str(task_counter))
pnt.coords = [(wbr, nbc)]
pnt.style = shared_style
pnt = kml.newpoint(name=str(task_counter))
pnt.coords = [(ebr, nbc)]
pnt.style = shared_style
pnt = kml.newpoint(name=str(task_counter))
pnt.coords = [(wbr, sbc)]
pnt.style = shared_style
pnt = kml.newpoint(name=str(task_counter))
pnt.coords = [(ebr, sbc)]
pnt.style = shared_style
task_counter += 1
print("Task: " + str(task_counter))
kml.save(config.get("meta", "batch_name") + "_tasks.kml")
if __name__ == "__main__":
# Arguments for the application
usage = "usage: %prog [options]"
parser = ArgumentParser(usage)
# only write corners of tasks to kml and exit
parser.add_argument("-d", "--dry", help="Write kml and exit", action="store_true")
# URL where PyBossa listens
parser.add_argument("-s", "--server", help="PyBossa URL http://domain.com/", required=True)
# API-KEY
parser.add_argument("-k", "--api-key", help="PyBossa User API-KEY to interact with PyBossa", required=True)
# Create App
parser.add_argument("-c", "--create-app", action="store_true", help="Create the application")
# Create tasks, using a polygon for the area
parser.add_argument("-t", "--task-config", help="File which contains configuration for task creation.")
# Update template for tasks and long_description for app
parser.add_argument("-u", "--update-template", action="store_true", help="Update Tasks template")
# Update tasks
parser.add_argument("-q", "--update-tasks", help="Update tasks", action="store_true")
# Verbose?
parser.add_argument("-v", "--verbose", action="store_true")
args = parser.parse_args()
app_config = None
# Load app details
try:
with file('app.json') as app_json:
app_config = json.load(app_json)
except IOError:
print "application config file is missing! Please create a new one"
exit(1)
pbclient.set('endpoint', args.server)
pbclient.set('api_key', args.api_key)
if args.verbose:
print('Running against PyBosssa instance at: %s' % args.server)
print('Using API-KEY: %s' % args.api_key)
if args.dry:
create_tasks(app_config, False)
exit(0)
if args.create_app:
try:
response = pbclient.create_app(app_config['name'],
app_config['short_name'],
app_config['description'])
check_api_error(response)
response = pbclient.find_app(short_name=app_config['short_name'])
check_api_error(response)
app = response[0]
app.long_description = open('long_description.html').read()
app.info['task_presenter'] = open('template.html').read()
app.info['thumbnail'] = app_config['thumbnail']
app.info['tutorial'] = open('tutorial.html').read()
except:
format_error("pbclient.create_app or pbclient.find_app", response)
try:
response = pbclient.update_app(app)
check_api_error(response)
except:
format_error("pbclient.update_app", response)
if args.task_config:
create_tasks(app_config, True)
if args.update_template:
print "Updating app template"
try:
response = pbclient.find_app(short_name=app_config['short_name'])
check_api_error(response)
app = response[0]
app.long_description = open('long_description.html').read()
app.info['task_presenter'] = open('template.html').read()
app.info['tutorial'] = open('tutorial.html').read()
response = pbclient.update_app(app)
check_api_error(response)
except:
format_error("pbclient.find_app or pbclient.update_app", "")
if args.update_tasks:
print "Updating task question"
try:
response = pbclient.find_app(short_name=app_config['short_name'])
check_api_error(response)
app = response[0]
n_tasks = 0
offset = 0
limit = 100
except:
format_error("pbclient.find_app", response)
try:
tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit)
check_api_error(tasks)
except:
format_error("pbclient.get_tasks", tasks)
while tasks:
for task in tasks:
print "Updating task: %s" % task.id
if ('n_answers' in task.info.keys()):
del(task.info['n_answers'])
task.n_answers = int(args.number_answers)
try:
response = pbclient.update_task(task)
check_api_error(response)
n_tasks += 1
except:
format_error("pbclient.update_task", response)
offset = (offset + limit)
try:
tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit)
check_api_error(tasks)
except:
format_error("pbclient.get_tasks", tasks)
print "%s Tasks have been updated!" % n_tasks