From c85dfbde6235fa823153c9737efce93637da2dd8 Mon Sep 17 00:00:00 2001
From: Sara <sarasdj@stud.ntnu.no>
Date: Wed, 20 Mar 2024 12:21:49 +0100
Subject: [PATCH] update: split cell_size into height and width

---
 app/lib/widgets/choropleth_map.dart |  1 -
 server/main.py                      |  6 +++---
 server/map/get_markers.py           |  7 ++++---
 server/map/get_relation.py          | 18 ++++++++++--------
 4 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/app/lib/widgets/choropleth_map.dart b/app/lib/widgets/choropleth_map.dart
index c8455d93..56452604 100644
--- a/app/lib/widgets/choropleth_map.dart
+++ b/app/lib/widgets/choropleth_map.dart
@@ -1,4 +1,3 @@
-import 'dart:math';
 import 'dart:typed_data';
 import 'package:flutter/material.dart';
 import 'package:syncfusion_flutter_maps/maps.dart';
diff --git a/server/main.py b/server/main.py
index aeef2812..2295e3eb 100644
--- a/server/main.py
+++ b/server/main.py
@@ -2,7 +2,7 @@ from flask import Flask
 from http.server import HTTPServer, BaseHTTPRequestHandler
 from consts import SSL_CERT_PATH, SSL_KEY_PATH, HOST, PORT
 from map.get_markers import get_all_markers
-from map.get_relation import get_relation
+from map.get_relation import cut_map
 from APIs.get_weather import get_weather
 import ssl
 import sqlite3
@@ -37,10 +37,10 @@ class IceHTTP(BaseHTTPRequestHandler):
             self.wfile.write(b"The root path provides no functionality. Please use a valid endpoint")
 
         elif self.path == '/update_map':  # NB: should be POST?
-            get_all_markers(self, self.cursor, 'Mjosa')  # Get all markers
+            get_all_markers(self, self.cursor, 'mjosa')  # Get all markers
             # NB: temporary hardcoded waterBodyName
         elif self.path == '/get_relation':
-            get_relation(self, 'Mjosa')  # NB temp hardcoded value
+            cut_map(self, 'Mjosa')  # NB temp hardcoded value
 
     def do_POST(self):
         if self.path == '/get_weather_data':
diff --git a/server/map/get_markers.py b/server/map/get_markers.py
index 799f723a..7288d2f0 100644
--- a/server/map/get_markers.py
+++ b/server/map/get_markers.py
@@ -1,4 +1,5 @@
 import json
+from get_relation import cut_map
 
 
 # get_markers requests all marker data or valid markers, converts the data to json, and writes
@@ -87,11 +88,11 @@ def get_all_markers(self, cursor, waterBodyName):
 
 
 def calculateColor(thickness: int):  # NB not final colors nor ranges
-    if 0 < thickness < 4:
+    if 0 < thickness <= 4:
         return 0xFFff0000  # Red
-    elif 4 < thickness < 6:
+    elif 4 < thickness <= 6:
         return 0xFFff9400  # Orange
-    elif 6 < thickness < 8:
+    elif 6 < thickness <= 8:
         return 0xFFb1ff00  # Green
     elif thickness > 8:
         return 0xFF00d6ff  # Blue
diff --git a/server/map/get_relation.py b/server/map/get_relation.py
index 9f175ee8..a2f455bc 100644
--- a/server/map/get_relation.py
+++ b/server/map/get_relation.py
@@ -7,9 +7,9 @@ import json
 import os
 
 # Read a json file with relation data and send to response object
-def get_relation(self, body_of_water: str):  # NB: implement body_of_water
+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/mjosa.geojson")
+    geo_data = gpd.read_file("server/map/" + body_of_water + ".json")
     polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
     polygons = [Polygon(polygon.exterior) for polygon in polygon_data['geometry']]
 
@@ -19,8 +19,9 @@ def get_relation(self, body_of_water: str):  # NB: implement body_of_water
     divided_map = []
 
     for polygon in polygons:
-        cell_size = 0.04
-        lines = create_grid(polygon, cell_size)
+        cell_width = 0.04
+        cell_height = 0.04
+        lines = create_grid(polygon, cell_width, cell_height)
         lines.append(polygon.boundary)
         lines = unary_union(lines)
         lines = linemerge(lines)
@@ -74,7 +75,8 @@ def get_relation(self, body_of_water: str):  # NB: implement body_of_water
 
     feature_collection = {
         'type': 'FeatureCollection',
-        'features': features
+        'features': features,
+        'tile_count': sub_div_id, # Add the last subdivision ID as number of tiles
     }
 
     self.send_response(200)
@@ -84,7 +86,7 @@ def get_relation(self, body_of_water: str):  # NB: implement body_of_water
     self.wfile.write(json.dumps(feature_collection).encode('utf-8'))
 
 
-def create_grid(poly: Polygon, cell_size):
+def create_grid(poly: Polygon, cell_width, cell_height):
     # Retrieve bounds of the entire polygon
     bounds = poly.bounds
 
@@ -96,14 +98,14 @@ def create_grid(poly: Polygon, cell_size):
     while y <= max_y:
         line = LineString([(min_x, y), (max_x, y)])
         grid_lines.append(line)
-        y += cell_size
+        y += cell_height
 
     # Vertical lines
     x = min_x
     while x <= max_x:
         line = LineString([(x, min_y), (x, max_y)])
         grid_lines.append(line)
-        x += cell_size
+        x += cell_width
 
     return grid_lines
 
-- 
GitLab