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

update: minor improvement, new edge points

parent 592f0b73
No related branches found
No related tags found
1 merge request!6Clhp map
No preview for this file type
......@@ -5,6 +5,8 @@ import matplotlib.ticker as ticker
import random
import numpy as np
polygon_min_x = None # The left most point of the entire polygon
def get_relation(self, body_of_water: str):
# Load GeoJSON data using geopandas
......@@ -38,7 +40,7 @@ def get_relation(self, body_of_water: str):
for hrz_line in hrz_lines:
# Split shape into upper and lower section as hrz_line as divider
divided_poly = cut_polygon_in_two(polygon, hrz_line, cell_size)
divided_poly = cut_polygon_by_points(polygon, hrz_line, cell_size)
horizontal_section = divided_poly[0] # Save upper horizontal section
polygon = divided_poly[1] # Set polygon to the remaining, un-split shape for next iteration
......@@ -49,10 +51,10 @@ def get_relation(self, body_of_water: str):
# Cut each horizontal section into vertical sections, right to left
for vrt_line in vrt_lines:
if len(horizontal_section.exterior.coords) < 3:
break # Break from loop im remaining section has no coordinates
break # Break from loop im remaining section has no coordinates
# Split the horizontal section into two vertical parts
vertical_parts = cut_polygon_in_two(horizontal_section, vrt_line, -0.1)
vertical_parts = cut_polygon_by_points(horizontal_section, vrt_line, -0.1)
divided_map.append(vertical_parts[0]) # Append split vertical sections to final list of shapes
......@@ -92,8 +94,8 @@ def get_relation(self, body_of_water: str):
# Takes a polygon and divides its coordinates into two shapes, where divisor is a
# coordinate that defines the point of division
def cut_polygon_in_two(polygon: Polygon, divisor: float, cell_size: float):
# 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)
......@@ -110,21 +112,29 @@ def cut_polygon_in_two(polygon: Polygon, divisor: float, cell_size: float):
else:
remaining_shape.append(point)
if len(split_shape) > 2:
# Get last point added to
last_point = split_shape[-1]
# Get length of the newly created edge
new_edge_len = abs(last_point.x - split_shape[0].x)
print("new_edge_len: ", new_edge_len, " cell_size: ", cell_size, " last_point.x: ", last_point.x, " split_shape[0].x: ", split_shape[0].x)
#################################################################
# 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)
# Add points along the new edge to allow horizontal sections to be split into vertical ones
while new_edge_len > cell_size:
x_val = new_edge_len - cell_size
while starting_point < left_corner.x: # Increment starting point until it is withing the polygons bounds
starting_point += cell_size
split_shape.insert(0, (x_val, last_point.y)) # NB may have to add/subtract small offset of 0.00001
remaining_shape.insert(0, (x_val, last_point.y)) # Prepend
# Insert new points with cell_size spacing while starting_point is within bounds
while starting_point < right_corner.x:
new_edge_len -= cell_size
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
#################################################################
else: # Vertical split
for point in exterior_coords:
......@@ -157,6 +167,9 @@ def create_grid_coords(polygon: Polygon, cell_size: float):
# Define boundaries of grid
min_x, min_y, max_x, max_y = polygon.bounds
global polygon_min_x # Set value of global variable
polygon_min_x = min_x
# Divide grid into sections of size *cell_size
x_coords = np.arange(min_x, max_x, cell_size)
y_coords = np.arange(min_y, max_y, cell_size)
......
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