Skip to content

Commit

Permalink
Restructure JSON Deser
Browse files Browse the repository at this point in the history
  • Loading branch information
DhimanSeal-Groww committed Mar 28, 2024
1 parent 03e1def commit ec65ea8
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 40 deletions.
112 changes: 73 additions & 39 deletions lib/src/models/geojson_feature_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,54 +156,26 @@ class GeoJsonFeatureGeometry {
factory GeoJsonFeatureGeometry.fromJson(Map<String, dynamic> json) {
final dynamic type = json['type'];
final dynamic coordinates = json['coordinates'];

if (coordinates is List<dynamic>) {
if (coordinates.first is List<dynamic>) {
if (coordinates.first.first is List<double>) {
// For Isochrone feature geometry, it has a list of list of coordinates.
final List<List<ORSCoordinate>> parsedCoordinates = coordinates
.map<List<ORSCoordinate>>(
(dynamic c) => (c as List<List<double>>)
.map<ORSCoordinate>(
(List<double> c) => ORSCoordinate.fromList(c),
)
.toList(),
)
.toList();
return GeoJsonFeatureGeometry(
type: type,
coordinates: parsedCoordinates,
internalType: GsonFeatureGeometryCoordinatesType.listList,
);
final List<dynamic> dynamicList = coordinates;
if (dynamicList.first is List<dynamic>) {
final List<List<dynamic>> dynamicListList = dynamicList
.map<List<dynamic>>((dynamic c) => c as List<dynamic>)
.toList();
// For Isochrone feature geometry, it has a list of list of coordinates.
if (dynamicListList.first.first is List<dynamic>) {
return _generateIsochroneGeometry(type, dynamicListList);
}

// For direction feature geometry, it has a list of coordinates.
if (coordinates.first is List<double>) {
final List<List<ORSCoordinate>> parsedCoordinates =
(coordinates as List<List<double>>)
.map<List<ORSCoordinate>>(
(List<double> c) => <ORSCoordinate>[
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: <List<ORSCoordinate>>[
<ORSCoordinate>[ORSCoordinate.fromList(coordinates)],
],
internalType: GsonFeatureGeometryCoordinatesType.single,
);
return _generatePointGeometry(type, coordinates);
}

final GsonFeatureGeometryCoordinatesType internalType;
Expand Down Expand Up @@ -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<List<dynamic>> dynamicListList,
) {
final List<List<ORSCoordinate>> coordinateListList = dynamicListList
.map<List<List<dynamic>>>(
(List<dynamic> c) =>
c.map<List<dynamic>>((dynamic c) => c as List<dynamic>).toList(),
)
.map<List<List<num>>>(
(List<List<dynamic>> c) => c
.map<List<num>>(
(List<dynamic> c) =>
c.map<num>((dynamic c) => c as num).toList(),
)
.toList(),
)
.map<List<ORSCoordinate>>(
(List<List<num>> c) => c
.map<ORSCoordinate>((List<num> 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<List<dynamic>> dynamicListList,
) {
final List<ORSCoordinate> coordinateList = dynamicListList
.map<ORSCoordinate>(
(List<dynamic> c) => ORSCoordinate.fromList(
c.map<double>((dynamic c) => (c as num).toDouble()).toList(),
),
)
.toList();
return GeoJsonFeatureGeometry(
type: type,
coordinates: <List<ORSCoordinate>>[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: <List<ORSCoordinate>>[
<ORSCoordinate>[coordinate],
],
internalType: GsonFeatureGeometryCoordinatesType.single,
);
}
}

enum GsonFeatureGeometryCoordinatesType { listList, list, single }
13 changes: 12 additions & 1 deletion test/miscellaneous/geojson_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand Down

0 comments on commit ec65ea8

Please sign in to comment.