diff --git a/server/map/__pycache__/get_relation.cpython-311.pyc b/server/map/__pycache__/get_relation.cpython-311.pyc index 61a991605c9616d12954c7a67b412e78d41c1477..1f9d6fb9f61cc81a841db10607e1f3e3d2ff0e9a 100644 Binary files a/server/map/__pycache__/get_relation.cpython-311.pyc and b/server/map/__pycache__/get_relation.cpython-311.pyc differ diff --git a/server/map/get_relation.py b/server/map/get_relation.py index 0ca4075bf6f793c10b8e017498986b78b2f1989c..ba0a3d1d1f744ac1cf7f23b1e5eaaca047bcc9c8 100644 --- a/server/map/get_relation.py +++ b/server/map/get_relation.py @@ -1,8 +1,8 @@ import geopandas as gpd from shapely.geometry import Polygon, Point, LineString, MultiPolygon import matplotlib.pyplot as plt -from shapely.ops import linemerge, unary_union, polygonize import matplotlib.ticker as ticker +import random import numpy as np @@ -30,23 +30,29 @@ def get_relation(self, body_of_water: str): # Divide the length and with of polygon into a grid grid_lines = create_grid_coords(polygon, cell_size=0.1) - #hrz_lines = grid_lines[0] # Horizontal coordinates - #vrt_lines = grid_lines[1] # Vertical coordinates - hrz_lines = grid_lines[1] # Horizontal coordinates - for line in hrz_lines: - print("line: ", line) - - vrt_lines = [11.0413] # Vertical coordinates + vrt_lines = grid_lines[0] # Vertical coordinates + for line in vrt_lines: + print("Line: ", line) - # Cut polygon into horizontal sections + # Cut polygon into horizontal sections, bottom to top for hrz_line in hrz_lines: # Split shape into upper and lower section as hrz_line as divider cut_poly_1 = cut_polygon_in_two(polygon, hrz_line, True) polygon_part = cut_poly_1[0] # Save upper horizontal section - divided_map.append(polygon_part) + if not polygon_part or not cut_poly_1[0]: + continue + + # Cut each horizontal section into vertical sections, right to left + for vrt_line in vrt_lines: + cut_poly_2 = cut_polygon_in_two(polygon_part, vrt_line, False) + + divided_map.append(cut_poly_2[0]) # Append part to final list of shapes + + # Set polygon_part to the remaining, un-split, horizontal section for next iteration + polygon_part = cut_poly_2[1] polygon = cut_poly_1[1] # Set polygon to the remaining, un-split shape for next iteration @@ -61,9 +67,12 @@ def get_relation(self, body_of_water: str): ax.set_aspect(1.5) ax.xaxis.set_major_locator(ticker.MultipleLocator(0.2)) - for polygon in tiles['geometry']: + for i, polygon in enumerate(tiles['geometry']): + random_color = "#{:06x}".format(random.randint(0, 0xFFFFFF)) + x, y = polygon.exterior.xy - ax.plot(x, y, color='blue', alpha=0.5) # Plot the exterior of the polygon + ax.plot(x, y, color='blue', alpha=0.5) + ax.fill(x, y, color=random_color, alpha=0.5) plt.show() @@ -105,14 +114,14 @@ def cut_polygon_in_two(polygon: Polygon, divisor: float, horizontal: bool): # Check if the split_shape has enough coordinates to create a polygon if len(split_shape) < 3: - print("Not enough coordinates to create valid polygon: Split shape: ", len(split_shape)) + # print("Not enough coordinates to create valid polygon: Split shape: ", len(split_shape)) split_shape = None else: split_shape.append(split_shape[0]) # Append first coord to create closed loop # Check if the remaining_shape has enough coordinates to create a polygon if len(remaining_shape) < 3: - print("Not enough coordinates to create valid polygon: Remaining shape: ", len(remaining_shape)) + # print("Not enough coordinates to create valid polygon: Remaining shape: ", len(remaining_shape)) remaining_shape = None else: remaining_shape.append(remaining_shape[0]) # Append first coord to create closed loop