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

update: small improvement on circle

parent a4a9d703
No related branches found
No related tags found
1 merge request!6Clhp map
No preview for this file type
...@@ -11,21 +11,6 @@ polygon_min_x = None # The left most point of the entire polygon ...@@ -11,21 +11,6 @@ polygon_min_x = None # The left most point of the entire polygon
# Read a json file with relation data and send to response object # Read a json file with relation data and send to response object
def get_relation(self, body_of_water: str): # NB: implement body_of_water def get_relation(self, body_of_water: str): # NB: implement body_of_water
# Load GeoJSON data using geopandas
geo_data = gpd.read_file("server/map/mjosa.geojson")
# Filter only polygons, exclude points and other feature types to reduce response size
polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
if polygon_data.empty:
raise ValueError("Failed to extract polygon data from file")
# Extract coordinates from polygons and create polygon objects
polygons = [Polygon(polygon.exterior) for polygon in polygon_data['geometry']]
if not polygons:
raise ValueError("Failed to convert to polygons")
divided_map = [] # List to store map shapes while splitting divided_map = [] # List to store map shapes while splitting
# NB: test polygon, remove after testing # NB: test polygon, remove after testing
...@@ -53,7 +38,20 @@ def get_relation(self, body_of_water: str): # NB: implement body_of_water ...@@ -53,7 +38,20 @@ def get_relation(self, body_of_water: str): # NB: implement body_of_water
if not horizontal_section or not divided_poly[0]: if not horizontal_section or not divided_poly[0]:
continue continue
divided_map.append(horizontal_section) # 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
# Split the horizontal section into two vertical parts
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
# Set horizontal_section to the remaining, un-split, horizontal section for next iteration
horizontal_section = vertical_parts[1]
polygon = divided_poly[1] # Set polygon to the remaining, un-split shape for next iteration
divided_map.append(polygon) divided_map.append(polygon)
...@@ -63,8 +61,8 @@ def get_relation(self, body_of_water: str): # NB: implement body_of_water ...@@ -63,8 +61,8 @@ def get_relation(self, body_of_water: str): # NB: implement body_of_water
# NB test plot # NB test plot
fig, ax = plt.subplots() fig, ax = plt.subplots()
ax.set_aspect(1.5) ax.set_aspect(1)
ax.xaxis.set_major_locator(ticker.MultipleLocator(0.2)) # ax.xaxis.set_major_locator(ticker.MultipleLocator(0.2))
for i, polygon in enumerate(tiles['geometry']): for i, polygon in enumerate(tiles['geometry']):
random_color = "#{:06x}".format(random.randint(0, 0xFFFFFF)) random_color = "#{:06x}".format(random.randint(0, 0xFFFFFF))
...@@ -106,10 +104,11 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float): ...@@ -106,10 +104,11 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float):
else: else:
remaining_shape.append(point) remaining_shape.append(point)
# Add points to the newly created edges of split_shape and remaining_shape # Populate newly created edges with points to facilitate vertical cutting
populate_new_edge(split_shape, remaining_shape, cell_size, divisor) populated_shapes = populate_new_edge(split_shape, remaining_shape, cell_size, divisor)
#split_shape = shape_w_edges[0] if populated_shapes is not None:
#remaining_shape = shape_w_edges[1] split_shape = populated_shapes[0]
remaining_shape = populated_shapes[1]
else: # Vertical split else: # Vertical split
for point in exterior_coords: for point in exterior_coords:
...@@ -137,39 +136,14 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float): ...@@ -137,39 +136,14 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float):
return Polygon(split_shape), Polygon(remaining_shape) return Polygon(split_shape), Polygon(remaining_shape)
# 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):
# 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, " left == right?: ", left_corner == right_corner)
while starting_point < left_corner.x: # 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 < right_corner.x:
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
return split_shape, remaining_shape
# Generate grid of equally spaced x and y coordinates where the grid size is determined by cell_size # 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): 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
polygon_min_x = min_x
# Divide grid into sections of size *cell_size # Divide grid into sections of size *cell_size
x_step = max((max_x - min_x) / 10, cell_size) x_step = max((max_x - min_x) / 10, cell_size)
y_step = max((max_y - min_y) / 10, cell_size) y_step = max((max_y - min_y) / 10, cell_size)
...@@ -183,7 +157,7 @@ def create_grid_coords(polygon: Polygon, cell_size: float): ...@@ -183,7 +157,7 @@ def create_grid_coords(polygon: Polygon, cell_size: float):
# NB: only for testing # NB: only for testing
def circle_polygon(): def circle_polygon():
circle_points = [] circle_points = []
num_points = 80 num_points = 200
center_x, center_y = 0.0, 0.0 center_x, center_y = 0.0, 0.0
radius = 6 radius = 6
...@@ -194,4 +168,34 @@ def circle_polygon(): ...@@ -194,4 +168,34 @@ def circle_polygon():
circle_points.append((x, y)) circle_points.append((x, y))
circle_points.append(circle_points[0]) # Close the circle circle_points.append(circle_points[0]) # Close the circle
return Polygon(circle_points) return Polygon(circle_points)
\ No newline at end of file
# 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):
# 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)
while starting_point < left_corner.x: # 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 < right_corner.x:
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
return split_shape, 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