From 4546ff39d2f63b60f7e060b9311ce694042be14f Mon Sep 17 00:00:00 2001
From: Sara <sarasdj@stud.ntnu.no>
Date: Wed, 13 Mar 2024 10:31:14 +0100
Subject: [PATCH] update: minor improvement, new edge points

---
 .../__pycache__/get_relation.cpython-311.pyc  | Bin 6656 -> 6653 bytes
 server/map/get_relation.py                    |  47 +++++++++++-------
 2 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/server/map/__pycache__/get_relation.cpython-311.pyc b/server/map/__pycache__/get_relation.cpython-311.pyc
index c02d4b335fff30f0ab474427196c1571b4382e6a..140394c18ade684876d995f2db2b1352085f9609 100644
GIT binary patch
delta 1114
zcmZ`&OK1~87@pZ|b~oK*JJ~$?h?=I_Rx2&7iWQ#`TkuhP5CgUdG3{<^YE9D37PW3{
zm4fwVnS&mRtWc}qgGfOXJk*;WgxwIZ<kX8N52lEUi0GstwQBw0|Nr^^$G0>4&y4T*
zv0b?DaFhWekB67!m+Pm5N_cImp=&#gluH&F^-~f(LH9T%t89@NS(I2=>}Mp7#9Yg<
z0;VM!!S1Is-x=tg)7b{-_4JLX;Y57s(m*^G?!82o(U_W?%W-ZB^7vz!>1a(?Jl{Nj
z#PoG#eO>r1A1-e*+K#;5WrVuTKu<Q%Gq=b729|MTYZjuPUSmNx3cuiP;l<c(RVZw^
zC|9ReqZF&rlBKGY1SyVSxC+};=SmVtw8lwR%_cD#HOm0-kZ4gL&{)kjXe;4!%L@gO
zD7ACc0RZ{X8U^V>0ryW^P^0xS{0g}l36cVcm&%(gMehm_#udVUTGPSUQF7)j(1yp*
zb;x4Y86h_6yb3w|!5OG=)({zz`_*t?T#3ob?wV17MEc~Rp>Q%fD&uysx!O@0#rbF~
zd@*NDC}bQ;Cpi<oCWab?q#9AwXl$T3r<`4h4xB9!=J-|Y_^Y^q=_P&1ZP@3U82iit
zd->#LllNtL-|a|-_Zz(b-f?5ozW3`JC%f_tSl6H{(+4Iy7pm*@$aM4MvG;bTA=c`h
z#<q_6%f?B`=#QK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~&f4
zvb@r2YTFw#ys^FLN&V7VtdiW9I#$bTT6OM=u8oWpA24GBrZi|tgO!ncZ2?2<)<=wU
z&u@(xFHah3)>3nIgck4!12oEv*)+~vVScx}*PHYv^D9C=*=_*Auw;`91!H=2TkJE$
zzMaTP{kj=ByW-p<(p}QMO}h0b^o8o|M%w6o#v}=gBn*<+A+fupcboL;x$5~^-{+4R
zeS;<$vdEA@hW5yDoj1wpmA1O?Q1qX55w-W!k&}-%pfY?fL`Q?e^y`q06I2dAA{6cS
z{J)xI=!fB$?_uSuBZ`Cm7>=De?0K-)+zQ?}@(L~TMV_8(?|~2SH9#-T;p6n9_5|<R
zbzRLaWRwE^p?ytSgDVt11z1LVU60=GFxigf;mY}yp4Bk@_}DERqR&Q_=r@r8yieI^
pj+Ub*#-7F(bax#A)!-R{wuT#Y6iS%jxa|b0fd%M)As>J{{0k3!GdutQ

diff --git a/server/map/get_relation.py b/server/map/get_relation.py
index 7f59fd80..2f290be9 100644
--- a/server/map/get_relation.py
+++ b/server/map/get_relation.py
@@ -5,6 +5,8 @@ import matplotlib.ticker as ticker
 import random
 import numpy as np
 
+polygon_min_x = None  # The left most point of the entire polygon
+
 
 def get_relation(self, body_of_water: str):
     # Load GeoJSON data using geopandas
@@ -38,7 +40,7 @@ def get_relation(self, body_of_water: str):
         for hrz_line in hrz_lines:
 
             # Split shape into upper and lower section as hrz_line as divider
-            divided_poly = cut_polygon_in_two(polygon, hrz_line, cell_size)
+            divided_poly = cut_polygon_by_points(polygon, hrz_line, cell_size)
             horizontal_section = divided_poly[0]  # Save upper horizontal section
 
             polygon = divided_poly[1]  # Set polygon to the remaining, un-split shape for next iteration
@@ -49,10 +51,10 @@ def get_relation(self, body_of_water: str):
             # Cut each horizontal section into vertical sections, right to left
             for vrt_line in vrt_lines:
                 if len(horizontal_section.exterior.coords) < 3:
-                    break # Break from loop im remaining section has no coordinates
+                    break  # Break from loop im remaining section has no coordinates
 
                 # Split the horizontal section into two vertical parts
-                vertical_parts = cut_polygon_in_two(horizontal_section, vrt_line, -0.1)
+                vertical_parts = cut_polygon_by_points(horizontal_section, vrt_line, -0.1)
 
                 divided_map.append(vertical_parts[0])  # Append split vertical sections to final list of shapes
 
@@ -92,8 +94,8 @@ def get_relation(self, body_of_water: str):
 
 
 # Takes a polygon and divides its coordinates into two shapes, where divisor is a
-# coordinate that defines the point of division
-def cut_polygon_in_two(polygon: Polygon, divisor: float, cell_size: float):
+# coordinate that defines the point of division.
+def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float):
     # Extract polygon exterior coordinates
     exterior_coords = list(polygon.exterior.coords)
 
@@ -110,21 +112,29 @@ def cut_polygon_in_two(polygon: Polygon, divisor: float, cell_size: float):
             else:
                 remaining_shape.append(point)
 
-        if len(split_shape) > 2:
-            # Get last point added to
-            last_point = split_shape[-1]
-            # Get length of the newly created edge
-            new_edge_len = abs(last_point.x - split_shape[0].x)
-            print("new_edge_len: ", new_edge_len, "    cell_size: ", cell_size, "   last_point.x: ", last_point.x,  "   split_shape[0].x: ", split_shape[0].x)
+    #################################################################
+        # Prepend new points onto the newly created edge to facilitate vertical splitting
+        if len(split_shape) > 2 and polygon_min_x is not None:
+            # Define starting point with an x-value that will be common for all polygons
+            starting_point = polygon_min_x
+
+            # Get corners of the newly created shape which make up the new edge
+            right_corner = split_shape[-1]
+            left_corner = split_shape[0]
+
+            print("right_corner: ", right_corner, "    left_corner: ", left_corner, "   cell_size: ", cell_size)
 
-            # Add points along the new edge to allow horizontal sections to be split into vertical ones
-            while new_edge_len > cell_size:
-                x_val = new_edge_len - cell_size
+            while starting_point < left_corner.x:  # Increment starting point until it is withing the polygons bounds
+                starting_point += cell_size
 
-                split_shape.insert(0, (x_val, last_point.y))  # NB may have to add/subtract small offset of 0.00001
-                remaining_shape.insert(0, (x_val, last_point.y))  # Prepend
+            # Insert new points with cell_size spacing while starting_point is within bounds
+            while starting_point < right_corner.x:
 
-                new_edge_len -= cell_size
+                split_shape.insert(0, (starting_point, divisor))  # NB may have to add/subtract small offset of 0.00001
+                remaining_shape.insert(0, (starting_point, divisor))  # Prepend new point to shape
+
+                starting_point += cell_size
+    #################################################################
 
     else:  # Vertical split
         for point in exterior_coords:
@@ -157,6 +167,9 @@ def create_grid_coords(polygon: Polygon, cell_size: float):
     # Define boundaries of grid
     min_x, min_y, max_x, max_y = polygon.bounds
 
+    global polygon_min_x  # Set value of global variable
+    polygon_min_x = min_x
+
     # Divide grid into sections of size *cell_size
     x_coords = np.arange(min_x, max_x, cell_size)
     y_coords = np.arange(min_y, max_y, cell_size)
-- 
GitLab