|
| 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}') |
0 commit comments