Skip to content

Commit

Permalink
Change return type of along to Feature<Point>
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leiflinse-trivector committed Jan 26, 2024
1 parent c123163 commit 91093b8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
9 changes: 5 additions & 4 deletions lib/src/along.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<LineString> line, num distance,
Feature<Point> along(Feature<LineString> line, num distance,
[Unit unit = Unit.kilometers]) {
// Get Coords
final coords = getCoords(line);
Expand All @@ -30,7 +30,7 @@ Point along(Feature<LineString> line, num distance,
break;
}
if (travelled == distance) {
return Point(coordinates: coords[i]);
return Feature<Point>(geometry: Point(coordinates: coords[i]));
}
if (travelled > distance) {
final overshot = distance - travelled;
Expand All @@ -43,11 +43,12 @@ Point along(Feature<LineString> line, num distance,
direction,
unit,
);
return interpolated;
return Feature<Point>(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<Point>(
geometry: Point(coordinates: coords[coords.length - 1]));
}
28 changes: 18 additions & 10 deletions test/components/along_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,32 @@ 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', () {
final startToViaDistance = distance(
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(
Expand All @@ -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));
});
}

Expand Down

0 comments on commit 91093b8

Please sign in to comment.