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

add: some error handling, still no split

parent d4dce05a
No related branches found
No related tags found
1 merge request!6Clhp map
No preview for this file type
......@@ -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
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