Skip to content
Snippets Groups Projects
Commit 92e69875 authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

update: overwrite mjøsa_lidar_data.json with test data

parent ad9ba005
No related branches found
No related tags found
1 merge request!16Clhp map into main
No preview for this file type
......@@ -10,8 +10,8 @@ from map_handler.add_new_lake import cut_map
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.update_measurements import update_measurements
from map_handler.input_new_data import input_new_Lidar_data
from map_handler.update_measurements import update_measurements, addTestData
app = Flask(__name__)
terminate_server = 0
......@@ -117,6 +117,14 @@ class IceHTTP(BaseHTTPRequestHandler):
self.send_response(400)
self.send_header('Content-type', 'application/json')
self.end_headers()
elif self.path.startswith('/add_test_data'):
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
addTestData(self, lake_name)
def do_POST(self):
if self.path == '/new_lidar_data':
......
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -57,7 +57,8 @@ def update_measurements(self, lake_name: str):
temp_to_date = "2024-10-12"
temp_from_date = "2024-10-19"
ice_stats = get_raw_dates(ice_prognosis_raw_data(
sub_div_id=subdiv_id, x=center_lat, y=center_lng), to_date=temp_to_date, from_date=temp_from_date)
sub_div_id=subdiv_id, x=center_lat, y=center_lng), to_date=temp_to_date,
from_date=temp_from_date)
# Ice statistics were retrieved successfully
if len(ice_stats) > 0 and len(ice_stats[0]) > 0:
......@@ -206,3 +207,77 @@ def calculateColor(thickness: float): # NB neither final colors nor ranges
return 0xFF00d6ff # Blue
else:
return 0xFF8C8C8C # Grey
def addTestData(self, lake_name: str):
"""
Adds random test data to lake_name_lidar_data.json. This function is purly for testing, not production.
The function overwrites the lidar data for the selected lake.
Parameters:
self (BaseHTTPRequestHandler): A instance of a BaseHTTPRequestHandler
lake_name (str): The name of the file/lake for the test data
"""
try:
test_data = []
for measurement_id in range(5):
measurement = {
"MeasurementID": measurement_id,
"TimeMeasured": datetime.now().isoformat(),
"CenterLat": random.uniform(60, 61),
"CenterLon": random.uniform(10, 11),
"Sensor": {
"SensorID": random.randint(1, 10),
"SensorType": random.choice(["LiDar", "GPS", "Camera"]),
"Active": random.choice([True, False])
},
"Subdivisions": []
}
# Create 10 subdivisions for each measurement, with randomized coordinates and thicknesses
for subdiv_id in range(20):
subdivision = {
"SubdivID": subdiv_id + 10*measurement_id,
"MinThickness": round(random.uniform(0, 20), 1),
"AvgThickness": round(random.uniform(0, 15), 1),
"CenLatitude": random.uniform(60, 61),
"CenLongitude": random.uniform(10, 11),
"Accuracy": 0
}
measurement["Subdivisions"].append(subdivision)
test_data.append(measurement)
# Overwrite the lidar data file
with open(LAKE_RELATIONS_PATH + lake_name + '_lidar_data.json', 'w') as f:
json.dump(test_data, f, indent=4)
# Convert list of dictionaries to JSON
response_data = json.dumps(test_data, indent=4)
# Set headers
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
# Write processed data to response object
self.wfile.write(response_data.encode('utf-8'))
except FileNotFoundError as e:
print("Failed to find relation file: ", e)
# Set headers
self.send_response(500)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write("File not found")
except Exception as e:
print("Failed to add remaining subdivisions: ", e)
# Set headers
self.send_response(500)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(f"Error in adding test data: {e}".encode('utf-8'))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment