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

update: divide polygon into tiles

parent e2bdb579
Branches
No related tags found
2 merge requests!5Clhp map,!4Clhp map
No preview for this file type
import string
import json import json
import geopandas as gpd import geopandas as gpd
from server.map.get_markers import get_all_markers from shapely.geometry import Polygon
from shapely.geometry import Polygon, LineString, Point
# get_relation returns the geojson data for a selected body of water def get_relation(self, body_of_water: str):
def get_relation(self, body_of_water: string):
# Load GeoJSON data using geopandas # Load GeoJSON data using geopandas
geo_data = gpd.read_file("server/map/mjosa.geojson") geo_data = gpd.read_file("server/map/mjosa.geojson")
# Filter only polygons, exclude points # Filter only polygons, exclude points and other feature types
polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon'] polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
# Convert GeoDataFrame to dictionary # Extract coordinates from polygons and create polygon objects
geojson_dict = json.loads(polygon_data.to_json()) polygons = [Polygon(polygon.exterior) for polygon in polygon_data['geometry']]
if len(polygons) <= 1:
print("Failed to convert to polygons")
tiles = []
for polygon in polygons:
tiles.extend(divide_relation(polygon)) # Divide each polygon from relation and append to tiles
# Convert polygon coordinates to lists
tiles_json = [list(tile.exterior.coords) for tile in tiles]
# Convert response data to JSON string # Convert response data to JSON string
response_json = json.dumps(divide_relation(self, polygon_data)) response_json = json.dumps(tiles_json)
# Set headers # Set headers
self.send_response(200) self.send_response(200)
...@@ -28,54 +36,30 @@ def get_relation(self, body_of_water: string): ...@@ -28,54 +36,30 @@ def get_relation(self, body_of_water: string):
self.wfile.write(response_json.encode('utf-8')) self.wfile.write(response_json.encode('utf-8'))
def divide_relation(self, polygon): def divide_relation(polygon):
# Fetch measurements from database # Define tile size
measurements = get_all_markers(self.cursor, False, 'Mjosa') tile_size = 0.01
# Temporary hardcoded point at which relation is split, should be x_min, y_min, x_max, y_max = polygon.bounds
splitting_points = [(60.6853, 10.9571), (60.7018, 11.1058)] rows = int((y_max - y_min) / tile_size)
cols = int((x_max - x_min) / tile_size)
# Lists to hold new polygons
divided_relation = [] tiles = []
temp_seg_1 = []
temp_seg_2 = [] if rows == 0 or cols == 0: # Return if the polygon is too small
return []
# Loop through polygon coordinates and split at defined coordinates
for i in range(len(polygon)): for row in range(rows):
for col in range(cols):
closest_point = [] tile_bbox = Polygon([
(x_min + col * tile_size, y_min + row * tile_size),
# Find the closest point on the polygon to the splitting point (x_min + (col + 1) * tile_size, y_min + row * tile_size),
for point in splitting_points: (x_min + (col + 1) * tile_size, y_min + (row + 1) * tile_size),
closest_point = polygon.exterior.interpolate(polygon.exterior.project(Point(point)),normalized=True) (x_min + col * tile_size, y_min + (row + 1) * tile_size)
])
# Check if the closest point is already in the polygon coordinates tiles.append(tile_bbox)
if closest_point.coords[0] not in polygon:
# If not, add it to the polygon coordinates if len(tiles) <= 1:
polygon.append(closest_point.coords[0]) print("Failed to divide polygon into tiles")
# Add the splitting point (either the original or the closest) to the splitting points list
splitting_points.append(closest_point.coords[0]) return tiles
current_point = polygon[i]
next_point = polygon[(i + 1) % len(polygon)] # Circular index
# Check if the current segment crosses any splitting point
for splitting_point in splitting_points:
if (current_point != splitting_point and
next_point != splitting_point and
LineString([current_point, next_point]).intersects(Point(splitting_point))):
# Split the segment at the splitting point
temp_seg_1.append(current_point)
temp_seg_2.append(splitting_point)
temp_seg_1.append(splitting_point)
temp_seg_2.append(next_point)
break
else:
# If the segment doesn't cross any splitting point, add it to both lists
temp_seg_1.append(current_point)
temp_seg_2.append(current_point)
# Return a list containing all the created polygons
divided_relation = [Polygon(temp_seg_1), Polygon(temp_seg_2)]
return divided_relation
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment