From a323d232177ca0d8eae199b1026a06b7a64fad34 Mon Sep 17 00:00:00 2001
From: Hoa Ben The Nguyen <hbnguye@stud.ntnu.no>
Date: Thu, 21 Mar 2024 14:58:35 +0100
Subject: [PATCH] fix: structure for x and y and lat and long

---
 server/data_processing/area_processing.py    |  6 ++--
 server/data_processing/process_lidar_data.py | 35 +++++++++++---------
 server/map/input_new_data.py                 |  4 +--
 3 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/server/data_processing/area_processing.py b/server/data_processing/area_processing.py
index 9c7bd0d0..649e18a6 100644
--- a/server/data_processing/area_processing.py
+++ b/server/data_processing/area_processing.py
@@ -44,9 +44,9 @@ def define_gridareas(lat, lng, area_offset, grid_size):
     i=0
     # find the center coordinates of each area in grid to find the corner areas
     for y in (range(grid_size)):
-        relative_size_lng = y / grid_size
+        relative_size_lat = y / grid_size
         for x in (range(grid_size)):
-            relative_size_lat = x / grid_size
+            relative_size_lng = x / grid_size
             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
@@ -64,5 +64,5 @@ def define_gridareas(lat, lng, area_offset, grid_size):
 
     return grided_area
 #[1,2,2,3,4,5,6,3,4,6,8,9,5,3,5.7,8,5,3]
-#print(define_gridareas(-60,10,10000,4))
+#print(define_gridareas(60,10,1500,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 a1f3968f..51d927cd 100644
--- a/server/data_processing/process_lidar_data.py
+++ b/server/data_processing/process_lidar_data.py
@@ -32,25 +32,27 @@ 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 (abs(areaRange[0][1]) > abs(y) > abs(areaRange[1][1])):
+    y, x, _ = position
+    if (abs(areaRange[0][0]) > x > abs(areaRange[1][0])) and (abs(areaRange[0][1]) > abs(y) > abs(areaRange[1][1])):
         return True
     else:
         return False
 
 # find distance between two points
 def distance(point1, point2):
-    x1, y1 = point1
-    x2, y2 = point2
-    return math.sqrt(abs(x2 - x1)**2 + abs(y2 - y1)**2)
+    y1, x1 = point1
+    y2, x2 = point2
+    print(y1," 2 ",x1)
+    print(y2," 3 ",x2)
+    return math.sqrt(abs(y2 - y1)**2 + abs(x2 - x1)**2)
 
 # find the closest point in json list
 def closest_points(point, list, coords_taken):
+    print(point, " 4 ")
     closest_point = None
     closest_dist = float('inf')
     for current_point in list:
-        lng, lat = current_point['properties']['sub_div_center'][0]
-        dist = distance(point, (lat, lng))
+        dist = distance(point, current_point['properties']['sub_div_center'][0])
         if dist < closest_dist and current_point not in coords_taken:
             closest_dist = dist
             closest_point = current_point
@@ -58,6 +60,7 @@ def closest_points(point, list, coords_taken):
 
 # Calculation of height in an area from las files
 def find_height(points):
+    print(points, " 5 ")
     height_differences = [] # final container for height data
 
     # sort the points
@@ -102,13 +105,13 @@ def calculate_area_data(center, cell_size, body_of_water):
         map_data = json.load(data)
 
     map_data = map_data['features']
-    print(map_data[0]['properties']['sub_div_center'][0])
+    print(map_data[0]['properties']['sub_div_center'])
 
     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))
+    map_data = list(filter(lambda point_position: inArea((point_position['properties']['sub_div_center'][0], point_position['properties']['sub_div_center'][1],0.0), map_zones), map_data))
 
     print(len(map_data))
 
@@ -126,8 +129,9 @@ def calculate_area_data(center, cell_size, body_of_water):
 
     # find the heights of each sub-area => area-heights
     for sub_area in grid_area_heights:
-        start = min(sub_area[2])
-        end = max(sub_area[2])
+        print(sub_area)
+        start = min(sub_area[1])
+        end = max(sub_area[1])
 
         #test data
         # zone coordinates sett to be relative to the lidar data
@@ -137,12 +141,12 @@ def calculate_area_data(center, cell_size, body_of_water):
                      (((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]))]
 
         # calculate map zones height
-        xs, ys = start
-        xe, ye = end
+        ys, xs = start
+        ye, xe = end
         sub_center = ((xs + xe)/2, (ys + ye)/2)
         if len(map_data) > 0:
             current_map_zone = closest_points(sub_center, map_data, taken_coords)
-            taken_coords.append((current_map_zone['properties']['sub_div_center'][0][1], current_map_zone['properties']['sub_div_center'][0][0]))
+            taken_coords.append((current_map_zone['properties']['sub_div_center'][0][0], current_map_zone['properties']['sub_div_center'][0][1]))
         else:
             return [0]
 
@@ -155,4 +159,5 @@ def calculate_area_data(center, cell_size, body_of_water):
 
     return area_heights
 
-# print(calculate_area_data((61,11), 0.04,'mjosa'))
\ No newline at end of file
+#print(calculate_area_data((61,11), 0.04,'mjosa'))
+
diff --git a/server/map/input_new_data.py b/server/map/input_new_data.py
index e66437df..0c5275e4 100644
--- a/server/map/input_new_data.py
+++ b/server/map/input_new_data.py
@@ -37,7 +37,7 @@ def input_new_Lidar_data(self, cursor, bodyOfWater):
             # calculate data for each zone within the area
             for area in areas_data:
                 # lng and lat relative to map
-                map_lng, map_lat = area[2]
+                map_lat, map_lng = area[2]
 
                 if(len(area[2]) != 0):
                     average = sum(area[2])/len(area[2])
@@ -101,4 +101,4 @@ def input_new_Lidar_data(self, cursor, bodyOfWater):
     except Exception as e:
         print("An error occurred", e)
         # rollback in case of error
-        cursor.connection.rollback()
\ No newline at end of file
+        cursor.connection.rollback()
-- 
GitLab