|
| 1 | +import 'package:test/test.dart'; |
| 2 | +import 'package:turf/turf.dart'; |
| 3 | + |
| 4 | +import '../context/load_test_cases.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + loadGeoJson('./test/examples/line_slice_along/fixtures/line1.geojson', |
| 8 | + (path, geoJson) { |
| 9 | + final line1 = Feature<LineString>( |
| 10 | + geometry: (geoJson as Feature).geometry as LineString, |
| 11 | + ); |
| 12 | + |
| 13 | + test('turf-line-slice-along -- line1', () { |
| 14 | + const start = 500.0; |
| 15 | + const stop = 750.0; |
| 16 | + const options = Unit.miles; |
| 17 | + |
| 18 | + final startPoint = along(line1, start, options); |
| 19 | + final endPoint = along(line1, stop, options); |
| 20 | + final sliced = lineSliceAlong(line1, start, stop, options); |
| 21 | + |
| 22 | + expect(sliced, isA<Feature<LineString>>()); |
| 23 | + expect(sliced.type, GeoJSONObjectType.feature); |
| 24 | + expect(sliced.geometry?.type, GeoJSONObjectType.lineString); |
| 25 | + expect(sliced.geometry?.coordinates[0], |
| 26 | + equals(startPoint.geometry!.coordinates)); |
| 27 | + expect( |
| 28 | + sliced.geometry?.coordinates[sliced.geometry!.coordinates.length - 1], |
| 29 | + equals(endPoint.geometry!.coordinates), |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + test('turf-line-slice-along -- line1 overshoot', () { |
| 34 | + const start = 500.0; |
| 35 | + const stop = 1500.0; |
| 36 | + const options = Unit.miles; |
| 37 | + |
| 38 | + final startPoint = along(line1, start, options); |
| 39 | + final endPoint = along(line1, stop, options); |
| 40 | + final sliced = lineSliceAlong(line1, start, stop, options); |
| 41 | + |
| 42 | + expect(sliced, isA<Feature<LineString>>()); |
| 43 | + expect(sliced.type, GeoJSONObjectType.feature); |
| 44 | + expect(sliced.geometry?.type, GeoJSONObjectType.lineString); |
| 45 | + expect(sliced.geometry?.coordinates[0], |
| 46 | + equals(startPoint.geometry!.coordinates)); |
| 47 | + expect( |
| 48 | + sliced.geometry?.coordinates[sliced.geometry!.coordinates.length - 1], |
| 49 | + equals(endPoint.geometry!.coordinates), |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + test('turf-line-slice-along -- start longer than line length', () { |
| 54 | + const start = 500000.0; |
| 55 | + const stop = 800000.0; |
| 56 | + const options = Unit.miles; |
| 57 | + |
| 58 | + expect( |
| 59 | + () => lineSliceAlong(line1, start, stop, options), |
| 60 | + throwsA(isA<Exception>().having( |
| 61 | + (e) => e.toString(), |
| 62 | + 'message', |
| 63 | + contains('Start position is beyond line'), |
| 64 | + )), |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + test('turf-line-slice-along -- start equal to line length', () { |
| 69 | + const options = Unit.miles; |
| 70 | + final start = length(line1, options); |
| 71 | + final stop = start + 100; |
| 72 | + |
| 73 | + final startPoint = along(line1, start, options); |
| 74 | + final endPoint = along(line1, stop, options); |
| 75 | + final sliced = |
| 76 | + lineSliceAlong(line1, start.toDouble(), stop.toDouble(), options); |
| 77 | + |
| 78 | + expect(sliced, isA<Feature<LineString>>()); |
| 79 | + expect(sliced.type, GeoJSONObjectType.feature); |
| 80 | + expect(sliced.geometry?.type, GeoJSONObjectType.lineString); |
| 81 | + expect(sliced.geometry?.coordinates[0], |
| 82 | + equals(startPoint.geometry!.coordinates)); |
| 83 | + expect( |
| 84 | + sliced.geometry?.coordinates[sliced.geometry!.coordinates.length - 1], |
| 85 | + endPoint.geometry!.coordinates, |
| 86 | + ); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + loadGeoJson('./test/examples/line_slice_along/fixtures/route1.geojson', |
| 91 | + (path, geoJson) { |
| 92 | + final route1 = Feature<LineString>( |
| 93 | + geometry: (geoJson as Feature).geometry as LineString, |
| 94 | + ); |
| 95 | + |
| 96 | + test('turf-line-slice-along -- route1', () { |
| 97 | + const start = 500.0; |
| 98 | + const stop = 750.0; |
| 99 | + const options = Unit.miles; |
| 100 | + |
| 101 | + final startPoint = along(route1, start, options); |
| 102 | + final endPoint = along(route1, stop, options); |
| 103 | + final sliced = lineSliceAlong(route1, start, stop, options); |
| 104 | + |
| 105 | + expect(sliced, isA<Feature<LineString>>()); |
| 106 | + expect(sliced.type, GeoJSONObjectType.feature); |
| 107 | + expect(sliced.geometry?.type, GeoJSONObjectType.lineString); |
| 108 | + expect(sliced.geometry?.coordinates[0], |
| 109 | + equals(startPoint.geometry!.coordinates)); |
| 110 | + expect( |
| 111 | + sliced.geometry?.coordinates[sliced.geometry!.coordinates.length - 1], |
| 112 | + equals(endPoint.geometry!.coordinates), |
| 113 | + ); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + loadGeoJson('./test/examples/line_slice_along/fixtures/route2.geojson', |
| 118 | + (path, geoJson) { |
| 119 | + final route2 = Feature<LineString>( |
| 120 | + geometry: (geoJson as Feature).geometry as LineString, |
| 121 | + ); |
| 122 | + |
| 123 | + test('turf-line-slice-along -- route2', () { |
| 124 | + const start = 25.0; |
| 125 | + const stop = 50.0; |
| 126 | + const options = Unit.miles; |
| 127 | + |
| 128 | + final startPoint = along(route2, start, options); |
| 129 | + final endPoint = along(route2, stop, options); |
| 130 | + final sliced = lineSliceAlong(route2, start, stop, options); |
| 131 | + |
| 132 | + expect(sliced, isA<Feature<LineString>>()); |
| 133 | + expect(sliced.type, GeoJSONObjectType.feature); |
| 134 | + expect(sliced.geometry?.type, GeoJSONObjectType.lineString); |
| 135 | + expect(sliced.geometry?.coordinates[0], |
| 136 | + equals(startPoint.geometry!.coordinates)); |
| 137 | + expect( |
| 138 | + sliced.geometry?.coordinates[sliced.geometry!.coordinates.length - 1], |
| 139 | + equals(endPoint.geometry!.coordinates), |
| 140 | + ); |
| 141 | + }); |
| 142 | + }); |
| 143 | +} |
0 commit comments