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