diff --git a/.coverage b/.coverage index 33e83fadb35bea307b53549f033bd58858f40a41..8ccad4cf530884d0d7e09dae50f7d7ef537febf1 100644 Binary files a/.coverage and b/.coverage differ diff --git a/server/main.py b/server/main.py index 8bb9b75f8bb80632c5192dcfb352d693bd02acc3..c7a1691335048990fa94fdb1bffcfa6e0eace208 100644 --- a/server/main.py +++ b/server/main.py @@ -6,10 +6,10 @@ from urllib.parse import urlparse, parse_qs, unquote from consts import SSL_CERT_PATH, SSL_KEY_PATH, HOST, PORT from http.server import HTTPServer, BaseHTTPRequestHandler -from map_handler.add_new_lake import cut_map +from map_handler.add_new_lake import cut_map_handler from server.scheduler import update_scheduler from server.consts import LAKE_RELATIONS_PATH -from map_handler.get_lake_relation import get_map_data +from map_handler.get_lake_relation import get_map_data_handler from map_handler.input_new_data import input_new_Lidar_data from map_handler.update_measurements import update_measurements, addTestData @@ -69,7 +69,7 @@ class IceHTTP(BaseHTTPRequestHandler): lake_name = unquote(lake_name_param) # Decode url param if lake_name_param: - get_map_data(self, lake_name, True) # Get all measurements for selected lake + get_map_data_handler(self, lake_name, True) # Get all measurements for selected lake else: self.send_response(400) self.send_header('Content-type', 'application/json') @@ -94,7 +94,7 @@ class IceHTTP(BaseHTTPRequestHandler): lake_name_param = query_params.get('lake', [''])[0] lake_name = unquote(lake_name_param) # Decode url param - get_map_data(self, lake_name, False) + get_map_data_handler(self, lake_name, False) elif self.path.startswith('/add_new_lake'): parsed_path = urlparse(self.path) query_params = parse_qs(parsed_path.query) @@ -108,11 +108,11 @@ class IceHTTP(BaseHTTPRequestHandler): try: # Try to convert the value to a float and the map cell_size_float = float(cell_size_param) - cut_map(self, cursor, lake_name, cell_size_float) + cut_map_handler(self, cursor, lake_name, cell_size_float) except ValueError: print("Error: cell_size_param is not a valid float") else: - cut_map(self, cursor, lake_name) + cut_map_handler(self, cursor, lake_name) else: self.send_response(400) self.send_header('Content-type', 'application/json') diff --git a/server/map_handler/__pycache__/add_new_lake.cpython-311.pyc b/server/map_handler/__pycache__/add_new_lake.cpython-311.pyc index a3599d330d3de9c7f28b5755675f91ddc48c6d37..7231b41d0dfa8a2b47a697b933aa36dcc34b3144 100644 Binary files a/server/map_handler/__pycache__/add_new_lake.cpython-311.pyc and b/server/map_handler/__pycache__/add_new_lake.cpython-311.pyc differ diff --git a/server/map_handler/__pycache__/get_lake_relation.cpython-311.pyc b/server/map_handler/__pycache__/get_lake_relation.cpython-311.pyc index f00723a019e00b75f19812ef8cd73a543fdbf61b..ac9abc72b06341d743f138c326c6ab08aab57679 100644 Binary files a/server/map_handler/__pycache__/get_lake_relation.cpython-311.pyc and b/server/map_handler/__pycache__/get_lake_relation.cpython-311.pyc differ diff --git a/server/map_handler/add_new_lake.py b/server/map_handler/add_new_lake.py index b9d22c5ee92c543483307cceb2b5fdb01ea0aed6..c14df4754e7b7f338e64ef51ddc7b6552882727a 100644 --- a/server/map_handler/add_new_lake.py +++ b/server/map_handler/add_new_lake.py @@ -10,12 +10,23 @@ from shapely.geometry import Polygon, LineString from server.consts import LAKE_RELATIONS_PATH -def cut_map(self, cursor, lake_name: str, cell_size_in_km: float = 0.5): +def cut_map_handler(self, cursor, lake_name: str, cell_size_in_km: float = 0.5): + status_code, map_data = cut_map(cursor, lake_name, cell_size_in_km) + + # Set headers + self.send_response(status_code) + self.send_header("Content-type", "application/json") + self.end_headers() + + # Write the map data to the response + self.wfile.write(json.dumps(map_data).encode('utf-8')) + + +def cut_map(cursor, lake_name: str, cell_size_in_km: float = 0.5) -> (int, str): """ Cuts a map into a grid based on a selected cell size Parameters: - self (BaseHTTPRequestHandler): A instance of a BaseHTTPRequestHandler cursor (cursor): An Sqlite3 cursor object that points to the database lake_name (str): The name of the lake to be cut cell_size_in_km (float): The selected cell size in kilometers @@ -38,9 +49,9 @@ def cut_map(self, cursor, lake_name: str, cell_size_in_km: float = 0.5): cell_height = cell_width / cos(start_x * 0.01745) # List to store new GeoJSON feature objects - features = [] # List to store new GeoJSON feature objects + features = [] # List to store new GeoJSON feature objects sub_div_id = 0 # Tracker to create unique subdivision ids - divided_map = [] # Object for plotting the tiles + divided_map = [] # Object for plotting the tiles # Process all polygons for polygon in polygons: @@ -114,20 +125,15 @@ def cut_map(self, cursor, lake_name: str, cell_size_in_km: float = 0.5): plot_map(divided_map) write_json_to_file(lake_name, feature_collection) - # Return the map to the response object - self.send_response(200) - self.send_header("Content-type", "application/json") - self.end_headers() - - self.wfile.write(json.dumps(feature_collection).encode('utf-8')) + # Return OK and the newly divided map + return 200, feature_collection + except FileNotFoundError as e: + print(f"Failed to find the map file: {e}") + return 404, [] except Exception as e: print(f"Error in adding new map: {e}") - - self.send_response(500) - self.send_header("Content-type", "application/json") - self.end_headers() - + return 500, [] def create_grid(poly: Polygon, cell_width: float, cell_height: float): """ diff --git a/server/map_handler/get_lake_relation.py b/server/map_handler/get_lake_relation.py index 220ebf52b804c072d792a4a86763461f88c3378b..f54746e124a291d4bb6ecc82c4c9107c43501903 100644 --- a/server/map_handler/get_lake_relation.py +++ b/server/map_handler/get_lake_relation.py @@ -1,7 +1,7 @@ from server.consts import LAKE_RELATIONS_PATH -def get_map_data(self, file_name: str, measurement: bool): +def get_map_data_handler(self, file_name: str, measurement: bool): status_code, map_data = fetch_data(file_name, measurement) # Set HTTP headers diff --git a/server/map_handler/unit_tests/__pycache__/test_add_new_lake.cpython-311-pytest-8.2.0.pyc b/server/map_handler/unit_tests/__pycache__/test_add_new_lake.cpython-311-pytest-8.2.0.pyc index c6733b1f387025c9507e38386f998e47bcb8bab2..a74c8c7b03b7928d80c0ec1baad0a0ff2cfdca0a 100644 Binary files a/server/map_handler/unit_tests/__pycache__/test_add_new_lake.cpython-311-pytest-8.2.0.pyc and b/server/map_handler/unit_tests/__pycache__/test_add_new_lake.cpython-311-pytest-8.2.0.pyc differ diff --git a/server/map_handler/unit_tests/__pycache__/test_get_lake_relation.cpython-311-pytest-8.2.0.pyc b/server/map_handler/unit_tests/__pycache__/test_get_lake_relation.cpython-311-pytest-8.2.0.pyc index 498461db33caa3aaa66d942e959682911a937c5d..353e41afb821f20c650945b8ed0f7ccbb176bd19 100644 Binary files a/server/map_handler/unit_tests/__pycache__/test_get_lake_relation.cpython-311-pytest-8.2.0.pyc and b/server/map_handler/unit_tests/__pycache__/test_get_lake_relation.cpython-311-pytest-8.2.0.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 2f7d4c6fb9aa4d3a6a7354920597bc77625d0c5e..6b3fd67f5377ee6bb0fc1f7cb44b67fc01c9cdb3 100644 --- a/server/map_handler/unit_tests/test_add_new_lake.py +++ b/server/map_handler/unit_tests/test_add_new_lake.py @@ -2,7 +2,16 @@ import json from shapely.geometry import Polygon, LineString from server.consts import LAKE_RELATIONS_PATH -from server.map_handler.add_new_lake import create_grid, write_json_to_file +from server.map_handler.add_new_lake import create_grid, write_json_to_file, cut_map + + +def test_cut_map() -> None: + test_cursor = None + test_lake_name = "test_lake" + + status_code, _ = cut_map(test_cursor, test_lake_name, 1) + + assert status_code == 500 def test_create_grid_default() -> None: @@ -37,8 +46,3 @@ def test_write_json_to_file() -> None: result = json.load(f) assert result == test_data - - - - -