|
| 1 | +# Standard library imports |
| 2 | +import json |
| 3 | +import os |
| 4 | +import uuid |
| 5 | + |
| 6 | +# Third party imports |
| 7 | +import flask |
| 8 | +import opengeode |
| 9 | +import werkzeug |
| 10 | + |
| 11 | +# Local application imports |
| 12 | +from opengeodeweb_back import geode_functions, utils_functions |
| 13 | +from opengeodeweb_microservice.database.data import Data |
| 14 | +from opengeodeweb_microservice.database.connection import get_session |
| 15 | +from opengeodeweb_back.utils_functions import save_all_viewables_and_return_info |
| 16 | + |
| 17 | +routes = flask.Blueprint("create", __name__, url_prefix="/opengeodeweb_back/create") |
| 18 | + |
| 19 | +schemas = os.path.join(os.path.dirname(__file__), "schemas") |
| 20 | + |
| 21 | +# Load schema for point creation |
| 22 | +with open(os.path.join(schemas, "create_point.json"), "r") as file: |
| 23 | + create_point_json = json.load(file) |
| 24 | + |
| 25 | +@routes.route( |
| 26 | + create_point_json["route"], |
| 27 | + methods=create_point_json["methods"] |
| 28 | +) |
| 29 | +def create_point(): |
| 30 | + """Endpoint to create a single point in 3D space.""" |
| 31 | + print(f"create_point : {flask.request=}", flush=True) |
| 32 | + |
| 33 | + utils_functions.validate_request(flask.request, create_point_json) |
| 34 | + |
| 35 | + # Extract data from request |
| 36 | + title = flask.request.json["title"] |
| 37 | + x = flask.request.json["x"] |
| 38 | + y = flask.request.json["y"] |
| 39 | + z = flask.request.json["z"] |
| 40 | + |
| 41 | + # Create the point set |
| 42 | + class_ = geode_functions.geode_object_class("PointSet3D") |
| 43 | + point_set = class_.create() |
| 44 | + builder = geode_functions.create_builder("PointSet3D", point_set) |
| 45 | + builder.create_point(opengeode.Point3D([x, y, z])) |
| 46 | + builder.set_name(title) |
| 47 | + |
| 48 | + # Save and get info |
| 49 | + result = save_all_viewables_and_return_info( |
| 50 | + geode_object="PointSet3D", |
| 51 | + data=point_set, |
| 52 | + input_file=None, |
| 53 | + additional_files=[] |
| 54 | + ) |
| 55 | + |
| 56 | + # Prepare response with the title |
| 57 | + response = { |
| 58 | + "viewable_file_name": result["viewable_file_name"], |
| 59 | + "id": result["id"], |
| 60 | + "name": title, |
| 61 | + "native_file_name": result["native_file_name"], |
| 62 | + "object_type": result["object_type"], |
| 63 | + "geode_object": result["geode_object"], |
| 64 | + } |
| 65 | + |
| 66 | + # Add binary_light_viewable if it exists |
| 67 | + if "binary_light_viewable" in result: |
| 68 | + response["binary_light_viewable"] = result["binary_light_viewable"] |
| 69 | + |
| 70 | + return flask.make_response(response, 200) |
| 71 | + |
| 72 | + |
| 73 | +# Load schema for AOI creation |
| 74 | +with open(os.path.join(schemas, "create_aoi.json"), "r") as file: |
| 75 | + create_aoi_json = json.load(file) |
| 76 | + |
| 77 | +@routes.route( |
| 78 | + create_aoi_json["route"], |
| 79 | + methods=create_aoi_json["methods"] |
| 80 | +) |
| 81 | +def create_aoi(): |
| 82 | + """Endpoint to create an Area of Interest (AOI) as an EdgedCurve3D.""" |
| 83 | + print(f"create_aoi : {flask.request=}", flush=True) |
| 84 | + |
| 85 | + utils_functions.validate_request(flask.request, create_aoi_json) |
| 86 | + |
| 87 | + # Extract data from request |
| 88 | + name = flask.request.json["name"] |
| 89 | + points = flask.request.json["points"] |
| 90 | + z = flask.request.json["z"] |
| 91 | + |
| 92 | + # Create the edged curve |
| 93 | + class_ = geode_functions.geode_object_class("EdgedCurve3D") |
| 94 | + edged_curve = class_.create() |
| 95 | + builder = geode_functions.create_builder("EdgedCurve3D", edged_curve) |
| 96 | + builder.set_name(name) |
| 97 | + |
| 98 | + # Create vertices first |
| 99 | + vertex_indices = [] |
| 100 | + for point in points: |
| 101 | + vertex_id = builder.create_point(opengeode.Point3D([point["x"], point["y"], z])) |
| 102 | + vertex_indices.append(vertex_id) |
| 103 | + |
| 104 | + # Create edges between consecutive vertices and close the loop |
| 105 | + num_vertices = len(vertex_indices) |
| 106 | + for i in range(num_vertices): |
| 107 | + edge_id = builder.create_edge() |
| 108 | + next_i = (i + 1) % num_vertices # Wrap around to close the loop |
| 109 | + builder.set_edge_vertex( |
| 110 | + opengeode.EdgeVertex(edge_id, 0), vertex_indices[i] |
| 111 | + ) |
| 112 | + builder.set_edge_vertex( |
| 113 | + opengeode.EdgeVertex(edge_id, 1), vertex_indices[next_i] |
| 114 | + ) |
| 115 | + |
| 116 | + # Save and get info |
| 117 | + result = save_all_viewables_and_return_info( |
| 118 | + geode_object="EdgedCurve3D", |
| 119 | + data=edged_curve, |
| 120 | + input_file=None, |
| 121 | + additional_files=[] |
| 122 | + ) |
| 123 | + |
| 124 | + # Prepare response |
| 125 | + response = { |
| 126 | + "viewable_file_name": result["viewable_file_name"], |
| 127 | + "id": result["id"], |
| 128 | + "name": name, |
| 129 | + "native_file_name": result["native_file_name"], |
| 130 | + "object_type": result["object_type"], |
| 131 | + "geode_object": result["geode_object"], |
| 132 | + } |
| 133 | + |
| 134 | + # Add binary_light_viewable if it exists |
| 135 | + if "binary_light_viewable" in result: |
| 136 | + response["binary_light_viewable"] = result["binary_light_viewable"] |
| 137 | + |
| 138 | + return flask.make_response(response, 200) |
0 commit comments