|
| 1 | +# Standard library imports |
| 2 | +import os |
| 3 | + |
| 4 | +# Third party imports |
| 5 | +import opengeode_geosciences as og_gs |
| 6 | +import opengeode as og |
| 7 | +import werkzeug |
| 8 | +import flask |
| 9 | + |
| 10 | +# Local application imports |
| 11 | +from .geode_objects import geode_objects_dict |
| 12 | +from . import utils_functions |
| 13 | + |
| 14 | + |
| 15 | +def geode_object_value(geode_object: str): |
| 16 | + return geode_objects_dict()[geode_object] |
| 17 | + |
| 18 | + |
| 19 | +def geode_object_class(geode_object: str): |
| 20 | + return geode_object_value(geode_object)["class"] |
| 21 | + |
| 22 | + |
| 23 | +def input_factory(geode_object: str): |
| 24 | + return geode_object_value(geode_object)["input_factory"] |
| 25 | + |
| 26 | + |
| 27 | +def output_factory(geode_object: str): |
| 28 | + return geode_object_value(geode_object)["output_factory"] |
| 29 | + |
| 30 | + |
| 31 | +def additional_files(geode_object: str, file_absolute_path: str): |
| 32 | + return geode_object_value(geode_object)["additional_files"](file_absolute_path) |
| 33 | + |
| 34 | + |
| 35 | +def is_loadable(geode_object: str, file_absolute_path: str) -> float: |
| 36 | + percentage = geode_object_value(geode_object)["is_loadable"](file_absolute_path) |
| 37 | + return percentage.value() |
| 38 | + |
| 39 | + |
| 40 | +def object_priority(geode_object: str, file_absolute_path: str) -> int: |
| 41 | + return geode_object_value(geode_object)["object_priority"](file_absolute_path) |
| 42 | + |
| 43 | + |
| 44 | +def load(geode_object: str, file_absolute_path: str): |
| 45 | + return geode_object_value(geode_object)["load"](file_absolute_path) |
| 46 | + |
| 47 | + |
| 48 | +def data_file_path(data_id: str, filename: str) -> str: |
| 49 | + data_folder_path = flask.current_app.config["DATA_FOLDER_PATH"] |
| 50 | + return os.path.join( |
| 51 | + data_folder_path, |
| 52 | + data_id, |
| 53 | + werkzeug.utils.secure_filename(filename), |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def load_data(geode_object: str, data_id: str, filename: str): |
| 58 | + file_absolute_path = data_file_path(data_id, filename) |
| 59 | + return load(geode_object, file_absolute_path) |
| 60 | + |
| 61 | + |
| 62 | +def upload_file_path(filename): |
| 63 | + upload_folder = flask.current_app.config["UPLOAD_FOLDER"] |
| 64 | + secure_filename = werkzeug.utils.secure_filename(filename) |
| 65 | + return os.path.abspath(os.path.join(upload_folder, secure_filename)) |
| 66 | + |
| 67 | + |
| 68 | +def is_saveable(geode_object: str, data, filename: str): |
| 69 | + return geode_object_value(geode_object)["is_saveable"](data, filename) |
| 70 | + |
| 71 | + |
| 72 | +def save(geode_object: str, data, folder_absolute_path: str, filename: str): |
| 73 | + return geode_object_value(geode_object)["save"]( |
| 74 | + data, os.path.join(folder_absolute_path, filename) |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +def create_builder(geode_object: str, data): |
| 79 | + return geode_object_value(geode_object)["builder"](data) |
| 80 | + |
| 81 | + |
| 82 | +def assign_crs(geode_object: str, data, crs_name: str, info): |
| 83 | + builder = create_builder(geode_object, data) |
| 84 | + geode_object_value(geode_object)["crs"]["assign"](data, builder, crs_name, info) |
| 85 | + |
| 86 | + |
| 87 | +def convert_crs(geode_object: str, data, crs_name: str, info): |
| 88 | + builder = create_builder(geode_object, data) |
| 89 | + geode_object_value(geode_object)["crs"]["convert"](data, builder, crs_name, info) |
| 90 | + |
| 91 | + |
| 92 | +def create_crs( |
| 93 | + geode_object: str, |
| 94 | + data, |
| 95 | + name: str, |
| 96 | + input_coordiante_system, |
| 97 | + output_coordiante_system, |
| 98 | +): |
| 99 | + builder = create_builder(geode_object, data) |
| 100 | + geode_object_value(geode_object)["crs"]["create"]( |
| 101 | + data, builder, name, input_coordiante_system, output_coordiante_system |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +def get_object_type(geode_object: str): |
| 106 | + return geode_object_value(geode_object)["object_type"] |
| 107 | + |
| 108 | + |
| 109 | +def get_elements(geode_object: str): |
| 110 | + return geode_object_value(geode_object)["elements"] |
| 111 | + |
| 112 | + |
| 113 | +def is_3D(geode_object: str): |
| 114 | + return geode_object_value(geode_object)["is_3D"] |
| 115 | + |
| 116 | + |
| 117 | +def is_viewable(geode_object: str): |
| 118 | + return geode_object_value(geode_object)["is_viewable"] |
| 119 | + |
| 120 | + |
| 121 | +def inspect(geode_object: str, data): |
| 122 | + return geode_object_value(geode_object)["inspector"](data) |
| 123 | + |
| 124 | + |
| 125 | +def save_viewable(geode_object: str, data, folder_absolute_path: str, id: str): |
| 126 | + return geode_object_value(geode_object)["save_viewable"]( |
| 127 | + data, os.path.join(folder_absolute_path, id) |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | +def save_light_viewable(geode_object: str, data, folder_absolute_path: str, id: str): |
| 132 | + return geode_object_value(geode_object)["save_light_viewable"]( |
| 133 | + data, os.path.join(folder_absolute_path, id) |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +def geode_object_input_extensions(geode_object: str): |
| 138 | + geode_object_input_list_creators = input_factory(geode_object).list_creators() |
| 139 | + geode_object_input_list_creators.sort() |
| 140 | + return geode_object_input_list_creators |
| 141 | + |
| 142 | + |
| 143 | +def geode_object_output_extensions(geode_object: str): |
| 144 | + geode_object_output_list_creators = output_factory(geode_object).list_creators() |
| 145 | + geode_object_output_list_creators.sort() |
| 146 | + return geode_object_output_list_creators |
| 147 | + |
| 148 | + |
| 149 | +def filter_geode_objects(key: str = None): |
| 150 | + geode_objects_filtered_list = [] |
| 151 | + for geode_object, value in geode_objects_dict().items(): |
| 152 | + if key != None and key != "": |
| 153 | + if key in value: |
| 154 | + if type(value[key]) == bool: |
| 155 | + geode_objects_filtered_list.append(geode_object) |
| 156 | + else: |
| 157 | + geode_objects_filtered_list.append(geode_object) |
| 158 | + else: |
| 159 | + geode_objects_filtered_list.append(geode_object) |
| 160 | + geode_objects_filtered_list.sort() |
| 161 | + return geode_objects_filtered_list |
| 162 | + |
| 163 | + |
| 164 | +def list_input_extensions(key: str = None): |
| 165 | + extensions_list = [] |
| 166 | + geode_objects_filtered_list = filter_geode_objects(key) |
| 167 | + for geode_object in geode_objects_filtered_list: |
| 168 | + extensions_list += geode_object_input_extensions(geode_object) |
| 169 | + |
| 170 | + extensions_list = list(set(extensions_list)) |
| 171 | + extensions_list.sort() |
| 172 | + return extensions_list |
| 173 | + |
| 174 | + |
| 175 | +def has_creator(geode_object: str, extension: str): |
| 176 | + return input_factory(geode_object).has_creator(extension) |
| 177 | + |
| 178 | + |
| 179 | +def list_geode_objects( |
| 180 | + file_absolute_path: str, |
| 181 | + key: str = None, |
| 182 | +): |
| 183 | + return_dict = {} |
| 184 | + file_extension = utils_functions.extension_from_filename( |
| 185 | + os.path.basename(file_absolute_path) |
| 186 | + ) |
| 187 | + geode_objects_filtered_list = filter_geode_objects(key) |
| 188 | + for geode_object in geode_objects_filtered_list: |
| 189 | + if has_creator(geode_object, file_extension): |
| 190 | + loadability_score = is_loadable(geode_object, file_absolute_path) |
| 191 | + priority_score = object_priority(geode_object, file_absolute_path) |
| 192 | + return_dict[geode_object] = { |
| 193 | + "is_loadable": loadability_score, |
| 194 | + "object_priority": priority_score, |
| 195 | + } |
| 196 | + return return_dict |
| 197 | + |
| 198 | + |
| 199 | +def geode_objects_output_extensions(geode_object: str, data): |
| 200 | + geode_objects_output_extensions_dict = {} |
| 201 | + output_extensions = geode_object_output_extensions(geode_object) |
| 202 | + extensions_dict = {} |
| 203 | + for output_extension in output_extensions: |
| 204 | + bool_is_saveable = is_saveable(geode_object, data, f"test.{output_extension}") |
| 205 | + extensions_dict[output_extension] = {"is_saveable": bool_is_saveable} |
| 206 | + geode_objects_output_extensions_dict[geode_object] = extensions_dict |
| 207 | + |
| 208 | + if "parent" in geode_object_value(geode_object).keys(): |
| 209 | + parent_geode_object = geode_object_value(geode_object)["parent"] |
| 210 | + geode_objects_output_extensions_dict.update( |
| 211 | + geode_objects_output_extensions(parent_geode_object, data) |
| 212 | + ) |
| 213 | + return geode_objects_output_extensions_dict |
| 214 | + |
| 215 | + |
| 216 | +def get_inspector_children(obj): |
| 217 | + new_object = {} |
| 218 | + |
| 219 | + if "inspection_type" in dir(obj): |
| 220 | + new_object["title"] = obj.inspection_type() |
| 221 | + new_object["nb_issues"] = 0 |
| 222 | + new_object["children"] = [] |
| 223 | + for child in dir(obj): |
| 224 | + if not child.startswith("__") and not child in [ |
| 225 | + "inspection_type", |
| 226 | + "string", |
| 227 | + ]: |
| 228 | + child_instance = obj.__getattribute__(child) |
| 229 | + child_object = get_inspector_children(child_instance) |
| 230 | + new_object["children"].append(child_object) |
| 231 | + new_object["nb_issues"] += child_object["nb_issues"] |
| 232 | + else: |
| 233 | + new_object["title"] = obj.description() |
| 234 | + nb_issues = obj.nb_issues() |
| 235 | + new_object["nb_issues"] = nb_issues |
| 236 | + if nb_issues > 0: |
| 237 | + issues = obj.string().split("\n") |
| 238 | + new_object["issues"] = issues |
| 239 | + return new_object |
| 240 | + |
| 241 | + |
| 242 | +def geographic_coordinate_systems(geode_object: str): |
| 243 | + if is_3D(geode_object): |
| 244 | + return og_gs.GeographicCoordinateSystem3D.geographic_coordinate_systems() |
| 245 | + else: |
| 246 | + return og_gs.GeographicCoordinateSystem2D.geographic_coordinate_systems() |
| 247 | + |
| 248 | + |
| 249 | +def geographic_coordinate_systems_info(geode_object: str, crs): |
| 250 | + if is_3D(geode_object): |
| 251 | + return og_gs.GeographicCoordinateSystemInfo3D( |
| 252 | + crs["authority"], crs["code"], crs["name"] |
| 253 | + ) |
| 254 | + else: |
| 255 | + return og_gs.GeographicCoordinateSystemInfo2D( |
| 256 | + crs["authority"], crs["code"], crs["name"] |
| 257 | + ) |
| 258 | + |
| 259 | + |
| 260 | +def coordinate_system(geode_object: str, coordinate_system): |
| 261 | + return og.CoordinateSystem2D( |
| 262 | + [ |
| 263 | + og.Vector2D( |
| 264 | + og.Point2D( |
| 265 | + [coordinate_system["origin_x"], coordinate_system["origin_y"]] |
| 266 | + ), |
| 267 | + og.Point2D( |
| 268 | + [coordinate_system["point_1_x"], coordinate_system["point_1_y"]] |
| 269 | + ), |
| 270 | + ), |
| 271 | + og.Vector2D( |
| 272 | + og.Point2D( |
| 273 | + [coordinate_system["origin_x"], coordinate_system["origin_y"]] |
| 274 | + ), |
| 275 | + og.Point2D( |
| 276 | + [coordinate_system["point_2_x"], coordinate_system["point_2_y"]] |
| 277 | + ), |
| 278 | + ), |
| 279 | + ], |
| 280 | + og.Point2D([coordinate_system["origin_x"], coordinate_system["origin_y"]]), |
| 281 | + ) |
| 282 | + |
| 283 | + |
| 284 | +def assign_geographic_coordinate_system_info(geode_object: str, data, input_crs): |
| 285 | + info = geographic_coordinate_systems_info(geode_object, input_crs) |
| 286 | + assign_crs(geode_object, data, input_crs["name"], info) |
| 287 | + |
| 288 | + |
| 289 | +def convert_geographic_coordinate_system_info(geode_object: str, data, output_crs): |
| 290 | + info = geographic_coordinate_systems_info(geode_object, output_crs) |
| 291 | + convert_crs(geode_object, data, output_crs["name"], info) |
| 292 | + |
| 293 | + |
| 294 | +def create_coordinate_system( |
| 295 | + geode_object: str, data, name, input_coordinate_points, output_coordinate_points |
| 296 | +): |
| 297 | + input_coordiante_system = coordinate_system(geode_object, input_coordinate_points) |
| 298 | + output_coordiante_system = coordinate_system(geode_object, output_coordinate_points) |
| 299 | + create_crs( |
| 300 | + geode_object, data, name, input_coordiante_system, output_coordiante_system |
| 301 | + ) |
0 commit comments