From 81b0bc998acaecf044790a0d918c5a7e98aa0b50 Mon Sep 17 00:00:00 2001
From: Sara <sarasdj@stud.ntnu.no>
Date: Tue, 9 Apr 2024 11:18:54 +0200
Subject: [PATCH] add: lake relations directory path to consts

---
 server/consts.py                   |  6 ++++--
 server/main.py                     |  8 +++-----
 server/map_handler/add_lake.py     |  5 +++--
 server/map_handler/process_lake.py | 19 ++-----------------
 4 files changed, 12 insertions(+), 26 deletions(-)

diff --git a/server/consts.py b/server/consts.py
index 6be29cfb..eaa96384 100644
--- a/server/consts.py
+++ b/server/consts.py
@@ -9,5 +9,7 @@ CERT_DIR = "server/certificates/"
 SSL_KEY_PATH = CERT_DIR + "testKey.key"
 SSL_CERT_PATH = CERT_DIR + "testCert.crt"
 
-# Measurement specs
-AREA_SIZE = 20
+# File paths
+MAP_HANDLER_PATH = "server/map_handler/"
+LAKE_RELATIONS_PATH = MAP_HANDLER_PATH + "lake_relations/"
+
diff --git a/server/main.py b/server/main.py
index c1410f9d..33097ee3 100644
--- a/server/main.py
+++ b/server/main.py
@@ -7,9 +7,10 @@ from http.server import HTTPServer, BaseHTTPRequestHandler
 from consts import SSL_CERT_PATH, SSL_KEY_PATH, HOST, PORT
 
 from map_handler.add_lake import cut_map
+from server.consts import LAKE_RELATIONS_PATH
+from map_handler.process_lake import get_divided_map
 from map_handler.get_measurements import get_all_markers
 from map_handler.input_new_data import input_new_Lidar_data
-from map_handler.process_lake import get_divided_map, get_ids_and_centers
 
 app = Flask(__name__)
 terminate_server = 0
@@ -42,7 +43,7 @@ class IceHTTP(BaseHTTPRequestHandler):
 
             self.wfile.write(b"Root path hit!")
         elif self.path == '/get_lake_names':
-            with open('server/map_handler/lake_relations/all_lake_names.json', 'r') as file:
+            with open(LAKE_RELATIONS_PATH + 'all_lake_names.json', 'r') as file:
                 lake_names = json.load(file)
 
             # Disable ensure_ascii to keep 'ø'
@@ -64,9 +65,6 @@ class IceHTTP(BaseHTTPRequestHandler):
             get_divided_map(self, 'Mjosa')  # NB temp hardcoded value
         elif self.path == '/divide_new_relation':
             cut_map(self, 'Mjosa')
-        elif self.path == '/test':
-            get_ids_and_centers('server/map_handler/lake_relations/mjosa_div.json')
-
     def do_POST(self):
         if self.path == '/new_lidar_data':
             input_new_Lidar_data(self, self.cursor, 1, 'Mjosa')  # hardcoded body of water must change later
diff --git a/server/map_handler/add_lake.py b/server/map_handler/add_lake.py
index 19a190fc..2a1ab71b 100644
--- a/server/map_handler/add_lake.py
+++ b/server/map_handler/add_lake.py
@@ -3,12 +3,13 @@ from shapely.geometry import Polygon, LineString, MultiLineString
 from shapely.ops import linemerge, unary_union, polygonize
 import json
 import os
+from server.consts import LAKE_RELATIONS_PATH
 
 
 # Read a json file with relation data and send to response object
 def cut_map(self, body_of_water: str):  # NB: implement body_of_water
     # Read relation from GeoJson file and extract all polygons
-    geo_data = gpd.read_file("server/map_handler/lake_relations/mjosa.geojson")
+    geo_data = gpd.read_file(LAKE_RELATIONS_PATH + "mjosa.geojson")
     polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
     polygons = [Polygon(polygon.exterior) for polygon in polygon_data['geometry']]
 
@@ -90,7 +91,7 @@ def cut_map(self, body_of_water: str):  # NB: implement body_of_water
 
     # NB currently hardcoded file path
     # Create the txt file specified by the path
-    with open("server/map_handler/lake_relations/mjosa_centers.txt", 'w') as file:
+    with open(LAKE_RELATIONS_PATH + "mjosa_centers.txt", 'w') as file:
         # Iterate over the list and write each element to the file
         for sub_div_id, x, y in sub_div_center_list:
             file.write(f"{sub_div_id}, {x}, {y}\n")  # Write each list entry on a new line
diff --git a/server/map_handler/process_lake.py b/server/map_handler/process_lake.py
index 904310a8..02c076a0 100644
--- a/server/map_handler/process_lake.py
+++ b/server/map_handler/process_lake.py
@@ -1,4 +1,4 @@
-import json
+from server.consts import LAKE_RELATIONS_PATH
 
 
 # Writes contents of a lake json file to the response
@@ -8,23 +8,8 @@ def get_divided_map(self, file_name):
     self.end_headers()
 
     # Extract contents from JSON file
-    with open("server/map_handler/lake_relations/" + file_name + "_div.json", "r") as file:
+    with open(LAKE_RELATIONS_PATH + file_name + "_div.json", "r") as file:
         data = file.read()
 
     # Write contents of the JSON file to response
     self.wfile.write(data.encode('utf-8'))
-
-
-# Returns a list of all sub_division ids and their center coordinates for a given lake
-def get_ids_and_centers(file_path):  # NB buggy
-    # Expected format: [(sub_div_id, [x,y]), (sub_div_id, [x,y]), ...]
-    return_list = []
-
-    f = open(file_path)
-    data = json.load(f)
-
-    for measurement in data['measurement']:
-        for sub_div in measurement['Subdivisions']:
-            return_list.append((sub_div['SubdivID'], (sub_div['CenLatitude'], sub_div['CenLongitude'])))
-
-    return return_list
-- 
GitLab