From d7b5e10f30450a4e9134eb82ce145bd6d42b2dd5 Mon Sep 17 00:00:00 2001 From: Sara <sarasdj@stud.ntnu.no> Date: Wed, 13 Mar 2024 22:12:09 +0100 Subject: [PATCH] update: another approach, still buggy --- .../__pycache__/get_relation.cpython-311.pyc | Bin 9986 -> 8312 bytes server/map/get_relation.py | 190 ++---------------- 2 files changed, 18 insertions(+), 172 deletions(-) diff --git a/server/map/__pycache__/get_relation.cpython-311.pyc b/server/map/__pycache__/get_relation.cpython-311.pyc index a7b87b9c6a75148f670c316d70ce731e9b58227a..f13e9b873b357cf5927106718f674e4048732460 100644 GIT binary patch delta 958 zcmZuuT};zZ6u#Y7Xld7VfZf*OeqckkM9KU|$imd1h6G}G(8LE^+&~3|Z5RWNFpP<X z5G-DdeZW|aKTGrh#RngGFb3mGyELI88ZpuLMWZ1;c{?E{#*=%#bMHC#eD~yj*Sj{_ znWuJp6+m{I`<D9RonzdXGPfi3Vm8WRr<ayl!G_H)eW-nD!4!DQr8eW}sulV7pB4aa zl6OH4NZ4d%#H83XbRO&n(_}vwx59b%IgEs7c@8q3AXMwzD7qXcMogYfn<C*bstr)p zFz=}#GL|Ih0XZvt<vE60^V>Zpdfjjrzpa53$v!M;yFon1pP&JrA^&63WYRK@=RgjJ z%K}H|Yy;(drWLWH*KJ!BYYrQOpS`tqJc*6j0r*eP;${z;h_zv$OyII2et4hbnJKad zm;;&~u7DO_3=6}!uZE`jE?iCw_GZr~hEp)%Yps8eCdg6<Hu7MZT8Y15@*r~<ox|9g zbs#kw#^I#Dt}a}a*@cpsW)qGeSR)5heIvc8WPb{-_yZHeW+9CrYYOGxvi3~7SvbB0 zi)fR5hn1f*DjBVkzc;S=4z6=<kuqvIku-R(==f0qs$F-o@`1VBd`@!)b!V^ya57v5 zF>ciGa&-RC)KM`mrVR%x9hips0DfLrW$TM<y%JZ_%6^TF=xjtSFT+_aow_YZg28%L zS$~oBD*@%C!f9-i&NiuR)5ced5wB^Kpi_dJC{jL^@;%<8j6R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50030107 100644 --- a/server/map/get_relation.py +++ b/server/map/get_relation.py @@ -85,89 +85,6 @@ def get_relation(self, body_of_water: str): # NB: implement body_of_water self.wfile.write(tiles_json.encode('utf-8')) -''' -# Takes a polygon and divides its coordinates into two shapes, where divisor is a -# 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) - - # Initialize lists to store coordinates of new shapes after split - split_shape = [] - remaining_shape = [] - - # Loop through points and check which side of the division line they are - if cell_size > 0: # Horizontal split - prev_point = exterior_coords[0] - for point in exterior_coords: - point = Point(point) # Convert coordinates to Shapely Point object - - if point.y < divisor < prev_point.y: - remaining_shape.append(point) - remaining_shape.append((point.x, divisor)) - - split_shape.append((prev_point.x, divisor)) - split_shape.append(prev_point) - elif point.y > divisor > prev_point.y: - split_shape.append(point) - split_shape.append((point.x, divisor)) - - remaining_shape.append((prev_point.x, divisor)) - remaining_shape.append(prev_point) - elif point.y < divisor: # Check if point is over or below divisor - split_shape.append(point) # Append to appropriate shape - elif point.y > divisor: - remaining_shape.append(point) - prev_point = point - - # Populate newly created edges with points to facilitate vertical cutting - populated_shapes = populate_new_edge(split_shape, remaining_shape, cell_size, divisor) - if populated_shapes is not None: - split_shape = populated_shapes[0] - remaining_shape = populated_shapes[1] - - else: # Vertical split - prev_point = exterior_coords[1] - for point in exterior_coords: - point = Point(point) # Convert coordinates to Shapely Point object - - if point.x < divisor < prev_point: - remaining_shape.append(point) - remaining_shape.append((divisor, point.y)) - - split_shape.append((divisor, prev_point.y)) - split_shape.append(prev_point) - elif point.x > divisor > prev_point: - split_shape.append(point) - split_shape.append((divisor, point.y)) - - remaining_shape.append((divisor, prev_point.y)) - remaining_shape.append(prev_point) - elif point.x < divisor: # Check if point is over or below divisor - split_shape.append(point) # Append to appropriate shape - elif point.x > divisor: - remaining_shape.append(point) - prev_point = point - - # 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)) - 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)) - remaining_shape = None - else: - remaining_shape.append(remaining_shape[0]) # Append first coord to create closed loop - - # Return split polygons as Shapely Polygon objects - return Polygon(split_shape), Polygon(remaining_shape) -''' - - def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float): # Extract polygon exterior coordinates exterior_coords = list(polygon.exterior.coords) @@ -259,110 +176,39 @@ def circle_polygon(): def populate_new_edge(polygon, cell_size: float, divisor: float): if polygon is not None and polygon_min_x is not None: - sub_division = [] - final_shapes = [] - last_sub_division = [] + final_shape = [] + left_edge = [] left_most_point = None + # Add all coordinates on the right side of the divisor first for i in range(len(polygon)): x, y = polygon[i] if x > divisor: - sub_division.append((x, y)) + final_shape.append((x, y)) elif x < divisor: - last_sub_division.append((x, y)) + left_edge.append((x, y)) - if len(sub_division) < 1 or len(last_sub_division) < 1: + if len(final_shape) < 1 or len(left_edge) < 1: return None # Find most left point in the polygon - for point in last_sub_division: + for point in left_edge: if not left_most_point: left_most_point = point[0] elif point[0] < left_most_point: left_most_point = point[0] - # Sort sub_division from bottom to top - if sub_division[0][1] > sub_division[-1][1]: - sub_division = sorted(sub_division, key=lambda point: sub_division[1]) - - # Append corners to edge piece - sub_division.append((sub_division[-1][0] - cell_size, sub_division[-1][1])) - sub_division.append((sub_division[-1][0] - cell_size, sub_division[0][1])) - - # Append first section to list as Shapely Polygon object - final_shapes.append(Polygon(sub_division)) - - # Save last point of previous tile (upper left corner) as the starting coordinate for a new tile - next_point = sub_division[-1] - - # Clear subdivision list for next tile and add point as a reference - sub_division = [next_point] - - # Continue making tiles on the horizontal section while the left edge is not reached - while sub_division[-1][0] > left_most_point: - # Append three more corners to new tile - sub_division.append((sub_division[0][0], sub_division[0][1] - cell_size)) - sub_division.append((sub_division[0][0] - cell_size, sub_division[-1][1])) - sub_division.append((sub_division[-1][0] - cell_size, sub_division[0][0])) - # NB maybe have to close loop??? - - # Append new shape, save next starting coordinate, and clear sub_div for next tile - final_shapes.append(Polygon(sub_division)) - next_point = sub_division[-1] - sub_division = [next_point] - - print("sub_div: ", sub_division[-1], " left_most_point: ", left_most_point) - - # Sort last coordinates of last tile from bottom to top - if last_sub_division[0][1] > last_sub_division[-1][1]: - last_sub_division = sorted(last_sub_division, key=lambda point: last_sub_division[1]) - - # Append two corners to complete the last tile - last_sub_division.append((sub_division[-1][0] - cell_size, sub_division[-1][1])) - last_sub_division.append((sub_division[-1][0] - cell_size, sub_division[-1][1] - cell_size)) - # NB maybe have to close loop??? - - final_shapes.append(Polygon(last_sub_division)) - - return final_shapes # Return list of all tiles for current horizontal section - - -''' -# Adds equally spaced points along an edge that is created after a polygon is cut. -def populate_new_edge(split_shape, remaining_shape, cell_size: float, divisor: float): - # Prepend new points onto the newly created edge to facilitate vertical splitting - if split_shape is not None 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 - - # Create list of corners - corners = [] - - divisor_range = ((divisor - 0.1),(divisor + 0.1)) # Define tolerance - - # Find all corners of split shape. Corner = point that intersects divisor - for point in split_shape: - if divisor_range[0] < point.y < divisor_range[1]: - corners.append(point) - - if not corners or len(corners) < 2: - return None - - # Sort corners in ascending order (left to right) based on x coordinate - sorted_corners = sorted(corners, key=lambda p: p.x) - - while starting_point < sorted_corners[0].x: # Increment starting point until it is withing the polygons bounds - starting_point += cell_size - - # if starting_point < left_corner.x: # NB: optimised substitute for previous while loop, requires testing - # starting_point += cell_size * math.floor(starting_point - left_corner.x) + # Sort final_shape from bottom to top + if final_shape[0][1] > final_shape[-1][1]: + final_shape = sorted(final_shape, key=lambda point: final_shape[1]) - # Insert new points with cell_size spacing while starting_point is within bounds - while starting_point < sorted_corners[-1].x: - split_shape.insert(0, (starting_point, divisor)) # NB may have to add/subtract small offset of 0.00001 - remaining_shape.insert(-1, (starting_point, divisor)) # Prepend new point to shape + i = 0 + while polygon[i][0] < left_most_point: # While to haven't reached left edge + final_shape.append(polygon[i][0] + cell_size) + i += 1 - starting_point += cell_size + # NB VERY IMPORTANT MUST CREATE LOGIC FOR SHAPES THAT ARE SPLIT + final_shape.append(left_edge) + final_shape.append(final_shape[0]) - return split_shape, remaining_shape -''' + return final_shape -- GitLab