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

update: perhaps some improvement

parent adb005ac
No related branches found
No related tags found
1 merge request!6Clhp map
No preview for this file type
......@@ -19,7 +19,7 @@ def get_relation(self, body_of_water: str): # NB: implement body_of_water
# Divide all polygons into sections
for polygon in polygons:
cell_size = 0.4
cell_size = 1.5
# Divide the length and with of polygon into a grid of equally sized parts
grid_lines = create_grid_coords(polygon, cell_size)
......@@ -28,7 +28,6 @@ def get_relation(self, body_of_water: str): # NB: implement body_of_water
# 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
divided_poly = cut_polygon_by_points(polygon, hrz_line, cell_size)
horizontal_section = divided_poly[0] # Save upper horizontal section
......@@ -122,10 +121,13 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float):
remaining_shape.append((point2.x, point2.y))
# 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]
populated_split = populate_new_edge(split_shape, cell_size, divisor)
if populated_split is not None:
split_shape = populated_split
populated_rem = populate_new_edge(remaining_shape, cell_size, divisor)
if populated_rem is not None:
remaining_shape = populated_rem
# Ensure that the shapes are closed loops
if len(split_shape) > 0 and split_shape[0] != split_shape[-1]:
......@@ -137,7 +139,6 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float):
return Polygon(split_shape), Polygon(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 +175,7 @@ def circle_polygon():
# 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):
def populate_new_edge(split_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
......@@ -183,11 +184,9 @@ def populate_new_edge(split_shape, remaining_shape, cell_size: float, divisor: f
# Create list of corners
corners = []
divisor_range = ((divisor - 0.1),(divisor + 0.1)) # Define tolerance NB: must find appropriate value
# Find all corners of split shape. Corner = point that intersects divisor
for point in split_shape:
if divisor_range[0] < point[1] < divisor_range[1]:
if point[1] == divisor:
corners.append(point)
if not corners or len(corners) < 2:
......@@ -199,16 +198,9 @@ def populate_new_edge(split_shape, remaining_shape, cell_size: float, divisor: f
while starting_point < sorted_corners[0][0]: # 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 < sorted_corners[-1][0]:
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
starting_point += cell_size
return split_shape, remaining_shape
return split_shape
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