|
| 1 | +import math |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import Dict, List |
| 4 | + |
| 5 | +from mapswipe_workers.definitions import logger |
| 6 | +from mapswipe_workers.firebase.firebase import Firebase |
| 7 | +from mapswipe_workers.firebase_to_postgres.transfer_results import ( |
| 8 | + results_to_file, |
| 9 | + save_results_to_postgres, |
| 10 | + truncate_temp_results, |
| 11 | +) |
| 12 | +from mapswipe_workers.generate_stats.project_stats import ( |
| 13 | + get_statistics_for_integer_result_project, |
| 14 | +) |
| 15 | +from mapswipe_workers.project_types.project import BaseGroup, BaseProject, BaseTask |
| 16 | +from mapswipe_workers.utils.process_mapillary import get_image_metadata |
| 17 | +from mapswipe_workers.utils.validate_input import ( |
| 18 | + build_multipolygon_from_layer_geometries, |
| 19 | + check_if_layer_has_too_many_geometries, |
| 20 | + check_if_layer_is_empty, |
| 21 | + load_geojson_to_ogr, |
| 22 | + multipolygon_to_wkt, |
| 23 | + save_geojson_to_file, |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +@dataclass |
| 28 | +class StreetGroup(BaseGroup): |
| 29 | + # todo: does client use this, or only for the implementation of project creation? |
| 30 | + pass |
| 31 | + |
| 32 | + |
| 33 | +@dataclass |
| 34 | +class StreetTask(BaseTask): |
| 35 | + geometry: str |
| 36 | + |
| 37 | + |
| 38 | +class StreetProject(BaseProject): |
| 39 | + def __init__(self, project_draft): |
| 40 | + super().__init__(project_draft) |
| 41 | + self.groups: Dict[str, StreetGroup] = {} |
| 42 | + self.tasks: Dict[str, List[StreetTask]] = {} |
| 43 | + |
| 44 | + self.geometry = project_draft["geometry"] |
| 45 | + |
| 46 | + # TODO: validate inputs |
| 47 | + ImageMetadata = get_image_metadata( |
| 48 | + self.geometry, |
| 49 | + is_pano=project_draft.get("isPano", None), |
| 50 | + start_time=project_draft.get("startTimestamp", None), |
| 51 | + end_time=project_draft.get("endTimestamp", None), |
| 52 | + organization_id=project_draft.get("organizationId", None), |
| 53 | + sampling_threshold=project_draft.get("samplingThreshold", None), |
| 54 | + ) |
| 55 | + |
| 56 | + self.imageIds = ImageMetadata["ids"] |
| 57 | + self.imageGeometries = ImageMetadata["geometries"] |
| 58 | + |
| 59 | + def save_tasks_to_firebase(self, projectId: str, tasks: dict): |
| 60 | + firebase = Firebase() |
| 61 | + firebase.save_tasks_to_firebase(projectId, tasks, useCompression=False) |
| 62 | + |
| 63 | + @staticmethod |
| 64 | + def results_to_postgres(results: dict, project_id: str, filter_mode: bool): |
| 65 | + """How to move the result data from firebase to postgres.""" |
| 66 | + results_file, user_group_results_file = results_to_file(results, project_id) |
| 67 | + truncate_temp_results() |
| 68 | + save_results_to_postgres(results_file, project_id, filter_mode) |
| 69 | + return user_group_results_file |
| 70 | + |
| 71 | + @staticmethod |
| 72 | + def get_per_project_statistics(project_id, project_info): |
| 73 | + """How to aggregate the project results.""" |
| 74 | + return get_statistics_for_integer_result_project( |
| 75 | + project_id, project_info, generate_hot_tm_geometries=False |
| 76 | + ) |
| 77 | + |
| 78 | + def validate_geometries(self): |
| 79 | + self.inputGeometriesFileName = save_geojson_to_file( |
| 80 | + self.projectId, self.geometry |
| 81 | + ) |
| 82 | + layer, datasource = load_geojson_to_ogr( |
| 83 | + self.projectId, self.inputGeometriesFileName |
| 84 | + ) |
| 85 | + |
| 86 | + # check if inputs fit constraints |
| 87 | + check_if_layer_is_empty(self.projectId, layer) |
| 88 | + |
| 89 | + multi_polygon, project_area = build_multipolygon_from_layer_geometries( |
| 90 | + self.projectId, layer |
| 91 | + ) |
| 92 | + |
| 93 | + check_if_layer_has_too_many_geometries(self.projectId, multi_polygon) |
| 94 | + |
| 95 | + del datasource |
| 96 | + del layer |
| 97 | + |
| 98 | + logger.info( |
| 99 | + f"{self.projectId}" f" - validate geometry - " f"input geometry is correct." |
| 100 | + ) |
| 101 | + wkt_geometry = multipolygon_to_wkt(multi_polygon) |
| 102 | + return wkt_geometry |
| 103 | + |
| 104 | + def create_groups(self): |
| 105 | + self.numberOfGroups = math.ceil(len(self.imageIds) / self.groupSize) |
| 106 | + for group_id in range(self.numberOfGroups): |
| 107 | + self.groups[f"g{group_id}"] = StreetGroup( |
| 108 | + projectId=self.projectId, |
| 109 | + groupId=f"g{group_id}", |
| 110 | + progress=0, |
| 111 | + finishedCount=0, |
| 112 | + requiredCount=0, |
| 113 | + numberOfTasks=self.groupSize, |
| 114 | + ) |
| 115 | + |
| 116 | + def create_tasks(self): |
| 117 | + if len(self.groups) == 0: |
| 118 | + raise ValueError("Groups needs to be created before tasks can be created.") |
| 119 | + for group_id, group in self.groups.items(): |
| 120 | + self.tasks[group_id] = [] |
| 121 | + for i in range(self.groupSize): |
| 122 | + task = StreetTask( |
| 123 | + projectId=self.projectId, |
| 124 | + groupId=group_id, |
| 125 | + geometry=self.imageGeometries.pop(), |
| 126 | + taskId=self.imageIds.pop(), |
| 127 | + ) |
| 128 | + self.tasks[group_id].append(task) |
| 129 | + |
| 130 | + # list now empty? if usual group size is not reached |
| 131 | + # the actual number of tasks for the group is updated |
| 132 | + if not self.imageIds: |
| 133 | + group.numberOfTasks = i + 1 |
| 134 | + break |
0 commit comments