Skip to content

Commit dc0a766

Browse files
committed
work on tutorials and clean up #185
1 parent 1c8f8d0 commit dc0a766

40 files changed

+272
-2117
lines changed

mapswipe_workers/mapswipe_workers/base/base_functions.py

Lines changed: 0 additions & 690 deletions
This file was deleted.

mapswipe_workers/mapswipe_workers/mapswipe_workers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import FootprintProject
2121
from mapswipe_workers.project_types.change_detection.change_detection_project \
2222
import ChangeDetectionProject
23+
from mapswipe_workers.project_types.change_detection import change_detection_tutorial
2324
from mapswipe_workers.utils import user_management
2425

2526
@click.group()
@@ -229,7 +230,8 @@ def run_create_tutorial(input_file):
229230

230231
project_types_tutorial = {
231232
# Make sure to import all project types here
232-
1: build_area_tutorial.create_tutorial
233+
1: build_area_tutorial.create_tutorial,
234+
3: change_detection_tutorial.create_tutorial
233235
}
234236

235237
project_types_tutorial[project_type](tutorial)
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import csv
2+
3+
from mapswipe_workers.definitions import logger
4+
from mapswipe_workers import auth
5+
from mapswipe_workers.project_types.build_area import tile_functions as t
6+
7+
8+
def create_tutorial(tutorial):
9+
tutorial, groups_dict, tasks_dict = generate_tutorial_data(tutorial)
10+
upload_tutorial_to_firebase(tutorial, groups_dict, tasks_dict)
11+
12+
13+
def generate_tutorial_data(tutorial):
14+
tutorial_id = tutorial['projectId']
15+
categories = tutorial['categories'].keys()
16+
logger.info(f'create tutorial for tutorial id: {tutorial_id}')
17+
18+
grouped_tasks = {
19+
'building_easy_1': {},
20+
'building_easy_2': {},
21+
'no_building_easy_1': {},
22+
'no_building_easy_2': {},
23+
'bad_clouds_1': {},
24+
'bad_clouds_2': {},
25+
'bad_no_imagery_1': {},
26+
'bad_no_imagery_2': {},
27+
}
28+
29+
c = 0
30+
with open(tutorial['examplesFile']) as csvDataFile:
31+
logger.info(f'tutorial tasks are based on: {tutorial["examplesFile"]}')
32+
csvReader = csv.reader(csvDataFile)
33+
for row in csvReader:
34+
c += 1
35+
if c == 1:
36+
header = row
37+
else:
38+
category = row[2]
39+
if not row[3] in grouped_tasks[category].keys():
40+
grouped_tasks[category][row[3]] = {
41+
'task_id_list': [],
42+
'task_x_list': [],
43+
'task_y_list': [],
44+
'reference_answer_list': [],
45+
'category': category,
46+
'url_list': []
47+
}
48+
49+
grouped_tasks[category][row[3]]['task_id_list'].append(row[0])
50+
zoom = row[0].split('-')[0]
51+
task_x = int(row[0].split('-')[1])
52+
task_y = int(row[0].split('-')[2])
53+
grouped_tasks[category][row[3]]['task_x_list'].append(task_x)
54+
grouped_tasks[category][row[3]]['task_y_list'].append(task_y)
55+
url = t.tile_coords_zoom_and_tileserver_to_URL(task_x, task_y, zoom, tutorial['tileServer']['name'],
56+
auth.get_api_key(tutorial['tileServer']['name']), None,
57+
None)
58+
grouped_tasks[category][row[3]]['url_list'].append(url)
59+
grouped_tasks[category][row[3]]['reference_answer_list'].append(row[1])
60+
61+
groups_dict = {}
62+
tasks_dict = {}
63+
64+
for i in range(0, 1):
65+
print(i)
66+
group_id = 101 + i
67+
groups_dict[group_id] = {
68+
"xMax": "115",
69+
"xMin": "100",
70+
"yMax": "202",
71+
"yMin": "200",
72+
"requiredCount": 5,
73+
"finishedCount": 0,
74+
"groupId": group_id,
75+
"projectId": tutorial_id,
76+
"numberOfTasks": 36,
77+
"progress": 0
78+
}
79+
80+
tasks_dict[group_id] = {}
81+
# select 6 tasks for each category and add to group
82+
counter = -1
83+
task_x = 100
84+
task_y = 200
85+
for category in categories:
86+
# print(category)
87+
keys = list(grouped_tasks[category].keys())
88+
tasks = grouped_tasks[category][keys[i]]
89+
90+
x_min = min(tasks['task_x_list'])
91+
y_min = min(tasks['task_y_list'])
92+
93+
# replace taskX and TaskY
94+
# TaskY between 0 and 2
95+
# TaskX between 0 and 11
96+
for j in range(0, len(tasks['task_id_list'])):
97+
counter += 1
98+
task = {
99+
'taskId_real': tasks['task_id_list'][j],
100+
'groupId': group_id,
101+
'projectId': tutorial_id,
102+
'referenceAnswer': tasks['reference_answer_list'][j],
103+
'category': tasks['category'],
104+
'url': tasks['url_list'][j]
105+
}
106+
107+
if tasks['task_x_list'][j] == x_min:
108+
task['taskX'] = str(task_x)
109+
elif tasks['task_x_list'][j] == (x_min + 1):
110+
task['taskX'] = str(task_x + 1)
111+
112+
if tasks['task_y_list'][j] == y_min:
113+
task['taskY'] = str(task_y)
114+
elif tasks['task_y_list'][j] == (y_min + 1):
115+
task['taskY'] = str(task_y + 1)
116+
elif tasks['task_y_list'][j] == (y_min + 2):
117+
task['taskY'] = str(task_y + 2)
118+
119+
task['taskId'] = '18-{}-{}'.format(task['taskX'], task['taskY'])
120+
tasks_dict[group_id][counter] = task
121+
122+
task_x += 2
123+
124+
return tutorial, groups_dict, tasks_dict
125+
126+
127+
def upload_tutorial_to_firebase(tutorial, groups_dict, tasks_dict):
128+
# upload groups and tasks to firebase
129+
tutorial_id = tutorial['projectId']
130+
131+
fb_db = auth.firebaseDB()
132+
ref = fb_db.reference('')
133+
ref.update({
134+
'v2/projects/{}'.format(tutorial_id): tutorial,
135+
'v2/groups/{}'.format(tutorial_id): groups_dict,
136+
'v2/tasks/{}'.format(tutorial_id): tasks_dict,
137+
})
138+
logger.info(f'uploaded tutorial data to firebase for {tutorial_id}')
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import csv
2+
import json
3+
4+
from mapswipe_workers.definitions import logger
5+
from mapswipe_workers import auth
6+
from mapswipe_workers.project_types.change_detection import tile_functions as t
7+
8+
9+
def create_tutorial(tutorial):
10+
tutorial, groups_dict, tasks_dict = generate_tutorial_data(tutorial)
11+
upload_tutorial_to_firebase(tutorial, groups_dict, tasks_dict)
12+
13+
14+
def generate_tutorial_data(tutorial):
15+
tutorial_id = tutorial['projectId']
16+
logger.info(f'create tutorial for tutorial id: {tutorial_id}')
17+
18+
groups_dict = {}
19+
tasks_dict = {}
20+
21+
with open(tutorial['examplesFile']) as json_file:
22+
tutorial_tasks = json.load(json_file)
23+
print(len(tutorial_tasks))
24+
print(tutorial_tasks)
25+
26+
for feature in tutorial_tasks['features']:
27+
28+
print(feature)
29+
30+
category = feature['properties']['category']
31+
group_id = 100 + feature['properties']['id']
32+
33+
if not group_id in groups_dict.keys():
34+
groups_dict[group_id] = {
35+
"xMax": "104",
36+
"xMin": "100",
37+
"yMax": "200",
38+
"yMin": "200",
39+
"requiredCount": 5,
40+
"finishedCount": 0,
41+
"groupId": group_id,
42+
"projectId": tutorial_id,
43+
"numberOfTasks": 4,
44+
"progress": 0
45+
}
46+
47+
reference = feature['properties']['reference']
48+
zoom = feature['properties']['TileZ']
49+
task_x = feature['properties']['TileX']
50+
task_y = feature['properties']['TileY']
51+
task_id_real = '{}-{}-{}'.format(zoom, task_x, task_y)
52+
53+
urlA = t.tile_coords_zoom_and_tileserver_to_URL(
54+
task_x,
55+
task_y,
56+
zoom,
57+
tutorial['tileServerA']['name'],
58+
tutorial['tileServerA']['apiKey'],
59+
tutorial['tileServerA']['url'],
60+
None,
61+
)
62+
urlB = t.tile_coords_zoom_and_tileserver_to_URL(
63+
task_x,
64+
task_y,
65+
zoom,
66+
tutorial['tileServerB']['name'],
67+
tutorial['tileServerB']['apiKey'],
68+
tutorial['tileServerB']['url'],
69+
None,
70+
)
71+
72+
if not group_id in tasks_dict.keys():
73+
tasks_dict[group_id] = {}
74+
task_id = '{}-{}-{}'.format(16, 100, 200)
75+
else:
76+
task_id = '{}-{}-{}'.format(16, 100 + len(tasks_dict[group_id].keys()), 200)
77+
78+
task = {
79+
'taskId_real': task_id_real,
80+
'taskId': task_id,
81+
'taskX': 100 + len(tasks_dict[group_id].keys()),
82+
'taskY': 200,
83+
'groupId': group_id,
84+
'projectId': tutorial_id,
85+
'referenceAnswer': reference,
86+
'category': category,
87+
'urlA': urlA,
88+
'urlB': urlB,
89+
}
90+
91+
tasks_dict[group_id][len(tasks_dict[group_id].keys())] = task
92+
93+
return tutorial, groups_dict, tasks_dict
94+
95+
96+
def upload_tutorial_to_firebase(tutorial, groups_dict, tasks_dict):
97+
# upload groups and tasks to firebase
98+
tutorial_id = tutorial['projectId']
99+
100+
fb_db = auth.firebaseDB()
101+
ref = fb_db.reference('')
102+
ref.update({
103+
'v2/projects/{}'.format(tutorial_id): tutorial,
104+
'v2/groups/{}'.format(tutorial_id): groups_dict,
105+
'v2/tasks/{}'.format(tutorial_id): tasks_dict,
106+
})
107+
logger.info(f'uploaded tutorial data to firebase for {tutorial_id}')

0 commit comments

Comments
 (0)