From c5eb24d71e5a91c60b322191091a9f94617e319c Mon Sep 17 00:00:00 2001
From: Sara <sarasdj@stud.ntnu.no>
Date: Thu, 7 Mar 2024 11:14:10 +0100
Subject: [PATCH] fix: shapeDataField value

---
 app/lib/pages/widgets/cloropleth_map.dart     |   2 +-
 .../__pycache__/get_relation.cpython-311.pyc  | Bin 3646 -> 3044 bytes
 server/map/get_relation.py                    |  48 +++++++-----------
 3 files changed, 18 insertions(+), 32 deletions(-)

diff --git a/app/lib/pages/widgets/cloropleth_map.dart b/app/lib/pages/widgets/cloropleth_map.dart
index 9f98e9a4..ea67a65e 100644
--- a/app/lib/pages/widgets/cloropleth_map.dart
+++ b/app/lib/pages/widgets/cloropleth_map.dart
@@ -45,7 +45,7 @@ class _ChoroplethMapState extends State<ChoroplethMap> {
           MapShapeLayer(
             source: MapShapeSource.memory(
               widget.relation,
-              shapeDataField: 'SubDivID',
+              shapeDataField: 'geometry',
             ),
             color: Colors.orange,
             zoomPanBehavior: _zoomPanBehavior,
diff --git a/server/map/__pycache__/get_relation.cpython-311.pyc b/server/map/__pycache__/get_relation.cpython-311.pyc
index 62331622ce7b3cf90124995d38175c5c4871b726..58dc7c0fd6a0e49edaa4dba3c77344d838746358 100644
GIT binary patch
delta 929
zcmZ8fUr19?7(eI!IlFh;{ns|9sj2C-nV}Lzs1&6+Ei1zQv6nHuSDDT&?wtnn4k!2^
z^x`-{KKQVwdPpMZUFxCtO$LrZU=TrXH=>Ao>D+50>hPW4_xt|vefNCd-D~+66rT%%
z8<2Abf7Z_P1+lTOk62HE3RIW`QAjkEq?YI?4S`B)%p9dM8%&f{*$ogyg_#lR6D$<>
zaW9lnjy35b!t2n7Bb0=4$OJ`~V}5}RkaJiw)d=}NDSpmrP5R+MD#)0q=(QuS3)EQ%
z1^kX)Cm3N)HA!k>C826DT}#F@iFB&w%A{ipMmmKbGiia%B$Ap@b2|zvY{=6^O0wh{
zKLEbWraBN1TDB+3-cZFG%3rk*ICkPd3GFMPveHvgdh*wB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04m(e%ua&q+~oIVbx
zk0yHIX)`?CV`fZd#uy5+**N?Kxw)2yzDJP*X`JqoY4nt<LkhjlkB6xm_Uf?40^nGj
zF3A-7j33AG9sX0SdwjY<-E-=!`l=qnG8poO%0jRK>Rz4Bg4N}^uj(7-VSb;?jlP8E
zce5!Dofq!NFY~gn3v_=L9O9Hq&xUzm17b4qoJ@8d(Df1Bi=Ob&xsg|CAlSRDd+&g>
zbbDhb^y7zr)ON3{WLqk!MVhjNJNLiH79QNlsuJ23-eO1tp(j+laTObn@o{vp8mL1Z
z%HVuOjNm!COSP%JAYOxVNC)(!`LKh$3>LdyE~~OeR!pT-(p4&{VQpM4JSfP@nyM7@
z)k3*+b-q|$&lk1#uz|j!g_R|orCr9V35>+efjn0_O85PPFOS%x{11XL_Rh<Q>LtvR
zSfy!;scu&kOLA5&l=4+Y8$?{_xp3ueizybgs!D2aaf98Z1L`<|Vzp3I*0e%Rv8aM<
zu>@+!FD_2G9COgy46)TI@&|XNT-@Hky-gZi3t(3&l|p$j^seQ*a6okdN67}Yzi~4)
z!I?kQywMk3&4s?(+x*}x7`MR)8*>yo6xZU|6vbF;@idy~th9F??btx@U?)~6<vM6w
zdNFE@En?~Uv!*|5__KX~U_amH_o;o#9wE6jCio`;1gVvISlg}bfBbc$**FzEhIh6n
zWK1Dr2pO9Lflza)?+@F6XD1!iVooEkb!NNb!F4k}W5%!bB6DVBt|gs?#`?}>yx1w=
z+V>JSO$_6=`msbOXT~ONo{70^fK|(FyMP!x5k`-N(auy)m@tJ2!x6lqJ7O?l=g!hD
zH9{&W;;%#{Uy}2hIzliv02Wn|(VR5KkuQb1fIgST8XsYkypP%zRyGYG>;tLk{Q4kj
k45bf(#!&j;4dYd@xg?zJuG;`l7u5osgb^G3UpYJe5AQU7kN^Mx

diff --git a/server/map/get_relation.py b/server/map/get_relation.py
index 0514bbc1..38cb963f 100644
--- a/server/map/get_relation.py
+++ b/server/map/get_relation.py
@@ -1,6 +1,4 @@
-import json
 import geopandas as gpd
-import pandas as pd
 from shapely.geometry import Polygon
 
 
@@ -8,25 +6,21 @@ def get_relation(self, body_of_water: str):
     # Load GeoJSON data using geopandas
     geo_data = gpd.read_file("server/map/mjosa.geojson")
 
-    # Filter only polygons, exclude points and other feature types
+    # Filter only polygons, exclude points and other feature types to reduce response size
     polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
 
     # Extract coordinates from polygons and create polygon objects
     polygons = [Polygon(polygon.exterior) for polygon in polygon_data['geometry']]
 
-    if len(polygons) <= 1:
+    if len(polygons) <= 1:  # Return if conversion to polygon fails
         print("Failed to convert to polygons")
         return
 
-    # Divide relation into tiles and append tiles to relation object
-    # tiles = divide_relation(polygons)
-    # tiles_dicts = [{"geometry": tile["polygon"], "SubDivID": tile["SubDivID"]} for tile in tiles]
-    # tiles_gdf = gpd.GeoDataFrame(tiles_dicts)
-    # combined_data = pd.concat([polygon_data, tiles_gdf], ignore_index=True)
+    # Divide relation into tiles
+    tiles = divide_relation(polygons)
 
-    # Parse geojson to json -> syncfusion_flutter_maps lib requires json
-    geojson_dict = json.loads(polygon_data.to_json())
-    response_json = json.dumps(geojson_dict)
+    # Convert GeoDataFrame to GeoJSON
+    tiles_json = polygon_data.to_json()
 
     # Set headers
     self.send_response(200)
@@ -34,13 +28,13 @@ def get_relation(self, body_of_water: str):
     self.end_headers()
 
     # Write coordinates to response object
-    self.wfile.write(response_json.encode('utf-8'))
+    self.wfile.write(tiles_json.encode('utf-8'))
 
 
 def divide_relation(polygons):
     # Define tile size
     tile_size = 0.01
-    id = 1
+    subdiv_id = 0
     tiles = []
 
     for polygon in polygons:
@@ -53,28 +47,20 @@ def divide_relation(polygons):
 
         for row in range(rows):
             for col in range(cols):
-                tile_bbox = Polygon([ # Calculate coordinate for current place in column and row
+                tile_bbox = Polygon([  # Calculate coordinate for current place in column and row
                     (x_min + col * tile_size, y_min + row * tile_size),
                     (x_min + (col + 1) * tile_size, y_min + row * tile_size),
                     (x_min + (col + 1) * tile_size, y_min + (row + 1) * tile_size),
                     (x_min + col * tile_size, y_min + (row + 1) * tile_size)
                 ])
-                tiles.append({"SubDivID": id, "polygon": tile_bbox})
-                id += 1
+                tiles.append({"SubDivID": subdiv_id, "geometry": tile_bbox})
+                subdiv_id += 1
 
-    if len(tiles) <= 1:
+    if len(tiles) <= 1:  # Return empty object if division fails
         print("Failed to divide polygons into tiles")
+        return []
 
-    # Format each tile object with coordinate, type, and subdivision id tags
-    tiles_json = []
-    for tile in tiles:
-        coordinates = list(tile["polygon"].exterior.coords)
-        polygon_json = {
-            "type": "Polygon",
-            "coordinates": [[(coord[0], coord[1]) for coord in coordinates]],
-            "SubDivID": tile["SubDivID"]
-        }
-        tiles_json.append(polygon_json)
-
-    # Return all tiles for current polygon
-    return tiles_json
+    # Convert tiles list to a GeoDataFrame
+    tiles_df = gpd.GeoDataFrame(tiles, geometry='geometry')
+
+    return tiles_df
-- 
GitLab