diff --git a/server/map/__pycache__/get_relation.cpython-311.pyc b/server/map/__pycache__/get_relation.cpython-311.pyc index bb596e391fabaf33934326971cb68302724bb81d..812af27f4b3e7b4696370419efaf670e47b9b2ff 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 9b1799824493e40ece7d58d1ce56ec57656853fd..b17ab008349f74516ffb79ca63635334ce33f5d9 100644 --- a/server/map/get_relation.py +++ b/server/map/get_relation.py @@ -7,21 +7,21 @@ import numpy as np def get_relation(self, body_of_water: str): - print("In get_relation") # 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 not polygon_data: + 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 len(polygons) <= 1: # Return if conversion to polygon fails - print("Failed to convert to polygons") - return + if not polygons: + raise ValueError("Failed to convert to polygons") - print("Creating grid and cutting polygons") divided_map = [] # Divide all polygons into sections @@ -71,21 +71,31 @@ def get_relation(self, body_of_water: str): def cut_polygon_in_two(polygon, line): - points = list(polygon.exterior.coords) # Extract polygon coordinates + # Check for invalid parameters + if not isinstance(polygon, Polygon) or not isinstance(line, LineString): + print("Inputs must be Shapely Polygon and LineString obects") + + # Extract polygon exterior coordinates + exterior_coords = list(polygon.exterior.coords) + # Initialize lists to store points for the two shapes shape_1 = [] shape_2 = [] - # Split points into separate shapes based on their location relative to line - for point in points: + # Split points into separate shapes based on their location relative to the line + for point in exterior_coords: point = Point(point) # Convert coordinates to Shapely Point object if line.distance(point) < 1e-10: # Floating point error tolerance - if line.contains(point): # Determine if point goes into shape 1 or shape 2 + if line.contains(point): # Determine if point belongs to shape 1 or shape 2 shape_1.append(point) else: shape_2.append(point) - # Convert the lists of points to Polygon objects + # Check if shapes are empty + if not shape_1 or not shape_2: + raise ValueError("One or both shapes are empty after cutting") + + # Create Polygon objects from the lists of points poly_1 = Polygon(shape_1) poly_2 = Polygon(shape_2) @@ -119,4 +129,7 @@ def create_grid(polygon, cell_size): for y in y_coords: horizontal_lines.append(LineString([(min_x, y), (max_x, y)])) + if not horizontal_lines or not vertical_lines: + raise ValueError("List of horizontal or vertical lines is empty") + return horizontal_lines, vertical_lines