Skip to content
Snippets Groups Projects
input_new_data.py 2.79 KiB
import json
from datetime import datetime
from server.data_processing.process_lidar_data import calculate_area_data

# 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):
    try:
        # hard coded coordinates
        latitude = 60.0
        longitude = 10.0

        total_measurement_average = 0  # the total average of a measurement

        # 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(  SensorID, TimeMeasured, WaterBodyName, 
                                        WholeAverageThickness, CenterLat, CenterLon) VALUES 
                (?,?,?,?,?,?);
        ''', (sensorId, datetime.utcnow().replace(microsecond=0), bodyOfWater, 0, latitude, longitude))

        # auto generate new measurement id
        measurement_id = cursor.lastrowid

        # calculate the area of to be calculated based on the coordinates given to the calculation model
        areas_data = calculate_area_data((latitude, longitude))


        if(areas_data):
            # calculate data for each zone within the area
            for area in areas_data:
                if(len(area[2]) != 0):
                    average = sum(area[2])/len(area[2])
                    minimum_thickness = min(area[2])
                else:
                    average = 0
                    minimum_thickness = 0

                total_measurement_average += average

                # input the data into the database
                cursor.execute('''
                INSERT INTO SubDivision(MeasurementID, SubDivisionID, GroupID, MinimumThickness, AverageThickness, CenterLatitude, CenterLongitude, Accuracy) VALUES
                    (?,?,?,?,?,?,?,?);
                ''',(measurement_id, area[0], area[1], float(minimum_thickness), float(average), float(latitude), float(longitude), float(1)))

            total_measurement_average = total_measurement_average / len(areas_data)

            # input the newly generated measurement_id and whole average thickness
            cursor.execute('''
                UPDATE Measurement
                SET measurementID = ?, WholeAverageThickness = ?
                WHERE MeasurementID IS NULL AND WholeAverageThickness = 0;
            ''', (int(measurement_id), total_measurement_average), )
        else:
            print('No data found')

        # send the changes to the database
        cursor.connection.commit()

        # Send response
        self.send_response(200)

    # error handling
    except Exception as e:
        print("An error occurred", e)
        # rollback in case of error
        cursor.connection.rollback()