From 616f4a3cba5bfdb59ffad2a0b9fd5dae74a1e7fb Mon Sep 17 00:00:00 2001
From: Sara <sarasdj@stud.ntnu.no>
Date: Thu, 4 Apr 2024 15:56:47 +0200
Subject: [PATCH] add: /get_all_lakes endpoint

---
 app/lib/consts.dart                           |   2 +
 app/lib/utils/custom_search_delegate.dart     |  17 +---
 server/main.py                                |  11 +++
 .../__pycache__/process_lake.cpython-311.pyc  | Bin 4689 -> 1994 bytes
 .../lake_relations/added_lakes.txt            |   1 -
 .../lake_relations/all_lake_names.json        |  10 +++
 .../lake_relations/all_system_relations.txt   |   2 -
 server/map_handler/process_lake.py            |  77 +-----------------
 8 files changed, 28 insertions(+), 92 deletions(-)
 delete mode 100644 server/map_handler/lake_relations/added_lakes.txt
 create mode 100644 server/map_handler/lake_relations/all_lake_names.json
 delete mode 100644 server/map_handler/lake_relations/all_system_relations.txt

diff --git a/app/lib/consts.dart b/app/lib/consts.dart
index 97d7ba83..13b7ca3b 100644
--- a/app/lib/consts.dart
+++ b/app/lib/consts.dart
@@ -17,6 +17,8 @@ List<Measurement> selectedMarkerList = [];
 LatLng mapCenter = LatLng(60.8000, 10.8471);
 DateTime ?lastUpdate; // Last time data was fetched from server
 
+List<String> lakeSearchOptions = []; // Init empty
+
 // Font settings
 const textColor = Colors.white;
 final appTitleStyle = GoogleFonts.chakraPetch(
diff --git a/app/lib/utils/custom_search_delegate.dart b/app/lib/utils/custom_search_delegate.dart
index 19366184..13c62cf9 100644
--- a/app/lib/utils/custom_search_delegate.dart
+++ b/app/lib/utils/custom_search_delegate.dart
@@ -1,6 +1,8 @@
 import 'package:fuzzy/fuzzy.dart';
 import 'package:flutter/material.dart';
 
+import '../consts.dart';
+
 typedef SearchResultCallback = void Function(String result);
 
 class CustomSearchDelegate extends SearchDelegate {
@@ -8,17 +10,6 @@ class CustomSearchDelegate extends SearchDelegate {
 
   CustomSearchDelegate(this.onResultSelected);
 
-  List<String> searchItems = [ // NB temp values
-    "Mjøsa",
-    "Bogstadsvannet",
-    "Einavatnet",
-    "Femsjøen",
-    "Femunden",
-    "Fjellsjøen",
-    "Gjende",
-    "Gjersjøen"
-  ];
-
   @override
   List<Widget> buildActions(BuildContext context) {
     return [
@@ -45,7 +36,7 @@ class CustomSearchDelegate extends SearchDelegate {
   Widget buildResults(BuildContext context) {
     List<String> searchResults = [];
     final options = FuzzyOptions(threshold: 0.4, findAllMatches: true);
-    final matcher = Fuzzy(searchItems, options: options);
+    final matcher = Fuzzy(lakeSearchOptions, options: options);
     final results = matcher.search(query);
     searchResults = results.map((result) => result.item as String).toList();
 
@@ -70,7 +61,7 @@ class CustomSearchDelegate extends SearchDelegate {
   Widget buildSuggestions(BuildContext context) {
     List<String> searchResults = [];
     final options = FuzzyOptions(threshold: 0.4, findAllMatches: true);
-    final matcher = Fuzzy(searchItems, options: options);
+    final matcher = Fuzzy(lakeSearchOptions, options: options);
     final results = matcher.search(query);
     searchResults = results.map((result) => result.item as String).toList();
 
diff --git a/server/main.py b/server/main.py
index f730b9e9..820e58ac 100644
--- a/server/main.py
+++ b/server/main.py
@@ -7,6 +7,7 @@ from map_handler.process_lake import fetch_divided_map
 from map_handler.input_new_data import input_new_Lidar_data
 from urllib.parse import urlparse, parse_qs
 import ssl
+import json
 import sqlite3
 
 app = Flask(__name__)
@@ -38,7 +39,17 @@ class IceHTTP(BaseHTTPRequestHandler):
             self.end_headers()
 
             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:
+                lake_names = json.load(file)
 
+            json_data = json.dumps(lake_names, ensure_ascii=False)
+
+            self.send_response(200)
+            self.send_header('Content-type', 'application/json')
+            self.end_headers()
+
+            self.wfile.write(json_data.encode('iso-8859-1'))
         elif self.path.startswith('/update_map'):  # NB: should be POST?
             parsed_path = urlparse(self.path)
             query_params = parse_qs(parsed_path.query)
diff --git a/server/map_handler/__pycache__/process_lake.cpython-311.pyc b/server/map_handler/__pycache__/process_lake.cpython-311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=u#KU-KN)~>U~
z&=*6pb4qbeRRGf#VGe_1O7QZYy}|<T;LEQ5lC7fE1l&aRhh7Yg?Dr_6SCr9@<owVz
zW$2pp-t~%J>vxkR5n(Znv{wKTQtME~1^gcz_{Ph=ap}T!>DN&5jmtiy_)uYigy<hC
zO+WfsVd2m_P-=eUE?NuoZ@!-*LBEbLpy%wvTP54Uz+`z~QXZI62BzfhOG@`8iFFgs
zrcSBzgZ=!RH2IOtUQ^g>QvG@Jt)-Pjp&x08YJG)Dqmn&D9+@3h*kS2i&l}PzTC$dW
zIr^{pyE*TiA3XP)uUNIuhaFdp+OLdy(hu6Ix61Cn3;{i!C5F2u<Iqp=B<bN9DrlYK
wVJhggS-*Z)a&Rq!!uXeDjNY5vA9sIYOpN<x8D^x|QUUZ^iqyXUUshrN3y!d`>i_@%

diff --git a/server/map_handler/lake_relations/added_lakes.txt b/server/map_handler/lake_relations/added_lakes.txt
deleted file mode 100644
index b10ac55a..00000000
--- a/server/map_handler/lake_relations/added_lakes.txt
+++ /dev/null
@@ -1 +0,0 @@
-mjosa
\ No newline at end of file
diff --git a/server/map_handler/lake_relations/all_lake_names.json b/server/map_handler/lake_relations/all_lake_names.json
new file mode 100644
index 00000000..33022b5c
--- /dev/null
+++ b/server/map_handler/lake_relations/all_lake_names.json
@@ -0,0 +1,10 @@
+[
+  "Mjøsa",
+  "Bogstadsvannet",
+  "Einavatnet",
+  "Femsjøen",
+  "Femunden",
+  "Fjellsjøen",
+  "Gjende",
+  "Gjersjøen"
+]
diff --git a/server/map_handler/lake_relations/all_system_relations.txt b/server/map_handler/lake_relations/all_system_relations.txt
deleted file mode 100644
index 6a20bd4a..00000000
--- a/server/map_handler/lake_relations/all_system_relations.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-Mjøsa - mjosa
-TestLake - testlake
\ No newline at end of file
diff --git a/server/map_handler/process_lake.py b/server/map_handler/process_lake.py
index dd0d202b..bf14018a 100644
--- a/server/map_handler/process_lake.py
+++ b/server/map_handler/process_lake.py
@@ -34,79 +34,4 @@ def get_ids_and_centers(file_name):  # NB buggy
             'sub_div_center': sub_div_center
         }
         subdivisions.append(subdivision)
-    return subdivisions
-
-# Create groups creates polygons which consist of groupings of related subdivisions
-def create_groups(relation_file: str, data: list):
-    try:
-        print("Creating groups...")
-
-        # Read lake relation from json file
-        geo_data = gpd.read_file("server/lake_relations/" + relation_file + "_div.json")
-        relation_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
-
-        # Loop through each measurement and create groupings of subdivisions
-        for measurement in data:
-            subdiv_list = []
-
-            for subdivision in measurement['Subdivisions']:
-                subDivID = str(subdivision['SubdivID'])  # Convert to string to match format in feature
-                group_id = subdivision['GroupID']  # Extract group ID
-
-                # Find the matching subdivision in relation_data
-                for index, feature in relation_data.iterrows():
-                    # Add the new group ID to the correct subdivision
-                    if feature['sub_div_id'] == subDivID:
-                        subdiv_list.append((group_id, Polygon(feature['geometry'].exterior.coords)))
-                        # subdiv_list.append((group_id, Polygon([feature['geometry']])))
-
-            # Sort subdiv_list based on group_ids
-            sorted_list = sorted(subdiv_list, key=lambda x: x[0])
-
-            current_group = -1  # Current group_id
-            new_shape = []      # List of subdivision geometries for current group
-
-            # Merge subdivisions in a given group
-            for element in sorted_list:
-                # If the subdivision still belongs to the current group
-                if element[0] == current_group:
-                    new_shape.append(element[1])
-
-                # New group id is found
-                elif len(new_shape) > 1:
-                    # Merger all subdivisions for previous group into a single shape
-                    merged_polygon = MultiPolygon(new_shape).buffer(0)
-
-                    # Convert to Polygon
-                    if isinstance(merged_polygon, MultiPolygon):
-                        merged_polygon = merged_polygon.convex_hull
-
-                    # Structure the new polygon
-                    merged_polygon_structure = {
-                        "type": "Feature",
-                        "properties": {
-                            "group_id": current_group,
-                        },
-                        "geometry": {
-                            "type": "Polygon",
-                            "coordinates": [list(merged_polygon.exterior.coords)]
-                        }
-                    }
-
-                    # Append new polygon to relation data
-                    relation_data = relation_data.append(merged_polygon_structure, ignore_index=True)
-
-                    # Update current group to new group_id and reset new_shape for next group
-                    current_group = element[0]
-                    new_shape = [element[1]]
-
-        # Convert GeoDataFrame to JSON
-        relation_data_json = json.loads(relation_data.to_json())
-
-        # Write relation with group shapes to file
-        write_json_to_file("server/lake_relations", "mjosa", relation_data_json)
-
-    except Exception as e:
-        print(f"Error in create_groups(): {e}")
-
-
+    return subdivisions
\ No newline at end of file
-- 
GitLab