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
Source diff could not be displayed: it is too large. Options to address this: view the blob.
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,13 +20,14 @@ 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):
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']
new_polygons = []
# Add group IDs to lake relation
# Loop through each measurement and create groupings of subdivisions
for measurement in data:
subdiv_list = []
......@@ -45,21 +45,24 @@ def create_groups(relation_file: str, data: list):
sorted_list = sorted(subdiv_list, key=lambda x: x[0])
current_group = -1 # Current group_id
new_shape = []
new_shape = [] # List of subdivision geometries for current group
# While group_id is same -> merge all polygons and give each a group id
# 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 MultiPolygon to Polygon
# Convert to Polygon
if isinstance(merged_polygon, MultiPolygon):
merged_polygon = merged_polygon.convex_hull
# Add following structure to new polygon
# Structure the new polygon
merged_polygon_structure = {
"type": "Feature",
"properties": {
......@@ -71,15 +74,12 @@ def create_groups(relation_file: str, data: list):
}
}
# 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
# Update current group to new group_id and reset new_shape for next group
current_group = element[0]
new_shape = []
# Append all new shapes
relation_data = relation_data.append(new_polygons, ignore_index=True)
new_shape = [element[1]]
# Convert GeoDataFrame to JSON
relation_data_json = json.loads(relation_data.to_json())
......@@ -87,6 +87,9 @@ def create_groups(relation_file: str, data: list):
# 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)]
def get_id_and_center(file_name): # NB buggy
......
......@@ -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.
Please register or to comment