From 720ed4d084f776c28cdf170280af585f038bdad7 Mon Sep 17 00:00:00 2001
From: Hoa Ben The Nguyen <hbnguye@stud.ntnu.no>
Date: Tue, 19 Mar 2024 17:46:18 +0100
Subject: [PATCH] change: adapt to json data

---
 server/data_processing/process_lidar_data.py | 31 ++++++++++++++++----
 server/main.py                               |  2 +-
 server/map/input_new_data.py                 |  5 ++--
 3 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/server/data_processing/process_lidar_data.py b/server/data_processing/process_lidar_data.py
index 97f0e1bd..278624c9 100644
--- a/server/data_processing/process_lidar_data.py
+++ b/server/data_processing/process_lidar_data.py
@@ -1,5 +1,6 @@
 import numpy as np
 import laspy
+import json
 import utm # src: https://github.com/Turbo87/utm
 from itertools import groupby
 from server.data_processing.area_processing import calculate_corners, define_gridareas
@@ -31,7 +32,7 @@ def about_laz_file():
 # 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 (abs(areaRange[0][1]) > abs(y) > abs(areaRange[1][1])):
         return True
     else:
         return False
@@ -64,12 +65,31 @@ def find_height(points):
 
 # 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 calculate_area_data(center):
+def calculate_area_data(center, cell_size, body_of_water):
+    # map application size to meters
+    cell_size_to_meters = cell_size * 600000
+
     # container for all the heights in area
     area_heights = []
 
     # set the limit of the area compared to local coordinates
-    area_limit = calculate_corners(center[0],center[1], 1500)
+    area_limit = calculate_corners(center[0],center[1], cell_size_to_meters)
+
+    # read json data with path to data for specific water body
+    file_name = "server/lake_relations/" + body_of_water + "_div.json"
+    with open(file_name) as data:
+        map_data = json.load(data)
+
+    map_data = map_data['features']
+    print(map_data[0]['properties']['sub_div_center'][0])
+
+    map_zones = [area_limit[1],area_limit[3]]
+
+    print(map_zones)
+
+    map_data = list(filter(lambda point_position: inArea((point_position['properties']['sub_div_center'][0][1], point_position['properties']['sub_div_center'][0][0],0.0), map_zones), map_data))
+
+    print(len(map_data))
 
     # Refactor lidar data to a readable format
     iceOver = laspy.read(lazData_path[0])
@@ -81,7 +101,7 @@ def calculate_area_data(center):
     min_point = min(ice_points)
 
     # define all the sub-areas within the area, local coordinates
-    grid_area_heights = define_gridareas(60,10, 1500,4)
+    grid_area_heights = define_gridareas(60,10, cell_size_to_meters,4)
 
     # find the heights of each sub-area => area-heights
     for sub_area in grid_area_heights:
@@ -101,5 +121,6 @@ def calculate_area_data(center):
 
     return area_heights
 
-
+print(calculate_area_data((61,11), 0.04,'mjosa'))
+print(abs(-69.9))
 
diff --git a/server/main.py b/server/main.py
index fd50247f..6a2e215d 100644
--- a/server/main.py
+++ b/server/main.py
@@ -48,7 +48,7 @@ class IceHTTP(BaseHTTPRequestHandler):
             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
+            input_new_Lidar_data(self,self.cursor, '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/input_new_data.py b/server/map/input_new_data.py
index d0a8c52b..af4b49e6 100644
--- a/server/map/input_new_data.py
+++ b/server/map/input_new_data.py
@@ -3,11 +3,12 @@ from datetime import datetime
 from server.data_processing.process_lidar_data import calculate_area_data, about_laz_file
 
 # 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):
+def input_new_Lidar_data(self, cursor, bodyOfWater):
     try:
         # hard coded coordinates
         latitude = 60.0
         longitude = 10.0
+        sensorId = 1
 
         total_measurement_average = 0  # the total average of a measurement
 
@@ -27,7 +28,7 @@ def input_new_Lidar_data(self, cursor, sensorId, bodyOfWater):
         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))
+        areas_data = calculate_area_data((latitude, longitude), 0.04, bodyOfWater)
 
 
         if(areas_data):
-- 
GitLab