diff --git a/server/data_processing/__pycache__/area_processing.cpython-311.pyc b/server/data_processing/__pycache__/area_processing.cpython-311.pyc index 633c3ea85b6c7a9ba12a981f1f16c33fb08d67d6..c4d18273f5db28962849572da4520dd4025acbdc 100644 Binary files a/server/data_processing/__pycache__/area_processing.cpython-311.pyc and b/server/data_processing/__pycache__/area_processing.cpython-311.pyc differ diff --git a/server/data_processing/__pycache__/process_lidar_data.cpython-311.pyc b/server/data_processing/__pycache__/process_lidar_data.cpython-311.pyc index bcaab3c96c44cab902c18d3bd614a974fbd0561c..987adc32a54b4c008aebe4a29f2353d85a1bad1b 100644 Binary files a/server/data_processing/__pycache__/process_lidar_data.cpython-311.pyc and b/server/data_processing/__pycache__/process_lidar_data.cpython-311.pyc differ diff --git a/server/data_processing/__pycache__/process_lidar_data.cpython-39.pyc b/server/data_processing/__pycache__/process_lidar_data.cpython-39.pyc index 47edf95b89a3f2a16363fcc7430b1e02bc70d60d..3edb3e6aec41636e85a32723ec41ee26bef38aba 100644 Binary files a/server/data_processing/__pycache__/process_lidar_data.cpython-39.pyc and b/server/data_processing/__pycache__/process_lidar_data.cpython-39.pyc differ diff --git a/server/data_processing/area_processing.py b/server/data_processing/area_processing.py index 95de76fcbee82192f8a6308c3a32d289a722f996..ada5e41f9b49028a38196f4aa7b98250e31c2bad 100644 --- a/server/data_processing/area_processing.py +++ b/server/data_processing/area_processing.py @@ -24,37 +24,45 @@ def calculate_corners(lat, lng, area_offset): (lat_pos, lng_neg), # bottom right (lat_neg, lng_neg) # bottom left ] -'''return [(60.7798, 10.7062), # NB: temporary hardcoded values - (60.7553, 10.7433), - (60.7718, 10.7975), - (60.7966, 10.7405)]''' # separate the zones into smaller area, into a grid -def define_gridareas(lat, lng, area_offset, grid_size, dimension, subId, groupId): - a = dimension + subId + groupId # soon to be implemented - print(a) - grided_area = [] # container for an area turned into a grid of areas +def define_gridareas(lat, lng, area_offset, grid_size): + # container for an area turned into a grid of areas + grided_area = [] # find the main area's corner positions main_area = calculate_corners(lat, lng, area_offset) # find the main area's range size - area_size = (main_area[0][0] - main_area[2][0], main_area[0][1] - main_area[2][1]) + area_size = (main_area[2][0] - main_area[0][0], main_area[0][1] - main_area[2][1]) # find each subareas vector to it's center dist_to_subcenter = (area_size[0]/(grid_size*2), area_size[1]/(grid_size*2)) + # find subareas size relative to main area subarea_offset = area_offset/grid_size + group_id = 0 + i=0 # find the center coordinates of each area in grid to find the corner areas - for y in range(grid_size-1): + for y in (range(grid_size)): relative_size_lng = y / grid_size - for x in range(grid_size-1): + for x in (range(grid_size)): relative_size_lat = x / grid_size - lat_pos = main_area[0][0] + relative_size_lat * area_size[0] - dist_to_subcenter[0] - lng_pos = main_area[0][1] + relative_size_lng * area_size[1] - dist_to_subcenter[1] + lat_pos = main_area[3][0] + relative_size_lat * area_size[0] + dist_to_subcenter[0] + lng_pos = main_area[3][1] + relative_size_lng * area_size[1] + dist_to_subcenter[1] + # id of each subarea + sub_id = x % grid_size + grid_size * y + + if (x+(y*grid_size)) % ((grid_size*2) + 1) == grid_size * 2: + group_id += 2 + + if (x+(y*grid_size)) % 2 == 0 and (x+(y*grid_size)) != 0: + i += 1 + # use the center of sub areas to find the corner of each subarea - grided_area.append(calculate_corners(lat_pos, lng_pos,subarea_offset)) + corners = calculate_corners(lat_pos, lng_pos, subarea_offset) + grided_area.append((sub_id, group_id + i%2, corners)) return grided_area -#print(calculate_corners(60,10,1500)) -#print(define_gridareas(60,10,1500,4)) \ No newline at end of file +#print(define_gridareas(-60,10,10000,4)) +#print(define_gridareas(3435693.5,6299200.0, 400000,4)) \ No newline at end of file diff --git a/server/data_processing/process_lidar_data.py b/server/data_processing/process_lidar_data.py index a0282be55a54d6a50c0a3b3c6aeaf2d8710924a7..46162b5a0db5bc9543e95b9332031865ad49a5f0 100644 --- a/server/data_processing/process_lidar_data.py +++ b/server/data_processing/process_lidar_data.py @@ -1,7 +1,9 @@ import numpy as np import laspy +import utm # src: https://github.com/Turbo87/utm from itertools import groupby -from process_lidar_data import calculate_corners, define_gridareas +from server.data_processing.area_processing import calculate_corners, define_gridareas + # hard coded files for test data lazData_path = ["server/example_lidar_data/ot_N_000005_1.laz", "server/example_lidar_data/ot_N_000033_1.laz"] @@ -27,7 +29,7 @@ with laspy.open(lazData_path[0]) as fh: # check if lidar points is within range of the area selected def inArea(position, areaRange): x, y, _ = position - if (areaRange[0][0] < x < areaRange[1][0]) and (areaRange[0][1] > y > areaRange[1][1]): + if (areaRange[0][0] > x > areaRange[1][0]) and (areaRange[0][1] < y < areaRange[1][1]): return True else: return False @@ -55,29 +57,17 @@ def find_height(points): difference = max_height - min_height height_differences.append(difference) - # list of thickness in a area + # list of thickness in an area return height_differences # find the height of an area based on the coordinates of it's center # and it's affiliations (subId and groupId) (soon to be implemented -def height_in_area(center): +def calculate_area_data(center): # container for all the heights in area area_heights = [] - # Limit the data to specific areas - - # this is only temporary data for coordinates in arctic - # areas = [ - # [(-3671212, 7898422), (-3200175, 4699978)], - # [(60.815356, 10.672022), (60.774878, 10.768867)], - # ] - - # specified the area and it's range - area = calculate_corners(center, 1500) - area = (area[0], area[2]) - # NB: is only for the test data (soon to be removed) - areazone = [(((area[0]) * ((-3671212/60.815356) - (-3200175/60.774878))) - 3200175, (area[1] * ((7898422/10.768867) - (4699978/10.672022))) + 4699978), - (((area[0] - 100) * ((-3671212/60.815356) - (-3200175/60.774878))) - 3200175, ((area[1] - 1) * ((7898422/10.768867) - (4699978/10.672022))) + 4699978)] + # set the limit of the area compared to local coordinates + area_limit = calculate_corners(center[0],center[1], 1500) # Refactor lidar data to a readable format iceOver = laspy.read(lazData_path[0]) @@ -85,13 +75,29 @@ def height_in_area(center): # add two files together temporary test data(soon to be removed) ice_points = list(zip(iceOver.X,iceOver.Y,iceOver.Z)) + list(zip(iceUnder.X,iceUnder.Y,iceUnder.Z)) + max_point = max(ice_points) + min_point = min(ice_points) - # area height in all the subsections - grid_area_heights = define_gridareas(areazone, 1500, 20) + # define all the sub-areas within the area, local coordinates + grid_area_heights = define_gridareas(60,10, 1500,4) # find the heights of each sub-area => area-heights for sub_area in grid_area_heights: - ice_points = list(filter(lambda point_position: inArea(point_position, (sub_area[0],sub_area[2])), ice_points)) - area_heights.append(find_height(ice_points)) + start = min(sub_area[2]) + end = max(sub_area[2]) + + #test data + # zone coordinates sett to be relative to the lidar data + areazone = [(((min_point[0] - max_point[0]) * ((start[0]-center[0])/(area_limit[1][0]-area_limit[3][0]))) + (min_point[0] - max_point[0])/2 + max_point[0], + (((min_point[1] - max_point[1]) * ((start[1]-center[1])/(area_limit[1][1]-area_limit[3][1]))) + (min_point[1] - max_point[1])/2 + min_point[1])), + ((((min_point[0] - max_point[0]) * ((end[0]-center[0])/(area_limit[1][0]-area_limit[3][0]))) + (min_point[0] - max_point[0])/2 + max_point[0], + (((min_point[1] - max_point[1]) * ((end[1]-center[1])/(area_limit[1][1]-area_limit[3][1])))) + (min_point[1] - max_point[1])/2 + min_point[1]))] + + # filter data within sub-area zones + points_in_area = list(filter(lambda point_position: inArea(point_position, areazone), ice_points)) + area_heights.append((sub_area[0],sub_area[1],find_height(points_in_area))) return area_heights + + + diff --git a/server/main.py b/server/main.py index 2295e3ebd0a1daa5ffaada2d542b4b357bff073a..dd9e1df7f2d1d6991136b29a0f0099a8c4294477 100644 --- a/server/main.py +++ b/server/main.py @@ -4,13 +4,13 @@ from consts import SSL_CERT_PATH, SSL_KEY_PATH, HOST, PORT from map.get_markers import get_all_markers from map.get_relation import cut_map from APIs.get_weather import get_weather +from map.input_new_data import input_new_Lidar_data import ssl import sqlite3 app = Flask(__name__) terminate_server = 0 - class IceHTTPServer(HTTPServer): def __init__(self, server_address, handler_class, cursor): super().__init__(server_address, handler_class) @@ -34,7 +34,7 @@ class IceHTTP(BaseHTTPRequestHandler): self.send_header("Content-type", "text/plain") self.end_headers() - self.wfile.write(b"The root path provides no functionality. Please use a valid endpoint") + self.wfile.write(b"Root path hit!") elif self.path == '/update_map': # NB: should be POST? get_all_markers(self, self.cursor, 'mjosa') # Get all markers @@ -46,6 +46,8 @@ class IceHTTP(BaseHTTPRequestHandler): if self.path == '/get_weather_data': get_weather(self) + elif self.path == '/new_lidar_data': + input_new_Lidar_data(self,self.cursor, 1, 'Mjosa') # hardcoded body of water must change later # Start a server on port 8443 using self defined HTTP class if __name__ == "__main__": diff --git a/server/map/__pycache__/get_markers.cpython-39.pyc b/server/map/__pycache__/get_markers.cpython-39.pyc index c1b95c053be539be6996efc2239388bcdada2a33..153eff491a414be4d1f89f774ec15cc0bcc92577 100644 Binary files a/server/map/__pycache__/get_markers.cpython-39.pyc and b/server/map/__pycache__/get_markers.cpython-39.pyc differ diff --git a/server/map/__pycache__/input_new_data.cpython-311.pyc b/server/map/__pycache__/input_new_data.cpython-311.pyc index bd6d9570fddc887d26dcac27986f9c6d168eb713..7f959582f8d78d8341fb2f14b0c7bfc033a99259 100644 Binary files a/server/map/__pycache__/input_new_data.cpython-311.pyc and b/server/map/__pycache__/input_new_data.cpython-311.pyc differ diff --git a/server/map/__pycache__/input_new_data.cpython-39.pyc b/server/map/__pycache__/input_new_data.cpython-39.pyc index 0bd02d71bf69b6ab2b4ebf9ed26a4e027128f49a..895a95e645fe34909fee7a3eabf48363211cab2d 100644 Binary files a/server/map/__pycache__/input_new_data.cpython-39.pyc and b/server/map/__pycache__/input_new_data.cpython-39.pyc differ diff --git a/server/map/input_new_data.py b/server/map/input_new_data.py index 906b358bbe64f53f26d8e45643f7130a6956a9a5..516c4fb8c95f1fa3d6d31d662fabe34ef6a5df5a 100644 --- a/server/map/input_new_data.py +++ b/server/map/input_new_data.py @@ -1,62 +1,68 @@ import json from datetime import datetime -from server.data_processing.process_lidar_data import height_in_area - +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) VALUES - (?,?,?,?); - ''', (sensorId, datetime.utcnow().replace(microsecond=0), bodyOfWater, 0)) + 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 - # input the newly generated measurement_id and ... average thickness (soon to be implemented) - cursor.execute(''' - UPDATE Measurement - SET measurementID = ? - WHERE MeasurementID IS NULL; - ''', (int(measurement_id),)) + # calculate the area of to be calculated based on the coordinates given to the calculation model + areas_data = calculate_area_data((latitude, longitude)) - # soon to be removed - cursor.execute(''' - SELECT CenterLatitude, CenterLongitude, SubDivisionID, GroupID - FROM SubDivision - GROUP BY CenterLatitude, CenterLongitude - ''') - position_data = cursor.fetchall() - - # soon to be removed - if (position_data): - for row in position_data: - latitude, longitude, subID, groupID = row - heights = height_in_area((latitude, longitude)) - if (len(heights) > 0): - print(heights) + + 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: - print("No height found") - average = sum(heights) / len(heights) + 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, subID, groupID, float(min(heights)), float(average), float(latitude), float(longitude), - float(1))) + ''',(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') + print('No data found') # send the changes to the database cursor.connection.commit() - print("suc_ceed") + # 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() + cursor.connection.rollback() \ No newline at end of file