Skip to content
Snippets Groups Projects
Commit 05b1ca38 authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

update: minor optimalisation

parent 4546ff39
No related branches found
No related tags found
1 merge request!6Clhp map
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment