From 05b1ca380ff3d242c1b3565bef52b3735d843916 Mon Sep 17 00:00:00 2001 From: Sara <sarasdj@stud.ntnu.no> Date: Wed, 13 Mar 2024 10:55:02 +0100 Subject: [PATCH] update: minor optimalisation --- server/map/get_relation.py | 60 ++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/server/map/get_relation.py b/server/map/get_relation.py index 2f290be9..c43bb700 100644 --- a/server/map/get_relation.py +++ b/server/map/get_relation.py @@ -3,12 +3,14 @@ from shapely.geometry import Polygon, Point, LineString, MultiPolygon import matplotlib.pyplot as plt import matplotlib.ticker as ticker import random +import math import numpy as np polygon_min_x = None # The left most point of the entire polygon -def get_relation(self, body_of_water: str): +# Read a json file with relation data and send to response object +def get_relation(self, body_of_water: str): # NB: implement body_of_water # Load GeoJSON data using geopandas geo_data = gpd.read_file("server/map/mjosa.geojson") @@ -112,29 +114,8 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float): else: remaining_shape.append(point) - ################################################################# - # 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) - - while starting_point < left_corner.x: # Increment starting point until it is withing the polygons bounds - starting_point += cell_size - - # Insert new points with cell_size spacing while starting_point is within bounds - while starting_point < right_corner.x: - - 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 - ################################################################# + # Add points to the newly created edges of split_shape and remaining_shape + split_shape, remaining_shape = populate_new_edge(split_shape, remaining_shape, cell_size, divisor) else: # Vertical split for point in exterior_coords: @@ -162,6 +143,35 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float): return Polygon(split_shape), Polygon(remaining_shape) +# 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 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) + + while starting_point < left_corner.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) + + # Insert new points with cell_size spacing while starting_point is within bounds + while starting_point < right_corner.x: + 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 + + return split_shape, remaining_shape + + # Generate grid of equally spaced x and y coordinates where the grid size is determined by cell_size def create_grid_coords(polygon: Polygon, cell_size: float): # Define boundaries of grid @@ -174,7 +184,7 @@ def create_grid_coords(polygon: Polygon, cell_size: float): x_coords = np.arange(min_x, max_x, cell_size) y_coords = np.arange(min_y, max_y, cell_size) - # Round the coordinates to 4 decimals + # Round coordinates to 4 decimals x_coords_rounded = np.around(x_coords, decimals=4) y_coords_rounded = np.around(y_coords, decimals=4) -- GitLab