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

update: progress in new group implementation

parent 3ad18fe6
No related branches found
No related tags found
No related merge requests found
import geopandas as gpd import geopandas as gpd
from shapely.geometry import Polygon, LineString, MultiLineString from shapely.geometry import Polygon, MultiPolygon
from shapely.ops import linemerge, unary_union, polygonize 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
...@@ -19,6 +19,7 @@ def fetch_divided_map(self, file_name): ...@@ -19,6 +19,7 @@ def fetch_divided_map(self, file_name):
self.wfile.write(data.encode('utf-8')) self.wfile.write(data.encode('utf-8'))
# 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 # Read lake relation from json file
geo_data = gpd.read_file("server/lake_relations/" + relation_file + "_div.json") geo_data = gpd.read_file("server/lake_relations/" + relation_file + "_div.json")
...@@ -40,29 +41,44 @@ def create_groups(relation_file: str, data: list): ...@@ -40,29 +41,44 @@ def create_groups(relation_file: str, data: list):
if feature['sub_div_id'] == subDivID: if feature['sub_div_id'] == subDivID:
subdiv_list.append((group_id, Polygon([feature['coordinates']]))) subdiv_list.append((group_id, Polygon([feature['coordinates']])))
# Sort subdiv_list in ascending order of group_ids # Sort subdiv_list based on group_ids
# While group_id is same -> merge all polygons and give each a group id sorted_list = sorted(subdiv_list, key=lambda x: x[0])
# new_polygons.append((group_id, new_shape))
# Put the coordinates and group id of each new polygon in the expected structure
for polygon in new_polygons:
'''
Expected structure:
{
"type": "Feature",
"properties": {
"group_id": "0",
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
10.4304,...
'''
current_group = -1 # Current group_id
# While group_id is same -> merge all polygons and give each a group id
for element in sorted_list:
new_shape = []
if element[0] == current_group:
new_shape.append(element[1])
elif current_group > -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
'''
Expected structure:
{
"type": "Feature",
"properties": {
"group_id": "0",
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
10.4304,...
'''
# Append to new_polygons
current_group = element[0] # Update current group to new group_id
else:
current_group = element[0]
# Convert GeoDataFrame to JSON # Convert GeoDataFrame to 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