diff --git a/.coverage b/.coverage index 90d487d5235d822965d20d2735542e15d82d4d70..d06917897e5641380540d8f2477af9c3b378a3b8 100644 Binary files a/.coverage and b/.coverage differ diff --git a/README.md b/README.md index e21145f8fb5442c1db3e08660e11d70e4acdc58e..24128d70d40816a6c8db108a217a8fcd906ae7f4 100644 --- a/README.md +++ b/README.md @@ -187,12 +187,14 @@ This endpoint is only for production, and is not implemented in the application. ### New lidar data ``` -Method: POST +Method: GET Path: new_lidar_data?lake=* Paramters: - lake (required) ``` - +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. ## Database This project requires SQLite3. Download the precompiled binary for your operating system. diff --git a/server/ModelFromNVE/icemodellingscripts/__pycache__/getIceThicknessLakes.cpython-39.pyc b/server/ModelFromNVE/icemodellingscripts/__pycache__/getIceThicknessLakes.cpython-39.pyc index 4dc75562c32a5891e0a754550e688c65f718bd34..9c0b3d1fb4c72d32d9bc8b3f5bbc46597f3a9236 100644 Binary files a/server/ModelFromNVE/icemodellingscripts/__pycache__/getIceThicknessLakes.cpython-39.pyc and b/server/ModelFromNVE/icemodellingscripts/__pycache__/getIceThicknessLakes.cpython-39.pyc differ diff --git a/server/Sentinelhub/__pycache__/box_funcitons.cpython-39.pyc b/server/Sentinelhub/__pycache__/box_funcitons.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..2f7dc778831dfc1953ae7a474ce1a164a87091d1 Binary files /dev/null and b/server/Sentinelhub/__pycache__/box_funcitons.cpython-39.pyc differ diff --git a/server/Sentinelhub/__pycache__/evalscript.cpython-39.pyc b/server/Sentinelhub/__pycache__/evalscript.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..540e1f7e8a3e8a9e29736ee320fb0be5b59f01e4 Binary files /dev/null and b/server/Sentinelhub/__pycache__/evalscript.cpython-39.pyc differ diff --git a/server/Sentinelhub/__pycache__/getAreaInfo.cpython-39.pyc b/server/Sentinelhub/__pycache__/getAreaInfo.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..735003454656becfb40081b64cfddc8afa681a6c Binary files /dev/null and b/server/Sentinelhub/__pycache__/getAreaInfo.cpython-39.pyc differ diff --git a/server/__pycache__/consts.cpython-39.pyc b/server/__pycache__/consts.cpython-39.pyc index ded41a9b9378df29070f95b610b3abead5441e75..e1e6e78b87fda546bfd0023eb017c1ddec604a98 100644 Binary files a/server/__pycache__/consts.cpython-39.pyc and b/server/__pycache__/consts.cpython-39.pyc differ diff --git a/server/data_processing/__pycache__/__init__.cpython-39.pyc b/server/data_processing/__pycache__/__init__.cpython-39.pyc index 5e7a44e7ddbd81200f088b345c160601980ccd48..1c432fa6eee7d5356b4a3b456d8fbeb68927634e 100644 Binary files a/server/data_processing/__pycache__/__init__.cpython-39.pyc and b/server/data_processing/__pycache__/__init__.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 new file mode 100644 index 0000000000000000000000000000000000000000..dad8079e88a289d04670f9d108d10184a82da143 Binary files /dev/null and b/server/data_processing/__pycache__/add_new_lidar_measurement.cpython-39.pyc differ diff --git a/server/data_processing/__pycache__/area_processing.cpython-39.pyc b/server/data_processing/__pycache__/area_processing.cpython-39.pyc index 391fab42fdbe9fd2de2ab4a7d5eee38cc51b5e0d..2b4ae7a7e2245ccb5a988c2785f3339556e81724 100644 Binary files a/server/data_processing/__pycache__/area_processing.cpython-39.pyc and b/server/data_processing/__pycache__/area_processing.cpython-39.pyc differ diff --git a/server/data_processing/__pycache__/input_new_data.cpython-39.pyc b/server/data_processing/__pycache__/input_new_data.cpython-39.pyc index 4d82a412097a52e9863d648d978ccd3a9591cfc8..1ba1de3faa52b6a385fb82c82e491b30b6c15a10 100644 Binary files a/server/data_processing/__pycache__/input_new_data.cpython-39.pyc and b/server/data_processing/__pycache__/input_new_data.cpython-39.pyc differ diff --git a/server/data_processing/__pycache__/process_lidar_data.cpython-39.pyc b/server/data_processing/__pycache__/process_lidar_data.cpython-39.pyc index b25a8d60414042c320cca5bb67208462c3090e40..00c429468ce27e0ca4e0f6b21e30495022da114e 100644 Binary files a/server/data_processing/__pycache__/process_lidar_data.cpython-39.pyc and b/server/data_processing/__pycache__/process_lidar_data.cpython-39.pyc differ diff --git a/server/data_processing/add_new_lidar_measurement.py b/server/data_processing/add_new_lidar_measurement.py new file mode 100644 index 0000000000000000000000000000000000000000..166a896c7b259a41a3007fe22bf377030097f462 --- /dev/null +++ b/server/data_processing/add_new_lidar_measurement.py @@ -0,0 +1,95 @@ +import json +import os +from datetime import datetime + +from server.consts import LIDAR_DATA_PATH, LAKE_RELATIONS_PATH + +def add_new_lidar_measurement_handler(self, lake_name: str, lat: float, lon: float): + status_code, updated_content = add_new_lidar_measurement(lake_name, lat, lon) + + self.send_response(status_code) + self.send_header("Content-type", "application/json") + self.end_headers() + + # Write content data to response object + self.wfile.write(json.dumps(updated_content).encode('utf-8')) + +def add_new_lidar_measurement(lake_name: str, lat: float, lon: float): + """ + Adds a new lidar measurement in another location of a lake + + :param self (BaseHTTPRequestHandler): A instance of a BaseHTTPRequestHandler + :param lake_name (str): The name of the requested file/lake + :param lat (float): The latitude of the measurement + :param lon (float): The longitude of the measurement + :return (int, str): A tuple containing the status code and all lidar measurements in one body of water + """ + try: + # find out if lake exist in our database + # sjekk hvordan sara sjekker dette - se i sqlDB eller mapper eller ... + lake_data_path = os.path.join(LIDAR_DATA_PATH + lake_name) + if not os.path.isdir(lake_data_path): + return 404, f"lake does not exist: {lake_name}" + + # read json data with path to data for specific water body + file_path = os.path.join(LAKE_RELATIONS_PATH, lake_name + '_lidar_data.json') + + # time last updated data + time_now = datetime.now().utcnow().replace(microsecond=0) + + content = [] + + # check if water body data exist, else create + if os.path.exists(file_path): + # get the areas from the map's data + with open(file_path) as data: + measurement_data = json.load(data) + + # create an empty container for new measurement + new_measurement_data = { + 'MeasurementID': len(measurement_data) - 1, + 'TimeMeasured': str(time_now), + 'CenterLat': lat, + 'CenterLon': lon, + 'Sensor': { + 'SensorId': 2, + 'SensorType': "LiDar", + "Active": True, + }, + 'Subdivisions': [], + } + + measurement_data.append(new_measurement_data) + + # convert data to json format + content = json.dumps(measurement_data, indent=4) + + else: + # create an empty container for new measurement + new_measurement_data = { + 'MeasurementID': 0, + 'TimeMeasured': str(time_now), + 'CenterLat': lat, + 'CenterLon': lon, + 'Sensor': { + 'SensorId': 2, + 'SensorType': "LiDar", + "Active": True, + }, + 'Subdivisions': [], + } + content = json.dumps(new_measurement_data, indent=4) + + # write to file + with open(file_path, "w") as file: + file.write(content) + + # send response + return 200, content + + # error handling + except Exception as e: + return 500, f"An error occurred: {e} g".encode("utf-8") + + +print(add_new_lidar_measurement("mjøsa", 60.9, 10.9)) \ No newline at end of file diff --git a/server/data_processing/input_new_data.py b/server/data_processing/input_new_data.py index 946824d7e63ba8efecfef8f75ab19c7f88c4b2ad..2ca6cd2b31685b24e83aceb012bad9579fa8609b 100644 --- a/server/data_processing/input_new_data.py +++ b/server/data_processing/input_new_data.py @@ -23,6 +23,8 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str): :param cursor (cursor): An Sqlite3 cursor object that points to the database :param sensorId: Id of the sensor used to measure the data :param lake_name (str): The name of the requested file/lake + :return: (int, str): A tuple containing the status code and the meassurement data of the lidar files and where it + was taken on the map """ try: # print("name=",lake_name) @@ -32,14 +34,14 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str): # read json data with path to data for specific water body file_path = os.path.join(LAKE_RELATIONS_PATH, lake_name + '_lidar_data.json') - # map of water body area exist + # check if water body data exist if not os.path.exists(file_path): - # Just temporary, not gonna use: - # with open(file_path, "w") as file: - # file.write( - # "[{\"MeasurementID\": 1,\"TimeMeasured\": \"2024-04-15 16:23:28.620516\",\"CenterLat\": 60.841532,\"CenterLon\": 10.717878,\"Sensor\": {\"SensorID\": 2,\"SensorType\": \"LiDar\",\"Active\": true},\"Subdivisions\": []},{\"MeasurementID\": 2,\"TimeMeasured\": \"2024-04-15 16:23:28.620516\",\"CenterLat\": 60.841532,\"CenterLon\": 10.717878,\"Sensor\": {\"SensorID\": 2,\"SensorType\": \"LiDar\",\"Active\": true},\"Subdivisions\": []}]") # Writing an empty JSON object - print("File not found") - return 404, f"no {lake_name} exist on this map" + return 404, f"no data for {lake_name} found" + + # Just temporary, not gonna use: + # with open(file_path, "w") as file: + # file.write( + # "[{\"MeasurementID\": 1,\"TimeMeasured\": \"2024-04-15 16:23:28.620516\",\"CenterLat\": 60.841532,\"CenterLon\": 10.717878,\"Sensor\": {\"SensorID\": 2,\"SensorType\": \"LiDar\",\"Active\": true},\"Subdivisions\": []},{\"MeasurementID\": 2,\"TimeMeasured\": \"2024-04-15 16:23:28.620516\",\"CenterLat\": 60.841532,\"CenterLon\": 10.717878,\"Sensor\": {\"SensorID\": 2,\"SensorType\": \"LiDar\",\"Active\": true},\"Subdivisions\": []}]") # Writing an empty JSON object # get the areas from the map's data with open(file_path) as data: @@ -83,7 +85,6 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str): # Calculate each area's ice thickness based on the area coordinates given to the calculation model areas_data = calculate_area_data((latitude, longitude), lake_name, laz_file_path) - print("somehow works") if (areas_data): # store lidar data in json format # calculate data for each zone within the area @@ -121,28 +122,27 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str): else: print('No data found') - print("went past") measurement_data = { 'MeasurementID': measurement_id, 'TimeMeasured': str(time_now), 'CenterLat': latitude, 'CenterLon': longitude, 'Sensor': { - 'SensorId': 2, + 'SensorId': sensorId, 'SensorType': "LiDar", "Active": True, }, 'Subdivisions': subdiv_json_data, } - print("went past to append") lidar_json_data.append(measurement_data) - print("went past to append") # send the changes to the database cursor.connection.commit() - print("jovid") + # container for content, might not be needed + content = None + # check if there are ice in area if len(lidar_json_data) > 0: if os.path.exists(file_path): @@ -154,16 +154,13 @@ def input_new_Lidar_data(cursor, sensorId, lake_name: str) -> (int, str): # write to file with open(file_path, "w") as file: file.write(content) - - print("jovid2") - # Send response - return 200, content else: # return empty list, since there were no ice in area scanned content = json.dumps([]) - print('No ice found in area') - return 200, content + print('No ice found in current area') + # send response + return 200, content # error handling except Exception as e: diff --git a/server/data_processing/unit_tests/__pycache__/__init__.cpython-39.pyc b/server/data_processing/unit_tests/__pycache__/__init__.cpython-39.pyc index bab6ac9d680fceba19ad6359bbdf4d33d0c06a25..30e35933ced48b5c9e62d2122fbd977d99ea0fa4 100644 Binary files a/server/data_processing/unit_tests/__pycache__/__init__.cpython-39.pyc and b/server/data_processing/unit_tests/__pycache__/__init__.cpython-39.pyc differ 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 new file mode 100644 index 0000000000000000000000000000000000000000..1c31f05f9ebcece4830641353883b360d925b792 Binary files /dev/null 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_area__processing.cpython-39-pytest-7.1.2.pyc b/server/data_processing/unit_tests/__pycache__/test_area__processing.cpython-39-pytest-7.1.2.pyc index 31953821049774df24066f0c6941245d740dc5fd..8b0f8126b378c7b1fe671e6a619184c6665dd649 100644 Binary files a/server/data_processing/unit_tests/__pycache__/test_area__processing.cpython-39-pytest-7.1.2.pyc and b/server/data_processing/unit_tests/__pycache__/test_area__processing.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 aa59423b4338645471aa047587af7f9807d410b1..279b93a3e9baf699a8abb16b934f7ae92a575cdb 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 bf986448ae8ed6e2e9d2b42f374754d56a45dca1..4d286464ec758a12fb05f87a060a9983fe992b85 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 new file mode 100644 index 0000000000000000000000000000000000000000..987ffe5b96663b72233889fbbd9a7160bda8ab9c --- /dev/null +++ b/server/data_processing/unit_tests/test_add_new_lidar_measurement.py @@ -0,0 +1,18 @@ +from server.data_processing.add_new_lidar_measurement import add_new_lidar_measurement + +def test_add_new_lidar_measurement_invalid_lake() -> None: + lake_name = 'test_lake' + lat = 0.0 + lon = 0.0 + actual, _ = add_new_lidar_measurement(lake_name, lat, lon) + expected = 404 + assert actual == expected + +def test_add_new_lidar_measurement_valid_lake() -> None: + lake_name = 'mjøsa' + lat = 0.0 + lon = 0.0 + actual, _ = add_new_lidar_measurement(lake_name, lat, lon) + expected = 200 + assert actual == expected + diff --git a/server/data_processing/unit_tests/test_area__processing.py b/server/data_processing/unit_tests/test_area__processing.py index e00a83009f5007bfb02a0b991928ab67f3955317..277deaba4dee991c529b7b2bf6d76200ba2b8c8e 100644 --- a/server/data_processing/unit_tests/test_area__processing.py +++ b/server/data_processing/unit_tests/test_area__processing.py @@ -1,5 +1,3 @@ -from unittest.mock import MagicMock - from server.data_processing.area_processing import inArea, distance, closest_points, find_height, calculate_corners, define_gridareas, define_grid_lidardata def test_inArea_inside() -> None: diff --git a/server/data_processing/unit_tests/test_input_new_data.py b/server/data_processing/unit_tests/test_input_new_data.py index 4303d0b41cfe871cfe0d43a98a79ff81c8510cc0..0daa6c55791cb3f640d65417ef7dc9a8c00cb603 100644 --- a/server/data_processing/unit_tests/test_input_new_data.py +++ b/server/data_processing/unit_tests/test_input_new_data.py @@ -63,6 +63,7 @@ def test_new_input_unvalid_cursor() -> None: status_code, _ = input_new_Lidar_data(mock_cursor, sensor_id, test_lake_name) assert status_code == 500 + ''' def test_new_input_valid_cursor() -> None: sensor_id = 1 diff --git a/server/map_handler/__pycache__/add_new_lake.cpython-39.pyc b/server/map_handler/__pycache__/add_new_lake.cpython-39.pyc index 360bb6bc650b05b6f1e0dd479c799ffdd42be2cd..a2003fedd994538f1e6f8c40f7c4b14cd9bfed16 100644 Binary files a/server/map_handler/__pycache__/add_new_lake.cpython-39.pyc and b/server/map_handler/__pycache__/add_new_lake.cpython-39.pyc differ diff --git a/server/map_handler/__pycache__/get_lake_relation.cpython-39.pyc b/server/map_handler/__pycache__/get_lake_relation.cpython-39.pyc index 69cc0062bde8be6c071a8b5022d03bb31e477a61..78a3ba0cb8002fc8db42bb51086a412bb9c7bc28 100644 Binary files a/server/map_handler/__pycache__/get_lake_relation.cpython-39.pyc and b/server/map_handler/__pycache__/get_lake_relation.cpython-39.pyc differ diff --git a/server/map_handler/__pycache__/update_measurements.cpython-39.pyc b/server/map_handler/__pycache__/update_measurements.cpython-39.pyc index 55dfc3d1701664629e0aae4131a0b1b428cf136d..9f807770e09b37f6ad378e4c78c19cb759f8fae9 100644 Binary files a/server/map_handler/__pycache__/update_measurements.cpython-39.pyc and b/server/map_handler/__pycache__/update_measurements.cpython-39.pyc differ 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 ed5bccfcc82a13f1ed283a3e65cac18b3c6ea0fe..cfee192e7c0b86ffbbe358e6c23a060d2523a7e5 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_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 0f0fc11bca8d501fc89871ef0468915c06d7aa85..99ac9992a6aa08b95b8a232a1adc866fe37baeaa 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_update_measurements.py b/server/map_handler/unit_tests/test_update_measurements.py index e05ae8edcf28565e8a21a18a808f4d1fa92e2c29..e8942cefeb9bbbd3093610679bf55fecf04ed0d9 100644 --- a/server/map_handler/unit_tests/test_update_measurements.py +++ b/server/map_handler/unit_tests/test_update_measurements.py @@ -1,6 +1,5 @@ from server.map_handler.update_measurements import update_measurements - def test_update_measurements_invalid_lake() -> None: test_lake_name = "test_lake" @@ -8,7 +7,6 @@ def test_update_measurements_invalid_lake() -> None: assert status_code == 404 - def test_update_measurements_valid_lake() -> None: test_lake_name = "Skumsjøen"