Newer
Older
from datetime import datetime
from server.data_processing.process_lidar_data import calculate_area_data, about_laz_file, find_folder_files
# input_new_Lidar_data send new data gathered from the lidar and send it to the database (from the drone, most likely)
def input_new_Lidar_data(self, cursor, sensorId, bodyOfWater):
send the sensors data of the bodyOfWater to database and update the jsonfile for specific body of water
:param self (BaseHTTPRequestHandler): A instance of a BaseHTTPRequestHandler
:param cursor (cursor): An Sqlite3 cursor object that points to the database
:param sensorId: Id of the sensor used to measure the data
:param bodyOfWater (str): The name of the requested file/lake
# print("name=",bodyOfWater)
# laz_root_path = "server\\lidar_data\\" + bodyOfWater
# laz_data_paths = find_folder_files(laz_root_path)
# read json data with path to data for specific water body
file_path = "server/map_handler/lake_relations/" + bodyOfWater + "_lidar_data.json"
if os.path.exists(file_path):
with open(file_path) as data:
measurement_data = json.load(data)
if len(measurement_data) < 1:
print("no coordinates found")
self.send_response(404) # Sending 404 Not Found response
self.end_headers()
# Stop the server
self.server.shutdown()
lidar_json_data = []
subdiv_json_data = []
for measurement in measurement_data:
measurement_id = measurement["MeasurementID"]
latitude = measurement["CenterLat"]
longitude = measurement["CenterLon"]
laz_file_path = "server\\lidar_data\\" + bodyOfWater + "\\measurement_id_" + str(
measurement_id) + ".laz"
about_laz_file(laz_file_path)
# data about the file read from
# about_laz = about_laz_file() cannot do this if data is changed
# scale_factor = max(about_laz[2])
scale_factor = max([0.01])
time_now = datetime.now().utcnow().replace(microsecond=0)
# create a new measurement with the time the data is sent, sensor type, where
# and an estimate of average thickness of ice on water body
cursor.execute('''
INSERT INTO Measurement(MeasurementID, SensorID, TimeMeasured, WaterBodyName, CenterLat, CenterLon) VALUES
(?,?,?,?,?,?);
''', (measurement_id, sensorId, time_now, bodyOfWater, latitude, longitude))
# calculate the area of to be calculated based on the coordinates given to the calculation model
areas_data = calculate_area_data((latitude, longitude), bodyOfWater, laz_file_path)
# store lidar data in jason formate
# calculate data for each zone within the area
for area in areas_data:
# lng and lat relative to map
subId = int(area[0])
map_lat, map_lng = area[1]
heights = area[2]
if (len(heights) != 0 or sum(heights) != 0):
average = sum(heights) / len(heights)
minimum_thickness = min(heights)
else:
average = 0
minimum_thickness = 0
# input the data into the database
cursor.execute('''
INSERT INTO SubdivisionMeasurementData(MeasurementID, TimeMeasured, SubdivID, WaterBodyName, MinimumThickness, AverageThickness, CalculatedSafety, Accuracy) VALUES
(?,?,?,?,?,?,?,?);
''', (measurement_id, time_now, subId, bodyOfWater, float(minimum_thickness), float(average),
float(0.0), scale_factor))
sub_center = (map_lat, map_lng)
# set up json formate
lidar_read = {
'SubdivID': subId,
'MinThickness': float(minimum_thickness),
'AvgThickness': float(average),
'CenLatitude': float(sub_center[0]),
'CenLongitude': float(sub_center[1]),
'Accuracy': scale_factor,
}
subdiv_json_data.append(lidar_read)
else:
print('No data found, line 79')
measurement_data = {
'MeasurementID': measurement_id,
'TimeMeasured': str(time_now),
'CenterLat': latitude,
'CenterLon': longitude,
'Sensor': {
'SensorId': 2,
'SensorType': "LiDar",
"Active": True,
},
'Subdivisions': subdiv_json_data,
lidar_json_data.append(measurement_data)
# send the changes to the database
cursor.connection.commit()
# Send response
self.send_response(200)
self.send_header('Content-type', "application/json")
self.end_headers()
content = None
current_directory = os.getcwd()
print("Current working directory:", current_directory)
if len(lidar_json_data) > 0:
if os.path.exists(file_path):
os.remove(file_path)
# convert list of lidar data to json
content = json.dumps(lidar_json_data, indent=4)
with open(file_path, "w") as file:
file.write(content)
else:
print('No data found, line 101')
content = json.dumps([])
Hoa Ben The Nguyen
committed
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 does not exist", file_path)
# Send response
self.send_response(404)
self.send_header('Content-type', "application/json")
self.end_headers()
content = None
Hoa Ben The Nguyen
committed
# Write content data to response object
self.wfile.write(content.encode('utf-8'))
# error handling
except Exception as e:
Hoa Ben The Nguyen
committed
print("An error occurred: ", e)
# rollback in case of error
Hoa Ben The Nguyen
committed
cursor.connection.rollback()
# laz_root_path = "server\\lidar_data\\mj\u00f8sa"
# laz_data_paths = find_folder_files(laz_root_path)
# print(laz_data_paths)