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 ...@@ -5,6 +5,8 @@ import matplotlib.ticker as ticker
import random import random
import numpy as np import numpy as np
polygon_min_x = None # The left most point of the entire polygon
def get_relation(self, body_of_water: str): def get_relation(self, body_of_water: str):
# Load GeoJSON data using geopandas # Load GeoJSON data using geopandas
...@@ -38,7 +40,7 @@ def get_relation(self, body_of_water: str): ...@@ -38,7 +40,7 @@ def get_relation(self, body_of_water: str):
for hrz_line in hrz_lines: for hrz_line in hrz_lines:
# Split shape into upper and lower section as hrz_line as divider # 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 horizontal_section = divided_poly[0] # Save upper horizontal section
polygon = divided_poly[1] # Set polygon to the remaining, un-split shape for next iteration polygon = divided_poly[1] # Set polygon to the remaining, un-split shape for next iteration
...@@ -52,7 +54,7 @@ def get_relation(self, body_of_water: str): ...@@ -52,7 +54,7 @@ def get_relation(self, body_of_water: str):
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 # 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 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): ...@@ -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 # Takes a polygon and divides its coordinates into two shapes, where divisor is a
# coordinate that defines the point of division # coordinate that defines the point of division.
def cut_polygon_in_two(polygon: Polygon, divisor: float, cell_size: float): def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float):
# Extract polygon exterior coordinates # Extract polygon exterior coordinates
exterior_coords = list(polygon.exterior.coords) exterior_coords = list(polygon.exterior.coords)
...@@ -110,21 +112,29 @@ def cut_polygon_in_two(polygon: Polygon, divisor: float, cell_size: float): ...@@ -110,21 +112,29 @@ def cut_polygon_in_two(polygon: Polygon, divisor: float, cell_size: float):
else: else:
remaining_shape.append(point) remaining_shape.append(point)
if len(split_shape) > 2: #################################################################
# Get last point added to # Prepend new points onto the newly created edge to facilitate vertical splitting
last_point = split_shape[-1] if len(split_shape) > 2 and polygon_min_x is not None:
# Get length of the newly created edge # Define starting point with an x-value that will be common for all polygons
new_edge_len = abs(last_point.x - split_shape[0].x) starting_point = polygon_min_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)
# 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 starting_point < left_corner.x: # Increment starting point until it is withing the polygons bounds
while new_edge_len > cell_size: starting_point += cell_size
x_val = new_edge_len - cell_size
split_shape.insert(0, (x_val, last_point.y)) # NB may have to add/subtract small offset of 0.00001 # Insert new points with cell_size spacing while starting_point is within bounds
remaining_shape.insert(0, (x_val, last_point.y)) # Prepend 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 else: # Vertical split
for point in exterior_coords: for point in exterior_coords:
...@@ -157,6 +167,9 @@ def create_grid_coords(polygon: Polygon, cell_size: float): ...@@ -157,6 +167,9 @@ def create_grid_coords(polygon: Polygon, cell_size: float):
# Define boundaries of grid # Define boundaries of grid
min_x, min_y, max_x, max_y = polygon.bounds 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 # Divide grid into sections of size *cell_size
x_coords = np.arange(min_x, max_x, cell_size) x_coords = np.arange(min_x, max_x, cell_size)
y_coords = np.arange(min_y, max_y, 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.
Please register or to comment