Skip to content

Commit

Permalink
Merge pull request #33 from divvitco/fix-polygon-fromjson
Browse files Browse the repository at this point in the history
`fromJson` method fix with casting
  • Loading branch information
ntatko authored Apr 5, 2024
2 parents 2e5ec67 + fbc4579 commit 542f096
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/src/featureTypes/feature_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FeatureCollection {
}

return FeatureCollection(
(json['features'] as List<Map<String, dynamic>>)
(List<Map<String, dynamic>>.from(json['features']))
.map((Map<String, dynamic> f) {
if (f['geometry']['type'] == 'Point') {
return Point.fromJson(f);
Expand Down
18 changes: 11 additions & 7 deletions lib/src/featureTypes/multi_polygon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,20 @@ class MultiPolygon extends Feature {
throw ArgumentError('json is not a MultiPolygon');
}

return MultiPolygon(
(json['geometry']['coordinates'] as List<List<List<List<double>>>>)
.map((dynamic poly) => (poly as List<List<List<double>>>)
.map((dynamic shape) => LinearRing((shape as List<List<double>>)
.map((dynamic coord) => Coordinate.fromJson(coord))
MultiPolygon poly = MultiPolygon(
(json['geometry']['coordinates'] as List)
.map((poly) => (poly as List)
.map((shape) => LinearRing((shape as List)
.map((coord) => Coordinate.fromJson((coord as List)
.map((e) => (e is int ? e.toDouble() : e as double))
.toList()))
.toList()))
.toList())
.toList(),
.toList()) // Convert the outer Iterable to a List
.toList(), // Convert the inner Iterable to a List
properties: Map<String, dynamic>.from(json['properties']),
);

return poly;
}

/// Creates a [MultiPolygon] from a WKT [String].
Expand Down
13 changes: 8 additions & 5 deletions lib/src/featureTypes/polygon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ class Polygon extends Feature {
throw ArgumentError('json is not a Polygon');
}

List<LinearRing> rings =
(json['geometry']['coordinates'] as List<List<dynamic>>)
.map((List<dynamic> shape) => LinearRing(
shape.map((dynamic c) => Coordinate.fromJson(c)).toList()))
.toList();
List<LinearRing> rings = (json['geometry']['coordinates'] as List)
.map((shape) => LinearRing((shape as List)
.map((c) => Coordinate.fromJson((c as List)
.map((e) => (e is int ? e.toDouble() : e as double))
.toList()))
.toList()))
.toList();

return Polygon(rings,
properties: Map<String, dynamic>.from(json['properties']));
}
Expand Down

0 comments on commit 542f096

Please sign in to comment.