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

update: minor map update

parent a7739fca
No related branches found
No related tags found
1 merge request!3Choropleth map implementation
This diff is collapsed.
import 'dart:typed_data';
import 'package:app/pages/consts.dart'; import 'package:app/pages/consts.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_maps/maps.dart'; import 'package:syncfusion_flutter_maps/maps.dart';
import 'package:flutter/services.dart'; import '../consts.dart';
import 'dart:convert';
import 'dart:io';
/// A class containing thickness for each subdivision of the map. /// A class containing thickness for each subdivision of the map.
class IceThicknessModel { class IceThicknessModel {
...@@ -24,19 +27,66 @@ class ChoroplethMap extends StatefulWidget { ...@@ -24,19 +27,66 @@ class ChoroplethMap extends StatefulWidget {
class _ChoroplethMapState extends State<ChoroplethMap> { class _ChoroplethMapState extends State<ChoroplethMap> {
late MapShapeSource mapShapeSource; late MapShapeSource mapShapeSource;
List<IceThicknessModel> iceThicknessList = <IceThicknessModel>[]; List<IceThicknessModel> iceThicknessList = <IceThicknessModel>[];
Uint8List? relation;
@override
void initState() {
super.initState();
fetchRelation(); // Fetch relation data from server
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return const SfMaps( return relation != null
? SfMaps(
layers: [ layers: [
MapShapeLayer( MapShapeLayer(
source: MapShapeSource.asset( source: MapShapeSource.memory(
'assets/australia.json', relation!,
shapeDataField: 'STATE_NAME', shapeDataField: 'name',
), ),
color: Colors.orange, color: Colors.orange,
), ),
], ],
); ) : const Center(child: CircularProgressIndicator());
}
// Fetch relation data from server
Future<void> fetchRelation() async {
try {
// Custom HTTP client
HttpClient client = HttpClient()
..badCertificateCallback = // NB: temporary disable SSL certificate validation
(X509Certificate cert, String host, int port) => true;
// Execute request to to get_relation endpoint
var request = await client.getUrl(Uri.parse('${serverURI}get_relation'));
var response = await request.close(); // Close response body at end of function
// Parse body to JSON if request is ok
if (response.statusCode == 200) {
var responseBody = await response.transform(utf8.decoder).join();
if (responseBody.isNotEmpty) {
var jsonData = json.decode(responseBody);
if (jsonData != null) {
print("JSON data NOT NULL");
// Convert JSON string to Uint8List
setState(() {
relation = Uint8List.fromList(utf8.encode(jsonData));
}); // Update UI
} else {
throw Exception('Failed to parse response body');
}
} else {
throw Exception('Response body is empty');
}
} else {
throw Exception('Failed to fetch relation data: Status code ${response.statusCode}');
}
} catch (e) {
throw Exception('Failed to fetch relation data: ${e.toString()}');
}
} }
} }
No preview for this file type
...@@ -9,7 +9,7 @@ def get_relation(self, body_of_water: string): ...@@ -9,7 +9,7 @@ def get_relation(self, body_of_water: string):
# 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 # Filter only polygons, exclude points
polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon'] polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
# Plot polygons # Plot polygons
...@@ -19,7 +19,7 @@ def get_relation(self, body_of_water: string): ...@@ -19,7 +19,7 @@ def get_relation(self, body_of_water: string):
plt.ylabel('Latitude') plt.ylabel('Latitude')
plt.show() plt.show()
# Convert GeoDataFrame to GeoJSON-like dictionary # Convert GeoDataFrame to dictionary
geojson_dict = json.loads(polygon_data.to_json()) geojson_dict = json.loads(polygon_data.to_json())
# Convert response data to JSON string # Convert response data to JSON string
......
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