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> { ...@@ -45,7 +45,7 @@ class _ChoroplethMapState extends State<ChoroplethMap> {
MapShapeLayer( MapShapeLayer(
source: MapShapeSource.memory( source: MapShapeSource.memory(
widget.relation, widget.relation,
shapeDataField: 'SubDivID', shapeDataField: 'geometry',
), ),
color: Colors.orange, color: Colors.orange,
zoomPanBehavior: _zoomPanBehavior, zoomPanBehavior: _zoomPanBehavior,
......
No preview for this file type
import json
import geopandas as gpd import geopandas as gpd
import pandas as pd
from shapely.geometry import Polygon from shapely.geometry import Polygon
...@@ -8,25 +6,21 @@ def get_relation(self, body_of_water: str): ...@@ -8,25 +6,21 @@ def get_relation(self, body_of_water: str):
# Load GeoJSON data using geopandas # Load GeoJSON data using geopandas
geo_data = gpd.read_file("server/map/mjosa.geojson") 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'] polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
# Extract coordinates from polygons and create polygon objects # Extract coordinates from polygons and create polygon objects
polygons = [Polygon(polygon.exterior) for polygon in polygon_data['geometry']] 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") print("Failed to convert to polygons")
return return
# Divide relation into tiles and append tiles to relation object # Divide relation into tiles
# tiles = divide_relation(polygons) 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)
# Parse geojson to json -> syncfusion_flutter_maps lib requires json # Convert GeoDataFrame to GeoJSON
geojson_dict = json.loads(polygon_data.to_json()) tiles_json = polygon_data.to_json()
response_json = json.dumps(geojson_dict)
# Set headers # Set headers
self.send_response(200) self.send_response(200)
...@@ -34,13 +28,13 @@ def get_relation(self, body_of_water: str): ...@@ -34,13 +28,13 @@ def get_relation(self, body_of_water: str):
self.end_headers() self.end_headers()
# Write coordinates to response object # 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): def divide_relation(polygons):
# Define tile size # Define tile size
tile_size = 0.01 tile_size = 0.01
id = 1 subdiv_id = 0
tiles = [] tiles = []
for polygon in polygons: for polygon in polygons:
...@@ -53,28 +47,20 @@ def divide_relation(polygons): ...@@ -53,28 +47,20 @@ def divide_relation(polygons):
for row in range(rows): for row in range(rows):
for col in range(cols): 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 * 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 * tile_size),
(x_min + (col + 1) * tile_size, y_min + (row + 1) * 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) (x_min + col * tile_size, y_min + (row + 1) * tile_size)
]) ])
tiles.append({"SubDivID": id, "polygon": tile_bbox}) tiles.append({"SubDivID": subdiv_id, "geometry": tile_bbox})
id += 1 subdiv_id += 1
if len(tiles) <= 1: if len(tiles) <= 1: # Return empty object if division fails
print("Failed to divide polygons into tiles") print("Failed to divide polygons into tiles")
return []
# Format each tile object with coordinate, type, and subdivision id tags # Convert tiles list to a GeoDataFrame
tiles_json = [] tiles_df = gpd.GeoDataFrame(tiles, geometry='geometry')
for tile in tiles:
coordinates = list(tile["polygon"].exterior.coords) return tiles_df
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
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