|
| 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 | +import 'package:turf/src/line_slice.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + test('lineSlice - exact points', () { |
| 10 | + final slice = lineSlice(startFeature, viaFeature, lineFeature); |
| 11 | + expect(slice, isNotNull); |
| 12 | + expect(slice.properties, isNotNull); |
| 13 | + expect(slice.properties!.keys, contains(propName)); |
| 14 | + expect(slice.properties![propName], equals(propValue)); |
| 15 | + |
| 16 | + final expectedLineFeature = Feature<LineString>( |
| 17 | + geometry: LineString(coordinates: [start, via]), |
| 18 | + ); |
| 19 | + expect(slice.geometry, isNotNull); |
| 20 | + expect(slice.geometry!.coordinates, hasLength(2)); |
| 21 | + expect(length(slice).round(), equals(length(expectedLineFeature).round())); |
| 22 | + }); |
| 23 | + test('lineSlice - interpolation', () { |
| 24 | + const skipDist = 10; |
| 25 | + |
| 26 | + final sliceFrom = along(lineFeature, skipDist, Unit.meters); |
| 27 | + expect(sliceFrom, isNotNull); |
| 28 | + |
| 29 | + final slice = lineSlice(sliceFrom, viaFeature, lineFeature); |
| 30 | + expect(slice, isNotNull); |
| 31 | + expect(slice.properties, isNotNull); |
| 32 | + expect(slice.properties!.keys, contains(propName)); |
| 33 | + expect(slice.properties![propName], equals(propValue)); |
| 34 | + |
| 35 | + final expectedLine = Feature<LineString>( |
| 36 | + geometry: LineString(coordinates: [start, via]), |
| 37 | + ); |
| 38 | + expect(slice.geometry, isNotNull); |
| 39 | + expect(slice.geometry!.coordinates, hasLength(2)); |
| 40 | + expect( |
| 41 | + length(slice, Unit.meters).round(), |
| 42 | + equals(length(expectedLine, Unit.meters).round() - skipDist), |
| 43 | + ); |
| 44 | + |
| 45 | + // Sanity check of test data. No interpolation occurs if start and via are skipDist apart. |
| 46 | + expect(distance(Point(coordinates: start), Point(coordinates: via)).round(), |
| 47 | + isNot(equals(skipDist))); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +final start = Position.named( |
| 52 | + lat: 55.7090430186194, |
| 53 | + lng: 13.184645393920405, |
| 54 | +); |
| 55 | +final via = Position.named( |
| 56 | + lat: 55.70901279569489, |
| 57 | + lng: 13.185546616182755, |
| 58 | +); |
| 59 | +final end = Position.named( |
| 60 | + lat: 55.70764669578079, |
| 61 | + lng: 13.187563637197076, |
| 62 | +); |
| 63 | +const propName = 'prop1'; |
| 64 | +const propValue = 1; |
| 65 | +final lineFeature = Feature<LineString>( |
| 66 | + geometry: LineString( |
| 67 | + coordinates: [ |
| 68 | + start, |
| 69 | + via, |
| 70 | + end, |
| 71 | + ], |
| 72 | + ), |
| 73 | + properties: { |
| 74 | + propName: propValue, |
| 75 | + }, |
| 76 | +); |
| 77 | +final startFeature = Feature<Point>(geometry: Point(coordinates: start)); |
| 78 | +final viaFeature = Feature<Point>(geometry: Point(coordinates: via)); |
0 commit comments