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

fix: shapeDataField value

parent f165345d
No related branches found
No related tags found
2 merge requests!5Clhp map,!4Clhp map
......@@ -45,7 +45,7 @@ class _ChoroplethMapState extends State<ChoroplethMap> {
MapShapeLayer(
source: MapShapeSource.memory(
widget.relation,
shapeDataField: 'SubDivID',
shapeDataField: 'geometry',
),
color: Colors.orange,
zoomPanBehavior: _zoomPanBehavior,
......
No preview for this file type
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
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