diff --git a/lib/flip.dart b/lib/flip.dart new file mode 100644 index 00000000..5591398a --- /dev/null +++ b/lib/flip.dart @@ -0,0 +1,4 @@ +library turf_flip; + +export 'package:geotypes/geotypes.dart'; +export 'src/flip.dart'; \ No newline at end of file diff --git a/lib/src/flip.dart b/lib/src/flip.dart new file mode 100644 index 00000000..3bd09ce5 --- /dev/null +++ b/lib/src/flip.dart @@ -0,0 +1,22 @@ +import 'package:turf/turf.dart'; +import 'package:turf/meta.dart'; + +// Takes a set of input Coordinate features and flips their Coordinates from Position(x,y) to Position(y, x) + +// Returns a GeoJSON FeatureCollection with the flipped Coordinates + +Feature flip(Feature? geojson, +{Map? properties, }) { + if (geojson == null) { + throw ArgumentError('geojson cannot be null'); + } + + coordEach( + geojson, + (Position? currentCoord, int? coordIndex, int? featureIndex, int? multiFeatureIndex, int? geometryIndex) { + currentCoord = Position.named(lat: currentCoord!.lng, lng: currentCoord.lat); + } + ); + + return geojson; +} diff --git a/test/components/flip_test.dart b/test/components/flip_test.dart new file mode 100644 index 00000000..05505afc --- /dev/null +++ b/test/components/flip_test.dart @@ -0,0 +1,44 @@ +import 'package:test/test.dart'; +import 'package:turf/flip.dart'; + +void main() { + group('flip tests', () { + test('flipping individual coordinates - point', () { + var point = Feature( + geometry: Point( + coordinates: Position.named(lat: -75, lng: 39) + ), + ); + + expect(flip(point).geometry?.coordinates, equals(Position(39,-75))); + }); + + test('flipping a group of coordinates - polygon', () { + var polygon = Feature( + geometry: Polygon( + coordinates: [ + [ + Position.named(lat: -75, lng: 39), + Position.named(lat: -75, lng: 38), + Position.named(lat: -74, lng: 38), + Position.named(lat: -74, lng: 39), + Position.named(lat: -75, lng: 39), + ], + ], + ), + ); + + var expPolygon = [ + [ + Position(39,-75), + Position(38, -75), + Position(38, -74), + Position(39, -74), + Position(39, -75) + ] + ]; + expect(flip(polygon).geometry?.coordinates, equals(expPolygon)); + + }); + }); +} \ No newline at end of file