From f233ed60669ab733c5039cfeae824ea390c43fb9 Mon Sep 17 00:00:00 2001
From: Sara <sarasdj@stud.ntnu.no>
Date: Thu, 21 Mar 2024 09:39:09 +0100
Subject: [PATCH] update: merger relation and measurement response, server-side

---
 .../map/__pycache__/add_lake.cpython-311.pyc  | Bin 7007 -> 7096 bytes
 server/map/get_measurements.py                |  34 ++++++++++++++++--
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/server/map/__pycache__/add_lake.cpython-311.pyc b/server/map/__pycache__/add_lake.cpython-311.pyc
index 5791d4e3b431d3b5e9e3a8bfa6f60c3045aaf4dc..7409ca6202d63e1e08f7263c9d2268868485bda9 100644
GIT binary patch
delta 701
zcmYL`O=}ZD7{_NeyXht-Y0@;=P3)Vdt=(c9j6_=$v?%z}Tzo@Yp=-!aVACX9XI3k=
z_#Je<Ku%HcqKr_#fCq0Wc#wDyJbP3WymX$e(Ff+o|9P92XTF|!e>VOy9*-hv?E4?G
z_BOtRm2WDK8e^h{yCKtGfZU-uNhh^ybVCTe4slB>42^WL)R^DVj+;)}8SN_4mFv(j
z+C`ll-`7U@hLK2u+sF@+ouiPL^Opw!gc{=mEe{Dc9{8OJS)9QczLOl&F>=r?WM##X
za7dYWnqh=KCbZbBZW5c4X4QASHtTDxhR3Qrp)cS`eqR5O9@(a@PwWQs+P3f6b+<`g
zM1J8C{~ht7H{?k%x!9g~7%eHepE0BvJ!A3BP;Ys4U#)ynD?PRHS=Gg>*gIS<%2KKV
z<|V8cy$Sjf;4&b{UjcO$a1C&sKTnjx7D!8cOr#Win}118<xfFG1Hh!|Nq`QR=b6;Z
z<Xw>WB^cC9fzdR?76XX2)QYmYA$31Yp190sbPgQV+ahgxE|AWn)|RDGD55st0fcd#
z!3+LxScX(t{7A1W$EHEg0A?j*bI^AI0v7=*+{+elOYrOt#x*`{7H0po^mRFBO1*7H
z3DgE7(*N$j@0sh^=fd2>(|j}M72%4s4mb^fkD$Sj;C+47ace#eZq|Z~&m;M9{7jt4
GFX03GwU@sD

delta 644
zcmX|;Uu)A~6vp$WNl9Z<+qJ9fnA4F>8?@pIx`}f-@!y4WLm6mMVKfmVU9-J!hJqdV
z9nOv36$Ayn8n2+2iVpk?x$z5lp*Mnh<NIW;2lC@NC(k+LoP3!1cG`LEIJTzZ^W5K{
zwe8F@{i8uk!(;Sjs314k^Ni8c`c+yjRxH2xifP(gMiZu+DfvURVV3E(IZQ9i^1d2|
zAo`;)*W;Pp6nr$cr+Wu>N9^VOgD~rUnSK_>v%Ypq^D45D&FFN)YOrzYSv$0n`^Bc|
zx*gd|sx7Y`@lx*CP2K!a(9E1HI`7!26ROM&z*F$@d>MKHxCo^0FGF1ct^(KSccGcB
zL)PW={*r$7rqZ63*kLygxCq*@Nb&<6MqPdqW)nCL90M%Cqpi}^p%utG3Id+P2wq05
zkwShdE$El-DxGA5C!vT1KZ?YN)`KoTlS+ScZQV0CO#B{jA7%DHV7b9Sx{#~rJd}%-
zmOkOaHVI5AsOGfq918QmdHPy)Sx0`aK4ol`9*($21}%vds#SUPSnxKy>GiDt6KH#+
z#RTPR@0m+qYSAJ-k^4Xs!2jUs{jgtR_+dNdA&MP901i+EaEL$F+*;&sc7l1nC7+GX
FvVXqRiK_qr

diff --git a/server/map/get_measurements.py b/server/map/get_measurements.py
index e3f02a62..4199b487 100644
--- a/server/map/get_measurements.py
+++ b/server/map/get_measurements.py
@@ -1,7 +1,8 @@
 import json
 from datetime import datetime
 import random
-from random import randint
+import geopandas as gpd
+
 
 # get_markers requests all marker data or valid markers, converts the data to json, and writes
 # the data to the response object
@@ -108,8 +109,35 @@ def get_all_markers(self, cursor, waterBodyName):
         ########################### TEST DATA ###########################################
 
         # Convert dictionary values to list of measurements
-        data = list(measurement_data.values()) + test_measurements
+        measurements_list = list(measurement_data.values()) + test_measurements
+
+        ######################### ADD GROUP_IDS TO RELATION DATA ##################################
+
+        # Read lake relation from json file
+        geo_data = gpd.read_file("server/lake_relations/mjosa_div.json")
+        relation_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
+
+        # Add group IDs to lake relation
+        for measurement in measurements_list:
+            measurement_id = measurement['MeasurementID']  # Extract measurement ID
+            for subdivision in measurement['Subdivisions']:
+                group_id = subdivision['GroupID']  # Extract group ID
+                new_group_id = str(measurement_id) + "-" + str(subdivision['GroupID']) # Create concatenated group ID
+
+                # Find the matching subdivision in relation_data
+                for idx, feature in relation_data.iterrows():
+                    # Add the new group ID to the correct subdivision
+                    if feature['properties']['sub_div_id'] == subdivision['sub_div_id']:
+                        relation_data.at[idx, 'GroupID'] = new_group_id
+
+        ####################################################################################
+
+        # Convert GeoDataFrame to JSON
+        relation_data_json = json.loads(relation_data.to_json())
 
+        # Combine measurements_list and relation_data
+        data = [measurements_list, relation_data_json]
+        
         if len(rows) == 0 or len(data) == 0:  # Return 500 and empty list if no data is found
             print(f"No data which meets the condition found")
             marker_data = '[]'
@@ -126,7 +154,7 @@ def get_all_markers(self, cursor, waterBodyName):
     self.send_header("Content-type", "application/json")
     self.end_headers()
 
-    # Write marker data to response object
+    # Write both measurement data and relation data to the response object
     self.wfile.write(marker_data.encode('utf-8'))
 
 
-- 
GitLab