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

add: tile width and height to mjosa_div.json

parent c7cce876
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -16,6 +16,8 @@ def cut_map(self, body_of_water: str): # NB: implement body_of_water
raise Exception("Failed to convert JSON object to Shapely Polygons")
divided_map = []
cell_width = 0
cell_height = 0
for polygon in polygons:
cell_width = 0.04
......@@ -78,8 +80,10 @@ def cut_map(self, body_of_water: str): # NB: implement body_of_water
feature_collection = {
'type': 'FeatureCollection',
'features': features,
'tile_count': sub_div_id, # Add the last subdivision ID as number of tiles
'tile_width': cell_width,
'tile_height': cell_height,
'features': features,
}
write_json_to_file("server/lake_relations", "mjosa", feature_collection)
......
import geopandas as gpd
from shapely.geometry import Polygon, MultiPolygon
from shapely.ops import linemerge, unary_union, polygonize
import json
from server.map.add_new_lake import write_json_to_file
......@@ -21,71 +20,75 @@ def fetch_divided_map(self, file_name):
# Create groups creates polygons which consist of groupings of related subdivisions
def create_groups(relation_file: str, data: list):
# Read lake relation from json file
geo_data = gpd.read_file("server/lake_relations/" + relation_file + "_div.json")
relation_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
new_polygons = []
# Add group IDs to lake relation
for measurement in data:
subdiv_list = []
for subdivision in measurement['Subdivisions']:
subDivID = str(subdivision['SubdivID']) # Convert to string to match format in feature
group_id = subdivision['GroupID'] # Extract group ID
# Find the matching subdivision in relation_data
for index, feature in relation_data.iterrows():
# Add the new group ID to the correct subdivision
if feature['sub_div_id'] == subDivID:
subdiv_list.append((group_id, Polygon([feature['coordinates']])))
# Sort subdiv_list based on group_ids
sorted_list = sorted(subdiv_list, key=lambda x: x[0])
current_group = -1 # Current group_id
new_shape = []
# While group_id is same -> merge all polygons and give each a group id
for element in sorted_list:
if element[0] == current_group:
new_shape.append(element[1])
elif len(new_shape) > 1:
merged_polygon = MultiPolygon(new_shape).buffer(0)
# Convert MultiPolygon to Polygon
if isinstance(merged_polygon, MultiPolygon):
merged_polygon = merged_polygon.convex_hull
# Add following structure to new polygon
merged_polygon_structure = {
"type": "Feature",
"properties": {
"group_id": current_group,
},
"geometry": {
"type": "Polygon",
"coordinates": [list(merged_polygon.exterior.coords)]
try:
print("Creating groups...")
# Read lake relation from json file
geo_data = gpd.read_file("server/lake_relations/" + relation_file + "_div.json")
relation_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
# Loop through each measurement and create groupings of subdivisions
for measurement in data:
subdiv_list = []
for subdivision in measurement['Subdivisions']:
subDivID = str(subdivision['SubdivID']) # Convert to string to match format in feature
group_id = subdivision['GroupID'] # Extract group ID
# Find the matching subdivision in relation_data
for index, feature in relation_data.iterrows():
# Add the new group ID to the correct subdivision
if feature['sub_div_id'] == subDivID:
subdiv_list.append((group_id, Polygon([feature['coordinates']])))
# Sort subdiv_list based on group_ids
sorted_list = sorted(subdiv_list, key=lambda x: x[0])
current_group = -1 # Current group_id
new_shape = [] # List of subdivision geometries for current group
# Merge subdivisions in a given group
for element in sorted_list:
# If the subdivision still belongs to the current group
if element[0] == current_group:
new_shape.append(element[1])
# New group id is found
elif len(new_shape) > 1:
# Merger all subdivisions for previous group into a single shape
merged_polygon = MultiPolygon(new_shape).buffer(0)
# Convert to Polygon
if isinstance(merged_polygon, MultiPolygon):
merged_polygon = merged_polygon.convex_hull
# Structure the new polygon
merged_polygon_structure = {
"type": "Feature",
"properties": {
"group_id": current_group,
},
"geometry": {
"type": "Polygon",
"coordinates": [list(merged_polygon.exterior.coords)]
}
}
}
# Append new polygon to new_polygons
new_polygons.append(merged_polygon_structure)
# Append new polygon to relation data
relation_data = relation_data.append(merged_polygon_structure, ignore_index=True)
# Update current group to new group_id and reset new_shapes for next group
current_group = element[0]
new_shape = []
# Update current group to new group_id and reset new_shape for next group
current_group = element[0]
new_shape = [element[1]]
# Append all new shapes
relation_data = relation_data.append(new_polygons, ignore_index=True)
# Convert GeoDataFrame to JSON
relation_data_json = json.loads(relation_data.to_json())
# Convert GeoDataFrame to JSON
relation_data_json = json.loads(relation_data.to_json())
# Write relation with group shapes to file
write_json_to_file("server/lake_relations", "mjosa", relation_data_json)
# Write relation with group shapes to file
write_json_to_file("server/lake_relations", "mjosa", relation_data_json)
except Exception as e:
print(f"Error in create_groups(): {e}")
# Returns a list of [(sub_div_id, sub_div_center)]
......
......@@ -3,6 +3,7 @@ from datetime import datetime
import random
import geopandas as gpd
from server.map.add_new_lake import write_json_to_file
from server.map.get_lake import create_groups
# get_markers requests all marker data or valid markers, converts the data to json, and writes
......@@ -112,7 +113,8 @@ def get_all_markers(self, cursor, waterBodyName):
# Convert dictionary values to list of measurements
data = list(measurement_data.values()) + test_measurements
######################### ADD GROUP_IDS TO RELATION DATA ##################################
# NB temporary placement
#create_groups("mjosa", data)
# Read lake relation from json file
geo_data = gpd.read_file("server/lake_relations/mjosa_div.json")
......
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