Skip to content

Commit

Permalink
Fix: handle case where startPt and stopPt resolve to same poin on line
Browse files Browse the repository at this point in the history
  • Loading branch information
leiflinse-trivector committed Jan 26, 2024
1 parent 5bae380 commit d31f89e
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/src/line_slice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import 'package:turf/src/invariant.dart';
/// and returns a subsection of the line in-between those points.
/// The start & stop points don't need to fall exactly on the line.
///
/// If [startPt] and [stopPt] resolve to the same point on [line], null is returned
/// as the resolved line would only contain one point which isn't supported by LineString.
///
/// This can be useful for extracting only the part of a route between waypoints.
Feature<LineString> lineSlice(
Point startPt, Point stopPt, Feature<LineString> line) {
Expand All @@ -16,6 +19,10 @@ Feature<LineString> lineSlice(

final startVertex = nearestPointOnLine(coords, startPt);
final stopVertex = nearestPointOnLine(coords, stopPt);
if (startVertex['index'] == stopVertex['index']) {
// LineString do not allow 1 point lines
return null;
}
late final List<Feature<Point>> ends;
if (startVertex.properties!['index'] <= stopVertex.properties!['index']) {
ends = [startVertex, stopVertex];
Expand Down

0 comments on commit d31f89e

Please sign in to comment.