|
| 1 | +import 'package:test/test.dart'; |
| 2 | +import 'package:turf/along.dart'; |
| 3 | +import 'package:turf/distance.dart'; |
| 4 | +import 'package:turf/helpers.dart'; |
| 5 | +import 'package:turf/length.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + test('along - negative distance should count backwards', () { |
| 9 | + final viaToEndDistance = |
| 10 | + distance(Point(coordinates: via), Point(coordinates: end), Unit.meters); |
| 11 | + expect(viaToEndDistance.round(), equals(198)); |
| 12 | + final resolvedViaPoint = along(line, -1 * viaToEndDistance, Unit.meters); |
| 13 | + expect(resolvedViaPoint.coordinates, equals(via)); |
| 14 | + }); |
| 15 | + test('along - to start point', () { |
| 16 | + final resolvedStartPoint = along(line, 0, Unit.meters); |
| 17 | + expect(resolvedStartPoint.coordinates, equals(start)); |
| 18 | + }); |
| 19 | + test('along - to point between start and via', () { |
| 20 | + final startToViaDistance = distance( |
| 21 | + Point(coordinates: start), Point(coordinates: via), Unit.meters); |
| 22 | + expect(startToViaDistance.round(), equals(57)); |
| 23 | + final resolvedViaPoint = along(line, startToViaDistance / 2, Unit.meters); |
| 24 | + expect(resolvedViaPoint.coordinates.lat.toStringAsFixed(6), |
| 25 | + equals('55.709028')); |
| 26 | + expect(resolvedViaPoint.coordinates.lng.toStringAsFixed(6), |
| 27 | + equals('13.185096')); |
| 28 | + }); |
| 29 | + test('along - to via point', () { |
| 30 | + final startToViaDistance = distance( |
| 31 | + Point(coordinates: start), Point(coordinates: via), Unit.meters); |
| 32 | + expect(startToViaDistance.round(), equals(57)); |
| 33 | + final resolvedViaPoint = along(line, startToViaDistance, Unit.meters); |
| 34 | + expect(resolvedViaPoint.coordinates, equals(via)); |
| 35 | + }); |
| 36 | + test('along - to point between via and end', () { |
| 37 | + final startToViaDistance = distance( |
| 38 | + Point(coordinates: start), Point(coordinates: via), Unit.meters); |
| 39 | + final viaToEndDistance = |
| 40 | + distance(Point(coordinates: via), Point(coordinates: end), Unit.meters); |
| 41 | + expect(startToViaDistance.round(), equals(57)); |
| 42 | + expect(viaToEndDistance.round(), equals(198)); |
| 43 | + final resolvedViaPoint = |
| 44 | + along(line, startToViaDistance + viaToEndDistance / 2, Unit.meters); |
| 45 | + expect(resolvedViaPoint.coordinates.lat.toStringAsFixed(6), |
| 46 | + equals('55.708330')); |
| 47 | + expect(resolvedViaPoint.coordinates.lng.toStringAsFixed(6), |
| 48 | + equals('13.186555')); |
| 49 | + }); |
| 50 | + test('along - to end point', () { |
| 51 | + final len = length(line, Unit.meters); |
| 52 | + expect(len.round(), equals(254)); |
| 53 | + final resolvedEndPoint = along(line, len, Unit.meters); |
| 54 | + expect(resolvedEndPoint.coordinates, equals(end)); |
| 55 | + }); |
| 56 | + test('along - to end point - default unit (km)', () { |
| 57 | + final len = length(line); |
| 58 | + expect((len * 1000).round(), equals(254)); |
| 59 | + final resolvedEndPoint = along(line, len); |
| 60 | + expect(resolvedEndPoint.coordinates, equals(end)); |
| 61 | + }); |
| 62 | + test('along - beyond end point', () { |
| 63 | + final len = length(line, Unit.meters); |
| 64 | + expect(len.round(), equals(254)); |
| 65 | + final resolvedEndPoint = along(line, len + 100, Unit.meters); |
| 66 | + expect(resolvedEndPoint.coordinates, equals(end)); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +final start = Position.named( |
| 71 | + lat: 55.7090430186194, |
| 72 | + lng: 13.184645393920405, |
| 73 | +); |
| 74 | +final via = Position.named( |
| 75 | + lat: 55.70901279569489, |
| 76 | + lng: 13.185546616182755, |
| 77 | +); |
| 78 | +final end = Position.named( |
| 79 | + lat: 55.70764669578079, |
| 80 | + lng: 13.187563637197076, |
| 81 | +); |
| 82 | +final line = Feature<LineString>( |
| 83 | + geometry: LineString( |
| 84 | + coordinates: [ |
| 85 | + start, |
| 86 | + via, |
| 87 | + end, |
| 88 | + ], |
| 89 | + ), |
| 90 | +); |
0 commit comments