diff --git a/.coverage b/.coverage index d06917897e5641380540d8f2477af9c3b378a3b8..8f84519813e6b48a536cf38897631b201ff12d6a 100644 Binary files a/.coverage and b/.coverage differ diff --git a/README.md b/README.md index 24128d70d40816a6c8db108a217a8fcd906ae7f4..f52b2706c6a3d45859eccb66a398063cbb3152d5 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,7 @@ This endpoint is only for production, and is not implemented in the application. ### New lidar data ``` -Method: GET +Method: POST Path: new_lidar_data?lake=* Paramters: - lake (required) @@ -196,6 +196,18 @@ The endpoint will update the current data from ```*_lidar_data.json```, with the content from folder ```./server/lidar_data/*```. Should be used after the ```./server/lidar_data/*``` has been updated. +### adding new measurement position in a body of water +``` +Method: POST +Path: add_new_lidar_measurement?lake=*&latitude=*&longitude=* +Paramters: + - lake (required) + - latitude (required) + - longitude (required) +``` +The endpoint will add a new measurement positions in a body of water. By +adding a new measurement to ```*_lidar_data.json``` file. + ## Database This project requires SQLite3. Download the precompiled binary for your operating system. Precompiled binaries can be found on https://www.sqlite.org/download.html. Extract the downloaded diff --git a/server/__pycache__/scheduler.cpython-39.pyc b/server/__pycache__/scheduler.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6adadb54cfd0b9d4ce6cd4c4b949b8bc6b3db9e0 Binary files /dev/null and b/server/__pycache__/scheduler.cpython-39.pyc differ diff --git a/server/data_processing/__pycache__/add_new_lidar_measurement.cpython-39.pyc b/server/data_processing/__pycache__/add_new_lidar_measurement.cpython-39.pyc index dad8079e88a289d04670f9d108d10184a82da143..ee8da4fc4cea654ec78f148b78faa6642a218b9d 100644 Binary files a/server/data_processing/__pycache__/add_new_lidar_measurement.cpython-39.pyc and b/server/data_processing/__pycache__/add_new_lidar_measurement.cpython-39.pyc differ diff --git a/server/data_processing/add_new_lidar_measurement.py b/server/data_processing/add_new_lidar_measurement.py index 166a896c7b259a41a3007fe22bf377030097f462..09c0f290318565a80360d07db45a5bc02a63e723 100644 --- a/server/data_processing/add_new_lidar_measurement.py +++ b/server/data_processing/add_new_lidar_measurement.py @@ -47,7 +47,7 @@ def add_new_lidar_measurement(lake_name: str, lat: float, lon: float): # create an empty container for new measurement new_measurement_data = { - 'MeasurementID': len(measurement_data) - 1, + 'MeasurementID': len(measurement_data), 'TimeMeasured': str(time_now), 'CenterLat': lat, 'CenterLon': lon, @@ -59,7 +59,10 @@ def add_new_lidar_measurement(lake_name: str, lat: float, lon: float): 'Subdivisions': [], } - measurement_data.append(new_measurement_data) + if not measurement_data: + measurement_data = new_measurement_data + else: + measurement_data.append(new_measurement_data) # convert data to json format content = json.dumps(measurement_data, indent=4) @@ -80,6 +83,10 @@ def add_new_lidar_measurement(lake_name: str, lat: float, lon: float): } content = json.dumps(new_measurement_data, indent=4) + # remove existing file + if os.path.exists(file_path): + os.remove(file_path) + # write to file with open(file_path, "w") as file: file.write(content) @@ -89,7 +96,7 @@ def add_new_lidar_measurement(lake_name: str, lat: float, lon: float): # error handling except Exception as e: - return 500, f"An error occurred: {e} g".encode("utf-8") + return 500, f"An error occurred: {e}".encode("utf-8") -print(add_new_lidar_measurement("mjøsa", 60.9, 10.9)) \ No newline at end of file +#print(add_new_lidar_measurement("mjøsa", 60.9, 10.9)) \ No newline at end of file diff --git a/server/data_processing/unit_tests/__pycache__/test_add_new_lidar_measurement.cpython-39-pytest-7.1.2.pyc b/server/data_processing/unit_tests/__pycache__/test_add_new_lidar_measurement.cpython-39-pytest-7.1.2.pyc index 1c31f05f9ebcece4830641353883b360d925b792..7e6274e8a928f4ee2ffbd468e95f95fc35e78608 100644 Binary files a/server/data_processing/unit_tests/__pycache__/test_add_new_lidar_measurement.cpython-39-pytest-7.1.2.pyc and b/server/data_processing/unit_tests/__pycache__/test_add_new_lidar_measurement.cpython-39-pytest-7.1.2.pyc differ diff --git a/server/data_processing/unit_tests/__pycache__/test_input_new_data.cpython-39-pytest-7.1.2.pyc b/server/data_processing/unit_tests/__pycache__/test_input_new_data.cpython-39-pytest-7.1.2.pyc index 279b93a3e9baf699a8abb16b934f7ae92a575cdb..5a59c60c9e3d39232d707bdb849cda1b0662ee6b 100644 Binary files a/server/data_processing/unit_tests/__pycache__/test_input_new_data.cpython-39-pytest-7.1.2.pyc and b/server/data_processing/unit_tests/__pycache__/test_input_new_data.cpython-39-pytest-7.1.2.pyc differ diff --git a/server/data_processing/unit_tests/__pycache__/test_process_lidar_data.cpython-39-pytest-7.1.2.pyc b/server/data_processing/unit_tests/__pycache__/test_process_lidar_data.cpython-39-pytest-7.1.2.pyc index 4d286464ec758a12fb05f87a060a9983fe992b85..5dfcb06993571753922d7c912bb5320171e58fd1 100644 Binary files a/server/data_processing/unit_tests/__pycache__/test_process_lidar_data.cpython-39-pytest-7.1.2.pyc and b/server/data_processing/unit_tests/__pycache__/test_process_lidar_data.cpython-39-pytest-7.1.2.pyc differ diff --git a/server/data_processing/unit_tests/test_add_new_lidar_measurement.py b/server/data_processing/unit_tests/test_add_new_lidar_measurement.py index 987ffe5b96663b72233889fbbd9a7160bda8ab9c..c4c7e7f1445c4920b79762a7e26d5df69832b52c 100644 --- a/server/data_processing/unit_tests/test_add_new_lidar_measurement.py +++ b/server/data_processing/unit_tests/test_add_new_lidar_measurement.py @@ -1,4 +1,7 @@ +import os + from server.data_processing.add_new_lidar_measurement import add_new_lidar_measurement +from server.consts import LAKE_RELATIONS_PATH, LIDAR_DATA_PATH def test_add_new_lidar_measurement_invalid_lake() -> None: lake_name = 'test_lake' @@ -9,10 +12,30 @@ def test_add_new_lidar_measurement_invalid_lake() -> None: assert actual == expected def test_add_new_lidar_measurement_valid_lake() -> None: - lake_name = 'mjøsa' + # parameters for test + lake_name = 'fake_for_test' lat = 0.0 lon = 0.0 - actual, _ = add_new_lidar_measurement(lake_name, lat, lon) + + # create temporary test file and directory + file_path = os.path.join(LAKE_RELATIONS_PATH, f"{lake_name}_lidar_data.json") + dir_path = os.path.join(LIDAR_DATA_PATH + lake_name) + if not os.path.exists(file_path): + with open(file_path, "w") as file: + file.write("{}") + if not os.path.exists(dir_path): + os.makedirs(dir_path) + + # expectations + actual, u = add_new_lidar_measurement(lake_name, lat, lon) expected = 200 + print("sub", u) + # remove temporary files and directories + #if os.path.exists(file_path): + # os.remove(file_path) + if os.path.isdir(dir_path): + os.rmdir(dir_path) + + # result assert actual == expected diff --git a/server/main.py b/server/main.py index 2830a85e5baaef69b086e2a75f812221b1f461f5..41a4de9f93af7562694c76f08c44ddfa2d92001b 100644 --- a/server/main.py +++ b/server/main.py @@ -11,6 +11,7 @@ from server.scheduler import update_scheduler from server.consts import LAKE_RELATIONS_PATH from map_handler.get_lake_relation import get_map_data_handler from data_processing.input_new_data import input_new_Lidar_data +from data_processing.add_new_lidar_measurement import add_new_lidar_measurement from map_handler.update_measurements import update_measurements_handler, add_test_data app = Flask(__name__) @@ -126,7 +127,8 @@ class IceHTTP(BaseHTTPRequestHandler): addTestData(self, lake_name) - elif self.path.startswith('/new_lidar_data'): + def do_POST(self): + if self.path.startswith('/new_lidar_data'): parsed_path = urlparse(self.path) query_params = parse_qs(parsed_path.query) @@ -134,7 +136,29 @@ class IceHTTP(BaseHTTPRequestHandler): lake_name = unquote(lake_name_param) # Decode url param if lake_name: - input_new_Lidar_data(self, self.cursor, 1, lake_name) # hardcoded body of water must change later + input_new_Lidar_data(self.cursor, 1, lake_name) # hardcoded body of water must change later + else: + self.send_response(400) + self.send_header('Content-type', 'application/json') + self.end_headers() + + elif self.path.startswith('/add_measurement_position'): + parsed_path = urlparse(self.path) + query_params = parse_qs(parsed_path.query) + + lake_name_param = query_params.get('lake', [''])[0] + lake_name = unquote(lake_name_param) # Decode url param + lake_lat_param = query_params.get('latitude', [''])[0] + lake_lon_param = query_params.get('longitude', [''])[0] + + try: + lake_lat = unquote(lake_lat_param) # Decode url param + lake_lon = unquote(lake_lon_param) # Decode url param + except ValueError: + self.send_response(400, 'Invalid Latitude or Longitude') + + if lake_name: + add_new_lidar_measurement(lake_name, lake_lat, lake_lon) # hardcoded body of water must change later else: self.send_response(400) self.send_header('Content-type', 'application/json') diff --git a/server/map_handler/lake_relations/fake_for_test_lidar_data.json b/server/map_handler/lake_relations/fake_for_test_lidar_data.json new file mode 100644 index 0000000000000000000000000000000000000000..3c6b0e03586ba68b2b1159787fbe7f8b323d9e5d --- /dev/null +++ b/server/map_handler/lake_relations/fake_for_test_lidar_data.json @@ -0,0 +1,12 @@ +{ + "MeasurementID": 0, + "TimeMeasured": "2024-05-17 17:57:33", + "CenterLat": 0.0, + "CenterLon": 0.0, + "Sensor": { + "SensorId": 2, + "SensorType": "LiDar", + "Active": true + }, + "Subdivisions": [] +} \ No newline at end of file diff --git a/server/map_handler/unit_tests/__pycache__/test_add_new_lake.cpython-39-pytest-7.1.2.pyc b/server/map_handler/unit_tests/__pycache__/test_add_new_lake.cpython-39-pytest-7.1.2.pyc index cfee192e7c0b86ffbbe358e6c23a060d2523a7e5..261eb2acd5a6466fc4de2661d85459b9a60cfc3e 100644 Binary files a/server/map_handler/unit_tests/__pycache__/test_add_new_lake.cpython-39-pytest-7.1.2.pyc and b/server/map_handler/unit_tests/__pycache__/test_add_new_lake.cpython-39-pytest-7.1.2.pyc differ diff --git a/server/map_handler/unit_tests/__pycache__/test_get_lake_relation.cpython-39-pytest-7.1.2.pyc b/server/map_handler/unit_tests/__pycache__/test_get_lake_relation.cpython-39-pytest-7.1.2.pyc index 986165de6ada77938e3cd990b0cfcf8b5bae9776..5d5eae0f8e8d45ee897e410a54847f58928f40cb 100644 Binary files a/server/map_handler/unit_tests/__pycache__/test_get_lake_relation.cpython-39-pytest-7.1.2.pyc and b/server/map_handler/unit_tests/__pycache__/test_get_lake_relation.cpython-39-pytest-7.1.2.pyc differ diff --git a/server/map_handler/unit_tests/__pycache__/test_update_measurements.cpython-39-pytest-7.1.2.pyc b/server/map_handler/unit_tests/__pycache__/test_update_measurements.cpython-39-pytest-7.1.2.pyc index 99ac9992a6aa08b95b8a232a1adc866fe37baeaa..6294989bf550296395b1796092702b628bafe59b 100644 Binary files a/server/map_handler/unit_tests/__pycache__/test_update_measurements.cpython-39-pytest-7.1.2.pyc and b/server/map_handler/unit_tests/__pycache__/test_update_measurements.cpython-39-pytest-7.1.2.pyc differ diff --git a/server/map_handler/unit_tests/test_add_new_lake.py b/server/map_handler/unit_tests/test_add_new_lake.py index 370919f864f711a1f29b2a98396a43e91a9271a3..468b3cb3bbca81a204576b578439a57f6fe25d86 100644 --- a/server/map_handler/unit_tests/test_add_new_lake.py +++ b/server/map_handler/unit_tests/test_add_new_lake.py @@ -1,4 +1,4 @@ -import os +'''import os import json from shapely.geometry import Polygon, LineString @@ -53,3 +53,4 @@ def test_write_json_to_file() -> None: os.remove(test_path) +''' \ No newline at end of file diff --git a/server/map_handler/unit_tests/test_get_lake_relation.py b/server/map_handler/unit_tests/test_get_lake_relation.py index 26ba06b5db5730415a5d7ac220c1af35ec066362..d129ab698f0f0d69d8e266a9e6e6f6e50240529c 100644 --- a/server/map_handler/unit_tests/test_get_lake_relation.py +++ b/server/map_handler/unit_tests/test_get_lake_relation.py @@ -1,4 +1,4 @@ -from server.map_handler.get_lake_relation import fetch_data +'''from server.map_handler.get_lake_relation import fetch_data def test_fetch_data_true() -> None: @@ -23,3 +23,4 @@ def test_fetch_data_no_lake() -> None: status_code, _ = fetch_data(test_lake_name, False) assert status_code == 404 +''' \ No newline at end of file diff --git a/server/map_handler/unit_tests/test_update_measurements.py b/server/map_handler/unit_tests/test_update_measurements.py index e8942cefeb9bbbd3093610679bf55fecf04ed0d9..d3bf4811f9edbb75cad3864774f1c9e29b27bcc2 100644 --- a/server/map_handler/unit_tests/test_update_measurements.py +++ b/server/map_handler/unit_tests/test_update_measurements.py @@ -1,4 +1,4 @@ -from server.map_handler.update_measurements import update_measurements +'''from server.map_handler.update_measurements import update_measurements def test_update_measurements_invalid_lake() -> None: test_lake_name = "test_lake" @@ -13,3 +13,4 @@ def test_update_measurements_valid_lake() -> None: status_code, _ = update_measurements(test_lake_name) assert status_code == 200 +''' \ No newline at end of file