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

fix: working Mjøsa choropleth map!

parent 75c89c93
No related branches found
No related tags found
1 merge request!3Choropleth map implementation
...@@ -4,6 +4,8 @@ import 'widgets/map_widget.dart'; ...@@ -4,6 +4,8 @@ import 'widgets/map_widget.dart';
import 'marker_handler/marker_data.dart'; import 'marker_handler/marker_data.dart';
import 'consts.dart'; import 'consts.dart';
import 'marker_handler/get_markers.dart'; import 'marker_handler/get_markers.dart';
import 'marker_handler/get_relation.dart';
import 'dart:typed_data';
class DefaultPage extends StatefulWidget { class DefaultPage extends StatefulWidget {
const DefaultPage({super.key}); const DefaultPage({super.key});
...@@ -17,14 +19,17 @@ class _DefaultPageState extends State<DefaultPage> { ...@@ -17,14 +19,17 @@ class _DefaultPageState extends State<DefaultPage> {
bool showBar = false; bool showBar = false;
List<Measurement> markerList = []; List<Measurement> markerList = [];
Uint8List relation = Uint8List(0);
// Call fetchMarkerTemplate and await its result before setting the state // Call fetchMarkerTemplate and await its result before setting the state
Future<void> loadMarkerList() async { Future<void> loadMarkerList() async {
try { try {
List<Measurement> fetchedMarkers = await fetchMarkerData(); List<Measurement> fetchedMarkers = await fetchMarkerData();
Uint8List fetchedRelation = await fetchRelation();
setState(() { setState(() { // Initialise markers and relations
markerList = fetchedMarkers; markerList = fetchedMarkers;
relation = fetchedRelation;
}); });
} catch (e) { } catch (e) {
showDialog( showDialog(
...@@ -95,7 +100,7 @@ class _DefaultPageState extends State<DefaultPage> { ...@@ -95,7 +100,7 @@ class _DefaultPageState extends State<DefaultPage> {
), ),
body: ListView( body: ListView(
children: [ // Add main widget children: [ // Add main widget
MapContainerWidget(markerList: markerList), MapContainerWidget(markerList: markerList, relation: relation),
], ],
), ),
), ),
......
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import '../consts.dart';
import 'dart:typed_data';
/// Fetch relation data from server
Future<Uint8List> 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) {
return Uint8List.fromList(utf8.encode(responseBody));
} 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()}');
}
}
...@@ -3,8 +3,7 @@ import 'package:app/pages/consts.dart'; ...@@ -3,8 +3,7 @@ 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 '../consts.dart'; import '../consts.dart';
import 'dart:convert'; import 'dart:typed_data';
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 {
...@@ -18,7 +17,9 @@ class IceThicknessModel { ...@@ -18,7 +17,9 @@ class IceThicknessModel {
/// The map data is fetched from the server, and the map is rendered /// The map data is fetched from the server, and the map is rendered
/// using the Syncfusion Flutter Maps library. /// using the Syncfusion Flutter Maps library.
class ChoroplethMap extends StatefulWidget { class ChoroplethMap extends StatefulWidget {
const ChoroplethMap({Key? key,}) : super(key: key); const ChoroplethMap({Key? key, required this.relation}) : super(key: key);
final Uint8List relation;
@override @override
_ChoroplethMapState createState() => _ChoroplethMapState(); _ChoroplethMapState createState() => _ChoroplethMapState();
...@@ -27,66 +28,24 @@ class ChoroplethMap extends StatefulWidget { ...@@ -27,66 +28,24 @@ 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 @override
void initState() { void initState() {
super.initState(); super.initState();
fetchRelation(); // Fetch relation data from server
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return relation != null return SfMaps(
? SfMaps(
layers: [ layers: [
MapShapeLayer( MapShapeLayer(
source: MapShapeSource.memory( source: MapShapeSource.memory(
relation!, widget.relation,
shapeDataField: '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()}');
}
} }
} }
...@@ -6,14 +6,17 @@ import 'stat_charts.dart'; ...@@ -6,14 +6,17 @@ import 'stat_charts.dart';
import 'sat_layer.dart'; import 'sat_layer.dart';
import 'package:flutter_map/flutter_map.dart'; import 'package:flutter_map/flutter_map.dart';
import 'cloropleth_map.dart'; import 'cloropleth_map.dart';
import 'dart:typed_data';
/// MapContainerWidget is the main widget that contains the map with all /// MapContainerWidget is the main widget that contains the map with all
/// its layers, polygons and markers. /// its layers, polygons and markers.
class MapContainerWidget extends StatefulWidget { class MapContainerWidget extends StatefulWidget {
final List<Measurement> markerList; final List<Measurement> markerList;
final Uint8List relation;
const MapContainerWidget({Key? key, const MapContainerWidget({Key? key,
required this.markerList, required this.markerList,
required this.relation,
}) : super(key: key); }) : super(key: key);
@override @override
...@@ -59,7 +62,7 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { ...@@ -59,7 +62,7 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
SizedBox( SizedBox(
width: screenWidth * boxWidth, width: screenWidth * boxWidth,
height: screenWidth * boxHeight, height: screenWidth * boxHeight,
child: ChoroplethMap(), child: ChoroplethMap(relation: widget.relation),
/*FlutterMap( /*FlutterMap(
options: MapOptions( options: MapOptions(
center: mapCenter, center: mapCenter,
......
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