From 0db7d92eac3e3dd1e2a3920adb45407f542bf4c7 Mon Sep 17 00:00:00 2001 From: Dhi13man Date: Fri, 29 Mar 2024 00:57:58 +0530 Subject: [PATCH] Restructure JSON Deser --- lib/src/models/geojson_feature_models.dart | 112 ++++++++++++++------- test/miscellaneous/geojson_test.dart | 13 ++- test/open_route_service_test.dart | 2 +- 3 files changed, 86 insertions(+), 41 deletions(-) diff --git a/lib/src/models/geojson_feature_models.dart b/lib/src/models/geojson_feature_models.dart index 2fbf23c..c4cde92 100644 --- a/lib/src/models/geojson_feature_models.dart +++ b/lib/src/models/geojson_feature_models.dart @@ -156,54 +156,26 @@ class GeoJsonFeatureGeometry { factory GeoJsonFeatureGeometry.fromJson(Map json) { final dynamic type = json['type']; final dynamic coordinates = json['coordinates']; - if (coordinates is List) { - if (coordinates.first is List) { - if (coordinates.first.first is List) { - // For Isochrone feature geometry, it has a list of list of coordinates. - final List> parsedCoordinates = coordinates - .map>( - (dynamic c) => (c as List>) - .map( - (List c) => ORSCoordinate.fromList(c), - ) - .toList(), - ) - .toList(); - return GeoJsonFeatureGeometry( - type: type, - coordinates: parsedCoordinates, - internalType: GsonFeatureGeometryCoordinatesType.listList, - ); + final List dynamicList = coordinates; + if (dynamicList.first is List) { + final List> dynamicListList = dynamicList + .map>((dynamic c) => c as List) + .toList(); + // For Isochrone feature geometry, it has a list of list of coordinates. + if (dynamicListList.first.first is List) { + return _generateIsochroneGeometry(type, dynamicListList); } // For direction feature geometry, it has a list of coordinates. - if (coordinates.first is List) { - final List> parsedCoordinates = - (coordinates as List>) - .map>( - (List c) => [ - ORSCoordinate.fromList(c), - ], - ) - .toList(); - return GeoJsonFeatureGeometry( - type: type, - coordinates: parsedCoordinates, - internalType: GsonFeatureGeometryCoordinatesType.list, - ); + if (dynamicListList.first.first is num) { + return _generateDirectionGeometry(type, dynamicListList); } } } // For Point feature geometry, it has a single coordinate. - return GeoJsonFeatureGeometry( - type: type, - coordinates: >[ - [ORSCoordinate.fromList(coordinates)], - ], - internalType: GsonFeatureGeometryCoordinatesType.single, - ); + return _generatePointGeometry(type, coordinates); } final GsonFeatureGeometryCoordinatesType internalType; @@ -259,6 +231,68 @@ class GeoJsonFeatureGeometry { @override String toString() => toJson().toString(); + + /// For Isochrone feature geometry, it has a list of list of coordinates. + static GeoJsonFeatureGeometry _generateIsochroneGeometry( + String type, + List> dynamicListList, + ) { + final List> coordinateListList = dynamicListList + .map>>( + (List c) => + c.map>((dynamic c) => c as List).toList(), + ) + .map>>( + (List> c) => c + .map>( + (List c) => + c.map((dynamic c) => c as num).toList(), + ) + .toList(), + ) + .map>( + (List> c) => c + .map((List c) => ORSCoordinate.fromList(c)) + .toList(), + ) + .toList(); + return GeoJsonFeatureGeometry( + type: type, + coordinates: coordinateListList, + internalType: GsonFeatureGeometryCoordinatesType.listList, + ); + } + + /// For direction feature geometry, it has a list of coordinates. + static _generateDirectionGeometry( + String type, + List> dynamicListList, + ) { + final List coordinateList = dynamicListList + .map( + (List c) => ORSCoordinate.fromList( + c.map((dynamic c) => (c as num).toDouble()).toList(), + ), + ) + .toList(); + return GeoJsonFeatureGeometry( + type: type, + coordinates: >[coordinateList], + internalType: GsonFeatureGeometryCoordinatesType.list, + ); + } + + /// For Point feature geometry, it has a single coordinate. + static _generatePointGeometry(String type, dynamic coordinates) { + final ORSCoordinate coordinate = ORSCoordinate.fromList(coordinates); + return GeoJsonFeatureGeometry( + type: type, + coordinates: >[ + [coordinate], + ], + internalType: GsonFeatureGeometryCoordinatesType.single, + ); + } } enum GsonFeatureGeometryCoordinatesType { listList, list, single } diff --git a/test/miscellaneous/geojson_test.dart b/test/miscellaneous/geojson_test.dart index 173655e..b161348 100644 --- a/test/miscellaneous/geojson_test.dart +++ b/test/miscellaneous/geojson_test.dart @@ -69,9 +69,20 @@ void geoJsonTests() { expect(result.bbox, expected.bbox); expect(result.properties, expected.properties); expect(result.type, expected.type); - expect(result.geometry.coordinates, expected.geometry.coordinates); expect(result.geometry.internalType, expected.geometry.internalType); expect(result.geometry.type, expected.geometry.type); + for (int i = 0; i < result.geometry.coordinates.length; i++) { + for (int j = 0; j < result.geometry.coordinates[i].length; j++) { + expect( + result.geometry.coordinates[i][j].latitude, + expected.geometry.coordinates[i][j].latitude, + ); + expect( + result.geometry.coordinates[i][j].longitude, + expected.geometry.coordinates[i][j].longitude, + ); + } + } }); test('Test GeoJSON Coordinate Point serialization and deserialization', () { diff --git a/test/open_route_service_test.dart b/test/open_route_service_test.dart index 7071c9c..4022f87 100644 --- a/test/open_route_service_test.dart +++ b/test/open_route_service_test.dart @@ -15,7 +15,7 @@ import 'package:open_route_service/open_route_service.dart'; Future main() async { // TODO: Change the API key to your own API key to ensure that package works. - String apiKey = 'test'; + String apiKey = '5b3ce3597851110001cf6248b44701010e9844649bdfa3d8d0f5633a'; // Change API key from environment if tests are running on Github Actions. if ((Platform.environment['EXEC_ENV'] ?? '') == 'github_actions') {