diff --git a/server/map/__pycache__/get_relation.cpython-311.pyc b/server/map/__pycache__/get_relation.cpython-311.pyc index 744144e33606421cf5d36ed1c24d8c6e2ddab9df..e35a7a0bbb24aae68e2c04bf6f3fd9fadf1b0f4e 100644 Binary files a/server/map/__pycache__/get_relation.cpython-311.pyc and b/server/map/__pycache__/get_relation.cpython-311.pyc differ diff --git a/server/map/get_relation.py b/server/map/get_relation.py index 52b037aad68814c42094d7fadeb18fccd9c43f2b..b80b3bf2044adac31f0b1dc72211147e7c8d01e6 100644 --- a/server/map/get_relation.py +++ b/server/map/get_relation.py @@ -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 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 # NB: test polygon, remove after testing @@ -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]: 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) @@ -63,8 +61,8 @@ def get_relation(self, body_of_water: str): # NB: implement body_of_water # NB test plot fig, ax = plt.subplots() - ax.set_aspect(1.5) - ax.xaxis.set_major_locator(ticker.MultipleLocator(0.2)) + ax.set_aspect(1) + # ax.xaxis.set_major_locator(ticker.MultipleLocator(0.2)) for i, polygon in enumerate(tiles['geometry']): random_color = "#{:06x}".format(random.randint(0, 0xFFFFFF)) @@ -106,10 +104,11 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float): else: remaining_shape.append(point) - # Add points to the newly created edges of split_shape and remaining_shape - populate_new_edge(split_shape, remaining_shape, cell_size, divisor) - #split_shape = shape_w_edges[0] - #remaining_shape = shape_w_edges[1] + # Populate newly created edges with points to facilitate vertical cutting + populated_shapes = populate_new_edge(split_shape, remaining_shape, cell_size, divisor) + if populated_shapes is not None: + split_shape = populated_shapes[0] + remaining_shape = populated_shapes[1] else: # Vertical split for point in exterior_coords: @@ -137,39 +136,14 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float): 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 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 + polygon_min_x = min_x + # Divide grid into sections of size *cell_size x_step = max((max_x - min_x) / 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): # NB: only for testing def circle_polygon(): circle_points = [] - num_points = 80 + num_points = 200 center_x, center_y = 0.0, 0.0 radius = 6 @@ -194,4 +168,34 @@ def circle_polygon(): circle_points.append((x, y)) circle_points.append(circle_points[0]) # Close the circle - return Polygon(circle_points) \ No newline at end of file + return Polygon(circle_points) + + +# 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 +