From 620d1f9ea614c27a52a824cb37aa3588e2013556 Mon Sep 17 00:00:00 2001 From: Leif Linse <118986082+leiflinse-trivector@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:35:34 +0100 Subject: [PATCH] Change return type of along to Feature (#161) This goes along with preferring to pass around full Features, and it allow to in the future set properties on the returned point without it being a breaking change. --- lib/src/along.dart | 9 +++++---- test/components/along_test.dart | 28 ++++++++++++++++++---------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/src/along.dart b/lib/src/along.dart index 9fb4c7e..f4b5026 100644 --- a/lib/src/along.dart +++ b/lib/src/along.dart @@ -14,7 +14,7 @@ import 'package:turf/src/invariant.dart'; /// the start point of the line is returned. /// If [distance] is larger than line length, the end point is returned /// If [line] have no geometry or coordinates, an Exception is thrown -Point along(Feature line, num distance, +Feature along(Feature line, num distance, [Unit unit = Unit.kilometers]) { // Get Coords final coords = getCoords(line); @@ -30,7 +30,7 @@ Point along(Feature line, num distance, break; } if (travelled == distance) { - return Point(coordinates: coords[i]); + return Feature(geometry: Point(coordinates: coords[i])); } if (travelled > distance) { final overshot = distance - travelled; @@ -43,11 +43,12 @@ Point along(Feature line, num distance, direction, unit, ); - return interpolated; + return Feature(geometry: interpolated); } else { travelled += measure_distance.distance(Point(coordinates: coords[i]), Point(coordinates: coords[i + 1]), unit); } } - return Point(coordinates: coords[coords.length - 1]); + return Feature( + geometry: Point(coordinates: coords[coords.length - 1])); } diff --git a/test/components/along_test.dart b/test/components/along_test.dart index dbc81a1..f2c9d30 100644 --- a/test/components/along_test.dart +++ b/test/components/along_test.dart @@ -10,20 +10,23 @@ void main() { distance(Point(coordinates: via), Point(coordinates: end), Unit.meters); expect(viaToEndDistance.round(), equals(198)); final resolvedViaPoint = along(line, -1 * viaToEndDistance, Unit.meters); - expect(resolvedViaPoint.coordinates, equals(via)); + expect(resolvedViaPoint.geometry, isNotNull); + expect(resolvedViaPoint.geometry!.coordinates, equals(via)); }); test('along - to start point', () { final resolvedStartPoint = along(line, 0, Unit.meters); - expect(resolvedStartPoint.coordinates, equals(start)); + expect(resolvedStartPoint.geometry, isNotNull); + expect(resolvedStartPoint.geometry!.coordinates, equals(start)); }); test('along - to point between start and via', () { final startToViaDistance = distance( Point(coordinates: start), Point(coordinates: via), Unit.meters); expect(startToViaDistance.round(), equals(57)); final resolvedViaPoint = along(line, startToViaDistance / 2, Unit.meters); - expect(resolvedViaPoint.coordinates.lat.toStringAsFixed(6), + expect(resolvedViaPoint.geometry, isNotNull); + expect(resolvedViaPoint.geometry!.coordinates.lat.toStringAsFixed(6), equals('55.709028')); - expect(resolvedViaPoint.coordinates.lng.toStringAsFixed(6), + expect(resolvedViaPoint.geometry!.coordinates.lng.toStringAsFixed(6), equals('13.185096')); }); test('along - to via point', () { @@ -31,7 +34,8 @@ void main() { Point(coordinates: start), Point(coordinates: via), Unit.meters); expect(startToViaDistance.round(), equals(57)); final resolvedViaPoint = along(line, startToViaDistance, Unit.meters); - expect(resolvedViaPoint.coordinates, equals(via)); + expect(resolvedViaPoint.geometry, isNotNull); + expect(resolvedViaPoint.geometry!.coordinates, equals(via)); }); test('along - to point between via and end', () { final startToViaDistance = distance( @@ -42,28 +46,32 @@ void main() { expect(viaToEndDistance.round(), equals(198)); final resolvedViaPoint = along(line, startToViaDistance + viaToEndDistance / 2, Unit.meters); - expect(resolvedViaPoint.coordinates.lat.toStringAsFixed(6), + expect(resolvedViaPoint.geometry, isNotNull); + expect(resolvedViaPoint.geometry!.coordinates.lat.toStringAsFixed(6), equals('55.708330')); - expect(resolvedViaPoint.coordinates.lng.toStringAsFixed(6), + expect(resolvedViaPoint.geometry!.coordinates.lng.toStringAsFixed(6), equals('13.186555')); }); test('along - to end point', () { final len = length(line, Unit.meters); expect(len.round(), equals(254)); final resolvedEndPoint = along(line, len, Unit.meters); - expect(resolvedEndPoint.coordinates, equals(end)); + expect(resolvedEndPoint.geometry, isNotNull); + expect(resolvedEndPoint.geometry!.coordinates, equals(end)); }); test('along - to end point - default unit (km)', () { final len = length(line); expect((len * 1000).round(), equals(254)); final resolvedEndPoint = along(line, len); - expect(resolvedEndPoint.coordinates, equals(end)); + expect(resolvedEndPoint.geometry, isNotNull); + expect(resolvedEndPoint.geometry!.coordinates, equals(end)); }); test('along - beyond end point', () { final len = length(line, Unit.meters); expect(len.round(), equals(254)); final resolvedEndPoint = along(line, len + 100, Unit.meters); - expect(resolvedEndPoint.coordinates, equals(end)); + expect(resolvedEndPoint.geometry, isNotNull); + expect(resolvedEndPoint.geometry!.coordinates, equals(end)); }); }