diff --git a/server/main.py b/server/main.py index 662aef6517ed83ed2862bfff0f99fa9f471c3816..1891385361f029134334370d473c6484da6f5f6f 100644 --- a/server/main.py +++ b/server/main.py @@ -81,7 +81,7 @@ class IceHTTP(BaseHTTPRequestHandler): lake_name = unquote(lake_name_param) # Decode url param if lake_name_param: - get_measurements(self, cursor, lake_name) + get_measurements(self, lake_name) else: self.send_response(400) self.send_header('Content-type', 'application/json') diff --git a/server/map_handler/get_measurements.py b/server/map_handler/get_measurements.py index bddb710f19b723a93bca76238eede870e169c380..cf73c8f290fbc3405c7c68a86eb1bb60c4921d09 100644 --- a/server/map_handler/get_measurements.py +++ b/server/map_handler/get_measurements.py @@ -1,3 +1,4 @@ +import os import json from datetime import datetime @@ -5,7 +6,7 @@ from server.consts import LAKE_RELATIONS_PATH from server.ModelFromNVE.icemodellingscripts.getIceThicknessLakes import get_raw_dates, ice_prognosis_raw_data -def get_measurements(self, cursor, lake_name): +def get_measurements(self, lake_name): """ Retrieves LiDar data for a given lake, and adds weather data to each subdivision. @@ -15,42 +16,47 @@ def get_measurements(self, cursor, lake_name): lake_name (str): The name of the requested file/lake """ try: - # Read the newest lidar data from JSON file - with open(LAKE_RELATIONS_PATH + lake_name + '_lidar_data.json', 'r') as file: - lidar_data = json.load(file) - + file_path = os.path.join(LAKE_RELATIONS_PATH, lake_name + '_measurements.json') sub_div_ids = [] - - # Iterate over all fetched rows - for measurement in lidar_data: - - for sub_division in measurement['Subdivisions']: - measurement_id = measurement['MeasurementID'] - subdiv_id = sub_division['SubdivID'] - center_lat = sub_division['CenLatitude'] - center_lng = sub_division['CenLongitude'] - - # Create new subdivision object - sub_division = { - 'SubdivID': subdiv_id, - 'GroupID': 0, - 'MinThickness': sub_division['MinThickness'], - 'AvgThickness': sub_division['AvgThickness'], - 'CenLatitude': center_lat, - 'CenLongitude': center_lng, - 'Accuracy': sub_division['Accuracy'], - 'Color': calculateColor(sub_division['MinThickness'], ), - # NB color calculated based on average thickness, should be minimum - # Fetch weather data from the NVE model - 'IceStats': get_raw_dates(ice_prognosis_raw_data(sub_div_id=subdiv_id, x=center_lat, y=center_lng)) - } - sub_div_ids.append(subdiv_id) - - lidar_data[measurement_id]['Subdivisions'].append(sub_division) + measurements = [] + + # Check if the file exists + if os.path.exists(file_path): + # Read the newest lidar data from JSON file + with open(LAKE_RELATIONS_PATH + lake_name + '_measurements.json', 'r') as file: + measurements = json.load(file) + + # Iterate over all fetched rows + for measurement in measurements: + + for sub_division in measurement['Subdivisions']: + measurement_id = measurement['MeasurementID'] + subdiv_id = sub_division['SubdivID'] + center_lat = sub_division['CenLatitude'] + center_lng = sub_division['CenLongitude'] + + # Create new subdivision object + sub_division = { + 'SubdivID': subdiv_id, + 'GroupID': 0, + 'MinThickness': sub_division['MinThickness'], + 'AvgThickness': sub_division['AvgThickness'], + 'CenLatitude': center_lat, + 'CenLongitude': center_lng, + 'Accuracy': sub_division['Accuracy'], + 'Color': calculateColor(sub_division['MinThickness'], ), + # NB color calculated based on average thickness, should be minimum + # Fetch weather data from the NVE model + 'IceStats': get_raw_dates( + ice_prognosis_raw_data(sub_div_id=subdiv_id, x=center_lat, y=center_lng)) + } + sub_div_ids.append(subdiv_id) + + measurements[measurement_id]['Subdivisions'].append(sub_division) # Populate remaining subdivisions and create "invalid" or "proxy" measurement to store them remaining_sub_divs = fill_remaining_subdivisions(lake_name, sub_div_ids) - lidar_data[-1] = { + measurements[-1] = { 'MeasurementID': -1, 'TimeMeasured': str(datetime.now()), 'CenterLat': None, @@ -60,21 +66,21 @@ def get_measurements(self, cursor, lake_name): } # Convert dictionary values to list of measurements - data = list(lidar_data.values()) + data = list(measurements.values()) # Write the newest measurements to file - with open(LAKE_RELATIONS_PATH + lake_name.lower() + '_lidar_data.json', 'w') as f: + with open(LAKE_RELATIONS_PATH + lake_name.lower() + '_measurements.json', 'w') as f: json.dump(data, f) if len(data) == 0: - marker_data = json.dumps(['no measurements']) + response_data = json.dumps(['no measurements']) else: # Convert list of dictionaries to JSON - marker_data = json.dumps(data, indent=4) + response_data = json.dumps(data, indent=4) except Exception as e: print(f"Error in getting measurements: {e}") - marker_data = '[]' + response_data = '[]' # Set headers self.send_response(500) @@ -86,8 +92,8 @@ def get_measurements(self, cursor, lake_name): self.send_header("Content-type", "application/json") self.end_headers() - # Write marker data to response object - self.wfile.write(marker_data.encode('utf-8')) + # Write processed data to response object + self.wfile.write(response_data.encode('utf-8')) def fill_remaining_subdivisions(lake_name: str, sub_div_ids: list): diff --git "a/server/map_handler/lake_relations/mj\303\270sa_lidar_data.json" "b/server/map_handler/lake_relations/mj\303\270sa_lidar_data.json" index 2a6cbd3bdf2d564a34efed805a51e74a877326d0..5f5ddb8f133c09a499643308e254b0b6c300c111 100644 --- "a/server/map_handler/lake_relations/mj\303\270sa_lidar_data.json" +++ "b/server/map_handler/lake_relations/mj\303\270sa_lidar_data.json" @@ -4,7 +4,7 @@ "TimeMeasured": "2024-04-15 16:23:28.620516", "CenterLat": 60.841532, "CenterLon": 10.717878, - "Sensor": null, + "Sensor": 2, "Subdivisions": [ { "SubdivID": 36, @@ -12,7 +12,7 @@ "AvgThickness": 1, "CenLatitude": 60.841532, "CenLongitude": 10.717878, - "Accuracy": null, + "Accuracy": 2, "Heights": [1] }, { @@ -21,7 +21,7 @@ "AvgThickness": 14, "CenLatitude": 60.828326, "CenLongitude": 10.982563, - "Accuracy": null, + "Accuracy": 2, "Heights": [1, 27] }, { @@ -30,7 +30,7 @@ "AvgThickness": 1, "CenLatitude": 60.771059, "CenLongitude": 10.698341, - "Accuracy": null, + "Accuracy": 2, "Heights": [1] }, { @@ -39,7 +39,7 @@ "AvgThickness": 9, "CenLatitude": 60.396856, "CenLongitude": 11.220933, - "Accuracy": null, + "Accuracy": 2, "Heights": [1, 4, 6, 27, 7] } ]