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

update: divide into horizontal sections

parent 33b45233
No related branches found
No related tags found
1 merge request!6Clhp map
No preview for this file type
...@@ -33,8 +33,8 @@ def get_relation(self, body_of_water: str): ...@@ -33,8 +33,8 @@ def get_relation(self, body_of_water: str):
#hrz_lines = grid_lines[0] # Horizontal coordinates #hrz_lines = grid_lines[0] # Horizontal coordinates
#vrt_lines = grid_lines[1] # Vertical coordinates #vrt_lines = grid_lines[1] # Vertical coordinates
hrz_lines = [60.7530] # Horizontal coordinates hrz_lines = [60.7530, 60.5931] # Horizontal coordinates
vrt_lines = [11.2267] # Vertical coordinates vrt_lines = [11.0413] # Vertical coordinates
# Cut polygon into horizontal sections # Cut polygon into horizontal sections
for hrz_line in hrz_lines: for hrz_line in hrz_lines:
...@@ -43,38 +43,23 @@ def get_relation(self, body_of_water: str): ...@@ -43,38 +43,23 @@ def get_relation(self, body_of_water: str):
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
if not polygon_part or not cut_poly_1[0]: divided_map.append(polygon_part)
continue
# Cut each horizontal section into vertical sections
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
# Split last remainder into horizontal lines divided_map.append(polygon)
for vrt_line in vrt_lines:
last_section = cut_polygon_in_two(polygon, vrt_line, False)
divided_map.append(last_section[0]) # Append part to final list of shapes
# Set polygon_part to the remaining, un-split, horizontal section for next iteration
polygon = last_section[1]
break # NB: temporary break to ensure treatment of only the first polygon
tiles = gpd.GeoDataFrame(geometry=divided_map) tiles = gpd.GeoDataFrame(geometry=divided_map)
# NB: test plots # NB test plot
fig, ax = plt.subplots() fig, ax = plt.subplots()
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))
tiles.plot(ax=ax, color='blue', edgecolor='orange', alpha=0.5, label='Original Polygon')
for polygon in tiles['geometry']:
x, y = polygon.exterior.xy
ax.plot(x, y, color='blue', alpha=0.5) # Plot the exterior of the polygon
plt.show() plt.show()
# Convert GeoDataFrame to GeoJSON # Convert GeoDataFrame to GeoJSON
...@@ -115,13 +100,17 @@ def cut_polygon_in_two(polygon: Polygon, divisor: float, horizontal: bool): ...@@ -115,13 +100,17 @@ 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") print("Not enough coordinates to create valid polygon: Split shape: ", len(split_shape))
split_shape = None split_shape = None
else:
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") print("Not enough coordinates to create valid polygon: Remaining shape: ", len(remaining_shape))
remaining_shape = None remaining_shape = None
else:
remaining_shape.append(remaining_shape[0]) # Append first coord to create closed loop
# Return split polygons as Shapely Polygon objects # Return split polygons as Shapely Polygon objects
return Polygon(split_shape), Polygon(remaining_shape) return Polygon(split_shape), Polygon(remaining_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