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

update: colored plotting for testing

parent 84468543
No related branches found
No related tags found
1 merge request!6Clhp map
No preview for this file type
import geopandas as gpd import geopandas as gpd
from shapely.geometry import Polygon, Point, LineString, MultiPolygon from shapely.geometry import Polygon, Point, LineString, MultiPolygon
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from shapely.ops import linemerge, unary_union, polygonize
import matplotlib.ticker as ticker import matplotlib.ticker as ticker
import random
import numpy as np import numpy as np
...@@ -30,23 +30,29 @@ def get_relation(self, body_of_water: str): ...@@ -30,23 +30,29 @@ def get_relation(self, body_of_water: str):
# Divide the length and with of polygon into a grid # Divide the length and with of polygon into a grid
grid_lines = create_grid_coords(polygon, cell_size=0.1) grid_lines = create_grid_coords(polygon, cell_size=0.1)
#hrz_lines = grid_lines[0] # Horizontal coordinates
#vrt_lines = grid_lines[1] # Vertical coordinates
hrz_lines = grid_lines[1] # Horizontal coordinates hrz_lines = grid_lines[1] # Horizontal coordinates
for line in hrz_lines: vrt_lines = grid_lines[0] # Vertical coordinates
print("line: ", line) for line in vrt_lines:
print("Line: ", line)
vrt_lines = [11.0413] # Vertical coordinates
# Cut polygon into horizontal sections # Cut polygon into horizontal sections, bottom to top
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
cut_poly_1 = cut_polygon_in_two(polygon, hrz_line, True) cut_poly_1 = cut_polygon_in_two(polygon, hrz_line, True)
polygon_part = cut_poly_1[0] # Save upper horizontal section polygon_part = cut_poly_1[0] # Save upper horizontal section
divided_map.append(polygon_part) if not polygon_part or not cut_poly_1[0]:
continue
# Cut each horizontal section into vertical sections, right to left
for vrt_line in vrt_lines:
cut_poly_2 = cut_polygon_in_two(polygon_part, vrt_line, False)
divided_map.append(cut_poly_2[0]) # Append part to final list of shapes
# Set polygon_part to the remaining, un-split, horizontal section for next iteration
polygon_part = cut_poly_2[1]
polygon = cut_poly_1[1] # Set polygon to the remaining, un-split shape for next iteration polygon = cut_poly_1[1] # Set polygon to the remaining, un-split shape for next iteration
...@@ -61,9 +67,12 @@ def get_relation(self, body_of_water: str): ...@@ -61,9 +67,12 @@ def get_relation(self, body_of_water: str):
ax.set_aspect(1.5) ax.set_aspect(1.5)
ax.xaxis.set_major_locator(ticker.MultipleLocator(0.2)) ax.xaxis.set_major_locator(ticker.MultipleLocator(0.2))
for polygon in tiles['geometry']: for i, polygon in enumerate(tiles['geometry']):
random_color = "#{:06x}".format(random.randint(0, 0xFFFFFF))
x, y = polygon.exterior.xy x, y = polygon.exterior.xy
ax.plot(x, y, color='blue', alpha=0.5) # Plot the exterior of the polygon ax.plot(x, y, color='blue', alpha=0.5)
ax.fill(x, y, color=random_color, alpha=0.5)
plt.show() plt.show()
...@@ -105,14 +114,14 @@ def cut_polygon_in_two(polygon: Polygon, divisor: float, horizontal: bool): ...@@ -105,14 +114,14 @@ def cut_polygon_in_two(polygon: Polygon, divisor: float, horizontal: bool):
# Check if the split_shape has enough coordinates to create a polygon # Check if the split_shape has enough coordinates to create a polygon
if len(split_shape) < 3: if len(split_shape) < 3:
print("Not enough coordinates to create valid polygon: Split shape: ", len(split_shape)) # print("Not enough coordinates to create valid polygon: Split shape: ", len(split_shape))
split_shape = None split_shape = None
else: else:
split_shape.append(split_shape[0]) # Append first coord to create closed loop split_shape.append(split_shape[0]) # Append first coord to create closed loop
# Check if the remaining_shape has enough coordinates to create a polygon # Check if the remaining_shape has enough coordinates to create a polygon
if len(remaining_shape) < 3: if len(remaining_shape) < 3:
print("Not enough coordinates to create valid polygon: Remaining shape: ", len(remaining_shape)) # print("Not enough coordinates to create valid polygon: Remaining shape: ", len(remaining_shape))
remaining_shape = None remaining_shape = None
else: else:
remaining_shape.append(remaining_shape[0]) # Append first coord to create closed loop remaining_shape.append(remaining_shape[0]) # Append first coord to create closed loop
......
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