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
No related branches found
No related tags found
2 merge requests!5Clhp map,!4Clhp map
No preview for this file type
import string
import json
import geopandas as gpd
from server.map.get_markers import get_all_markers
from shapely.geometry import Polygon, LineString, Point
from shapely.geometry import Polygon
# get_relation returns the geojson data for a selected body of water
def get_relation(self, body_of_water: string):
def get_relation(self, body_of_water: str):
# Load GeoJSON data using geopandas
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']
# Convert GeoDataFrame to dictionary
geojson_dict = json.loads(polygon_data.to_json())
# Extract coordinates from polygons and create polygon objects
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
response_json = json.dumps(divide_relation(self, polygon_data))
response_json = json.dumps(tiles_json)
# Set headers
self.send_response(200)
......@@ -28,54 +36,30 @@ def get_relation(self, body_of_water: string):
self.wfile.write(response_json.encode('utf-8'))
def divide_relation(self, polygon):
# Fetch measurements from database
measurements = get_all_markers(self.cursor, False, 'Mjosa')
# Temporary hardcoded point at which relation is split, should be
splitting_points = [(60.6853, 10.9571), (60.7018, 11.1058)]
# Lists to hold new polygons
divided_relation = []
temp_seg_1 = []
temp_seg_2 = []
# Loop through polygon coordinates and split at defined coordinates
for i in range(len(polygon)):
closest_point = []
# Find the closest point on the polygon to the splitting point
for point in splitting_points:
closest_point = polygon.exterior.interpolate(polygon.exterior.project(Point(point)),normalized=True)
# Check if the closest point is already in the polygon coordinates
if closest_point.coords[0] not in polygon:
# If not, add it to the polygon coordinates
polygon.append(closest_point.coords[0])
# Add the splitting point (either the original or the closest) to the splitting points list
splitting_points.append(closest_point.coords[0])
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
def divide_relation(polygon):
# Define tile size
tile_size = 0.01
x_min, y_min, x_max, y_max = polygon.bounds
rows = int((y_max - y_min) / tile_size)
cols = int((x_max - x_min) / tile_size)
tiles = []
if rows == 0 or cols == 0: # Return if the polygon is too small
return []
for row in range(rows):
for col in range(cols):
tile_bbox = Polygon([
(x_min + col * tile_size, y_min + row * tile_size),
(x_min + (col + 1) * tile_size, y_min + row * tile_size),
(x_min + (col + 1) * tile_size, y_min + (row + 1) * tile_size),
(x_min + col * tile_size, y_min + (row + 1) * tile_size)
])
tiles.append(tile_bbox)
if len(tiles) <= 1:
print("Failed to divide polygon into tiles")
return tiles
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