-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathflip.dart
More file actions
55 lines (50 loc) · 1.58 KB
/
Copy pathflip.dart
File metadata and controls
55 lines (50 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import 'package:turf/turf.dart';
import 'package:turf/meta.dart';
/// Swap lat/lng of a Position
Position flipPosition(Position pos) => Position.named(
lat: pos.lng,
lng: pos.lat,
);
/// Flip the coordinates of a Feature, with proper typing for each geometry
Feature<GeometryType> flip(Feature<GeometryType>? geojson) {
if (geojson == null) {
throw ArgumentError('geojson cannot be null');
}
final geometry = geojson.geometry;
if (geometry == null) return geojson;
if (geometry is Point) {
geojson.geometry = Point(
coordinates: flipPosition(geometry.coordinates),
);
} else if (geometry is MultiPoint) {
geojson.geometry = MultiPoint(
coordinates: geometry.coordinates.map(flipPosition).toList(),
);
} else if (geometry is LineString) {
geojson.geometry = LineString(
coordinates: geometry.coordinates.map(flipPosition).toList(),
);
} else if (geometry is MultiLineString) {
geojson.geometry = MultiLineString(
coordinates: geometry.coordinates
.map((line) => line.map(flipPosition).toList())
.toList(),
);
} else if (geometry is Polygon) {
geojson.geometry = Polygon(
coordinates: geometry.coordinates
.map((ring) => ring.map(flipPosition).toList())
.toList(),
);
} else if (geometry is MultiPolygon) {
geojson.geometry = MultiPolygon(
coordinates: geometry.coordinates
.map(
(polygon) =>
polygon.map((ring) => ring.map(flipPosition).toList()).toList(),
)
.toList(),
);
}
return geojson;
}