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

fix: sucessfull map division

parent 966538a1
No related branches found
No related tags found
1 merge request!6Clhp map
The following query
[out:json];
(
way["natural"="water"]["name"="lakeName"];
relation["natural"="water"]["name"="lakeName"];
);
/*added by auto repair*/
(._;>;);
/*end of auto repair*/
out body;
The following query
\ No newline at end of file
No preview for this file type
......@@ -10,14 +10,29 @@ 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
divided_map = [] # List to store map shapes while splitting
# NB: test polygon, remove after testing
polygons = [circle_polygon()]
# 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']
# 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
divided_map = [] # List to store map shapes while splitting
polygon_counter = 1
num_of_polygons = len(polygons)
print("Dividing map... This may take a few minutes. As reference, "
"a cell size of 0.01 takes approximately 13 minutes")
# Divide all polygons into sections
for polygon in polygons:
cell_size = 1
cell_size = 0.04 # NB smaller values require patience
# Divide the length and with of polygon into a grid of equally sized parts
lines = create_grid(polygon, cell_size)
......@@ -28,18 +43,24 @@ def get_relation(self, body_of_water: str): # NB: implement body_of_water
divided_map = combine_gird_with_poly(polygon, lines)
print("Polygon nr.", polygon_counter, " finished processing. ", num_of_polygons-polygon_counter,
" polygons left to process.")
polygon_counter += 1
break
tiles = gpd.GeoDataFrame(geometry=divided_map)
# NB test plot
fig, ax = plt.subplots()
ax.set_aspect(1)
ax.set_aspect(1.5)
# Plot the divided_map
tiles.plot(ax=ax, facecolor='none', edgecolor='black')
for i, tile in enumerate(tiles.geometry):
random_color = "#{:06x}".format(random.randint(0, 0xFFFFFF))
gpd.GeoSeries(tile).plot(ax=ax, facecolor=random_color, edgecolor='black')
gpd.GeoSeries(tile).plot(ax=ax, facecolor=random_color, edgecolor='none')
plt.show()
......@@ -54,24 +75,6 @@ def get_relation(self, body_of_water: str): # NB: implement body_of_water
# Write GeoJSON to response object
self.wfile.write(tiles_json.encode('utf-8'))
# NB: only for testing
def circle_polygon():
circle_points = []
num_points = 500
center_x, center_y = 0.0, 0.0
radius = 6
for i in range(num_points):
angle = 2 * np.pi * i / num_points
x = center_x + radius * np.cos(angle)
y = center_y + radius * np.sin(angle)
circle_points.append((x, y))
circle_points.append(circle_points[0]) # Close the circle
return Polygon(circle_points)
def create_grid(poly: Polygon, cell_size):
# Retrieve bounds of the entire polygon
bounds = poly.bounds
......
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