From 46cc40aa99f9efd80a305c80f1af4967dbdb2dd2 Mon Sep 17 00:00:00 2001
From: Hoa Ben The Nguyen <hbnguye@stud.ntnu.no>
Date: Thu, 21 Mar 2024 10:16:24 +0100
Subject: [PATCH] change: response json instead of only 200 ok

---
 server/data_processing/process_lidar_data.py |  3 +-
 server/map/input_new_data.py                 | 29 ++++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/server/data_processing/process_lidar_data.py b/server/data_processing/process_lidar_data.py
index af2588a7..1369d8db 100644
--- a/server/data_processing/process_lidar_data.py
+++ b/server/data_processing/process_lidar_data.py
@@ -143,7 +143,8 @@ def calculate_area_data(center, cell_size, body_of_water):
 
         # filter data within sub-area zones
         points_in_area = list(filter(lambda point_position: inArea(point_position, areazone), ice_points))
-        area_heights.append((current_zone_id,sub_area[0], sub_center, find_height(points_in_area)))
+        heights = find_height(points_in_area)
+        area_heights.append((current_zone_id, sub_area[0], sub_center, heights))
 
     return area_heights
 
diff --git a/server/map/input_new_data.py b/server/map/input_new_data.py
index c94a186a..36f0ea6e 100644
--- a/server/map/input_new_data.py
+++ b/server/map/input_new_data.py
@@ -30,7 +30,10 @@ def input_new_Lidar_data(self, cursor, bodyOfWater):
         # calculate the area of to be calculated based on the coordinates given to the calculation model
         areas_data = calculate_area_data((latitude, longitude), 0.04, bodyOfWater)
 
+        lidar_json_data = {}
+
         if(areas_data):
+            # 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
@@ -51,6 +54,17 @@ def input_new_Lidar_data(self, cursor, bodyOfWater):
                     (?,?,?,?,?,?,?,?);
                 ''',(measurement_id, area[0], area[1], float(minimum_thickness), float(average), float(map_lat), float(map_lng), float(1)))
 
+                # set up json formate
+                lidar_read = {
+                    'MeasurementId': measurement_id,
+                    'SubId': area[0],
+                    'GroupId': area[1],
+                    'SubCenter': (map_lat, map_lng),
+                    'Heights': area[2]
+                }
+
+                lidar_json_data.append(lidar_read)
+
             total_measurement_average = total_measurement_average / len(areas_data)
 
             # input the newly generated measurement_id and whole average thickness
@@ -59,6 +73,7 @@ def input_new_Lidar_data(self, cursor, bodyOfWater):
                 SET measurementID = ?, WholeAverageThickness = ?
                 WHERE MeasurementID IS NULL AND WholeAverageThickness = 0;
             ''', (int(measurement_id), total_measurement_average), )
+
         else:
             print('No data found')
 
@@ -67,6 +82,20 @@ def input_new_Lidar_data(self, cursor, bodyOfWater):
 
         # Send response
         self.send_response(200)
+        self.send_header('Content-type', "application/json")
+        self.end_headers()
+
+        content = None
+        if len(lidar_json_data) > 0:
+            # convert list of lidar data to json
+            content = json.dumps(lidar_json_data)
+        else:
+            print('No data found')
+            content = []
+
+        # Write content data to response object
+        self.wfile.write(content.encode('utf-8'))
+
 
     # error handling
     except Exception as e:
-- 
GitLab