diff --git a/app/lib/pages/widgets/cloropleth_map.dart b/app/lib/pages/widgets/cloropleth_map.dart index 9f98e9a4bb09924138f7c0b7cdaccd2bf6c848b7..ea67a65ed16a4c76d82472387ec14195790fb684 100644 --- a/app/lib/pages/widgets/cloropleth_map.dart +++ b/app/lib/pages/widgets/cloropleth_map.dart @@ -45,7 +45,7 @@ class _ChoroplethMapState extends State<ChoroplethMap> { MapShapeLayer( source: MapShapeSource.memory( widget.relation, - shapeDataField: 'SubDivID', + shapeDataField: 'geometry', ), color: Colors.orange, zoomPanBehavior: _zoomPanBehavior, diff --git a/server/map/__pycache__/get_relation.cpython-311.pyc b/server/map/__pycache__/get_relation.cpython-311.pyc index 62331622ce7b3cf90124995d38175c5c4871b726..58dc7c0fd6a0e49edaa4dba3c77344d838746358 100644 Binary files a/server/map/__pycache__/get_relation.cpython-311.pyc and b/server/map/__pycache__/get_relation.cpython-311.pyc differ diff --git a/server/map/get_relation.py b/server/map/get_relation.py index 0514bbc1d219ef32cec9b99f542c076e3bbb2a03..38cb963fa2bab23888816e0a87330677fefd4f29 100644 --- a/server/map/get_relation.py +++ b/server/map/get_relation.py @@ -1,6 +1,4 @@ -import json import geopandas as gpd -import pandas as pd from shapely.geometry import Polygon @@ -8,25 +6,21 @@ def get_relation(self, body_of_water: str): # Load GeoJSON data using geopandas geo_data = gpd.read_file("server/map/mjosa.geojson") - # Filter only polygons, exclude points and other feature types + # 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: + if len(polygons) <= 1: # Return if conversion to polygon fails print("Failed to convert to polygons") return - # Divide relation into tiles and append tiles to relation object - # tiles = divide_relation(polygons) - # tiles_dicts = [{"geometry": tile["polygon"], "SubDivID": tile["SubDivID"]} for tile in tiles] - # tiles_gdf = gpd.GeoDataFrame(tiles_dicts) - # combined_data = pd.concat([polygon_data, tiles_gdf], ignore_index=True) + # Divide relation into tiles + tiles = divide_relation(polygons) - # Parse geojson to json -> syncfusion_flutter_maps lib requires json - geojson_dict = json.loads(polygon_data.to_json()) - response_json = json.dumps(geojson_dict) + # Convert GeoDataFrame to GeoJSON + tiles_json = polygon_data.to_json() # Set headers self.send_response(200) @@ -34,13 +28,13 @@ def get_relation(self, body_of_water: str): self.end_headers() # Write coordinates to response object - self.wfile.write(response_json.encode('utf-8')) + self.wfile.write(tiles_json.encode('utf-8')) def divide_relation(polygons): # Define tile size tile_size = 0.01 - id = 1 + subdiv_id = 0 tiles = [] for polygon in polygons: @@ -53,28 +47,20 @@ def divide_relation(polygons): for row in range(rows): for col in range(cols): - tile_bbox = Polygon([ # Calculate coordinate for current place in column and row + tile_bbox = Polygon([ # Calculate coordinate for current place in column and row (x_min + col * tile_size, y_min + row * tile_size), (x_min + (col + 1) * tile_size, y_min + row * tile_size), (x_min + (col + 1) * tile_size, y_min + (row + 1) * tile_size), (x_min + col * tile_size, y_min + (row + 1) * tile_size) ]) - tiles.append({"SubDivID": id, "polygon": tile_bbox}) - id += 1 + tiles.append({"SubDivID": subdiv_id, "geometry": tile_bbox}) + subdiv_id += 1 - if len(tiles) <= 1: + if len(tiles) <= 1: # Return empty object if division fails print("Failed to divide polygons into tiles") + return [] - # Format each tile object with coordinate, type, and subdivision id tags - tiles_json = [] - for tile in tiles: - coordinates = list(tile["polygon"].exterior.coords) - polygon_json = { - "type": "Polygon", - "coordinates": [[(coord[0], coord[1]) for coord in coordinates]], - "SubDivID": tile["SubDivID"] - } - tiles_json.append(polygon_json) - - # Return all tiles for current polygon - return tiles_json + # Convert tiles list to a GeoDataFrame + tiles_df = gpd.GeoDataFrame(tiles, geometry='geometry') + + return tiles_df