@@ -9,11 +9,22 @@ import {
99 point ,
1010 lineString ,
1111 multiPoint ,
12+ polygon ,
1213 featureCollection ,
1314 round ,
1415} from "@turf/helpers" ;
1516import { getCoords } from "@turf/invariant" ;
1617import { lineSplit } from "./index.js" ;
18+ import type {
19+ Feature ,
20+ FeatureCollection ,
21+ LineString ,
22+ MultiLineString ,
23+ MultiPoint ,
24+ MultiPolygon ,
25+ Point ,
26+ Polygon ,
27+ } from "geojson" ;
1728
1829const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
1930
@@ -26,15 +37,17 @@ let fixtures = fs.readdirSync(directories.in).map((filename) => {
2637 return {
2738 filename,
2839 name : path . parse ( filename ) . name ,
29- geojson : loadJsonFileSync ( directories . in + filename ) ,
40+ geojson : loadJsonFileSync ( directories . in + filename ) as FeatureCollection ,
3041 } ;
3142} ) ;
32- // fixtures = fixtures.filter(name => name === ' issue-#1075')
43+ // fixtures = fixtures.filter(({ name }) => name === " issue-#1075");
3344
3445test ( "turf-line-split" , ( t ) => {
3546 for ( const { filename, name, geojson } of fixtures ) {
36- const line = geojson . features [ 0 ] ;
37- const splitter = geojson . features [ 1 ] ;
47+ const line = geojson . features [ 0 ] as Feature < LineString > ;
48+ const splitter = geojson . features [ 1 ] as Feature <
49+ Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon
50+ > ;
3851 const results = colorize ( lineSplit ( line , splitter ) ) ;
3952 featureEach ( geojson , ( feature ) => results . features . push ( feature ) ) ;
4053
@@ -82,10 +95,14 @@ test("turf-line-split -- throws", (t) => {
8295 [ 9 , 50 ] ,
8396 ] ) ;
8497
98+ // @ts -expect-error passing null for line
8599 t . throws ( ( ) => lineSplit ( null , pt ) , "<geojson> is required" ) ;
100+ // @ts -expect-error passing null for splitter
86101 t . throws ( ( ) => lineSplit ( line , null ) , "<geojson> is required" ) ;
102+ // @ts -expect-error passing wrong type for line
87103 t . throws ( ( ) => lineSplit ( pt , pt ) , "<line> must be LineString" ) ;
88104 t . throws (
105+ // @ts -expect-error passing wrong type for splitter
89106 ( ) => lineSplit ( line , featureCollection ( [ pt , line ] ) ) ,
90107 "<splitter> cannot be a FeatureCollection"
91108 ) ;
@@ -200,14 +217,132 @@ test("turf-line-split -- issue #1075", (t) => {
200217 t . end ( ) ;
201218} ) ;
202219
220+ test ( "lineSplit - incorrect split - issue #1075" , ( t ) => {
221+ let line , splitter , split ;
222+
223+ // Example sourced from https://github.com/Turfjs/turf/issues/1232#issue-290515769
224+ line = lineString ( [
225+ [ 13.8716 , 56.2783 ] ,
226+ [ 13.8715 , 56.2785 ] ,
227+ [ 13.8743 , 56.2794 ] ,
228+ [ 13.8796 , 56.2746 ] ,
229+ ] ) ;
230+
231+ splitter = polygon ( [
232+ [
233+ [ 13.8726 , 56.2786 ] ,
234+ [ 13.8716 , 56.2786 ] ,
235+ [ 13.8713 , 56.2784 ] ,
236+ [ 13.8726 , 56.2786 ] ,
237+ ] ,
238+ ] ) ;
239+
240+ split = lineSplit ( line , splitter ) ;
241+ t . equal ( split . features . length , 3 , "Line split into 3 pieces example 1" ) ;
242+
243+ // Example sourced from https://github.com/Turfjs/turf/issues/1232#issuecomment-361970429
244+ line = lineString ( [
245+ [ 10.424716 , 50.024888 ] ,
246+ [ 10.417643 , 50.029512 ] ,
247+ ] ) ;
248+
249+ splitter = polygon ( [
250+ [
251+ [ 10.41993839 , 50.0301184 ] ,
252+ [ 10.42587086 , 50.02630702 ] ,
253+ [ 10.41993839 , 50.02249594 ] ,
254+ [ 10.41400592 , 50.02630702 ] ,
255+ [ 10.41993839 , 50.0301184 ] ,
256+ ] ,
257+ ] ) ;
258+
259+ split = lineSplit ( line , splitter ) ;
260+ t . equal ( split . features . length , 3 , "Line split into 3 pieces example 2" ) ;
261+
262+ // Example sourced from https://github.com/Turfjs/turf/issues/1232#issuecomment-2033181347
263+ line = lineString ( [
264+ [ - 111.570323 , 49.587462 ] ,
265+ [ - 111.570824 , 49.587462 ] ,
266+ [ - 111.571218 , 49.587212 ] ,
267+ [ - 111.571075 , 49.587212 ] ,
268+ [ - 111.566432 , 49.584359 ] ,
269+ ] ) ;
270+
271+ splitter = lineString ( [
272+ [ - 111.57071881384209 , 49.58746705929172 ] ,
273+ [ - 111.57072743959235 , 49.587462 ] ,
274+ [ - 111.57106608768505 , 49.58726337153985 ] ,
275+ [ - 111.57109709453526 , 49.587212 ] ,
276+ [ - 111.5711022620136 , 49.58720343862349 ] ,
277+ ] ) ;
278+
279+ split = lineSplit ( line , splitter ) ;
280+ t . equal ( split . features . length , 3 , "Line split into 3 pieces example 3" ) ;
281+
282+ // Example sourced from https://github.com/Turfjs/turf/issues/1232#issuecomment-2231554080
283+ line = lineString ( [
284+ [ 0 , 44 ] ,
285+ [ 25 , 38 ] ,
286+ [ 27 , 40 ] ,
287+ [ 0 , 62 ] ,
288+ ] ) ;
289+
290+ splitter = polygon ( [
291+ [
292+ [ 0 , 20 ] ,
293+ [ 42 , 20 ] ,
294+ [ 42 , 39 ] ,
295+ [ 28 , 39 ] ,
296+ [ 0 , 52 ] ,
297+ [ 0 , 20 ] ,
298+ ] ,
299+ ] ) ;
300+
301+ split = lineSplit ( line , splitter ) ;
302+
303+ t . equal ( split . features . length , 2 , "Line split into 2 pieces example 4" ) ;
304+ t . end ( ) ;
305+ } ) ;
306+
307+ test ( "lineSplit - wavy lines - issue #2288" , ( t ) => {
308+ // Example sourced from https://github.com/Turfjs/turf/issues/2288#issuecomment-1125555752
309+ const line = lineString ( [
310+ [ - 122.7779211637529 , 38.46929673131614 ] ,
311+ [ - 122.78647173567292 , 38.46208580491829 ] ,
312+ ] ) ;
313+
314+ const splitter = polygon ( [
315+ [
316+ [ - 122.784210824 , 38.46577859000006 , 0 ] ,
317+ [ - 122.783497375 , 38.46577665500005 , 0 ] ,
318+ [ - 122.78349775899989 , 38.46574340700003 , 0 ] ,
319+ [ - 122.78337219700002 , 38.46574404700004 , 0 ] ,
320+ [ - 122.7833724819999 , 38.46570876200008 , 0 ] ,
321+ [ - 122.783186362 , 38.46571185100002 , 0 ] ,
322+ [ - 122.78318417300001 , 38.46569283300004 , 0 ] ,
323+ [ - 122.782172859 , 38.46568571000005 , 0 ] ,
324+ [ - 122.7821622549999 , 38.46587288700003 , 0 ] ,
325+ [ - 122.783391339 , 38.465877436 , 0 ] ,
326+ [ - 122.78339171499998 , 38.46589273900003 , 0 ] ,
327+ [ - 122.78420487999992 , 38.46590174800005 , 0 ] ,
328+ [ - 122.784210824 , 38.46577859000006 , 0 ] ,
329+ ] ,
330+ ] ) ;
331+
332+ const split = lineSplit ( line , splitter ) ;
333+
334+ t . equal ( split . features . length , 3 , "Line split into 3 pieces" ) ;
335+ t . end ( ) ;
336+ } ) ;
337+
203338/**
204339 * Colorize FeatureCollection
205340 *
206341 * @param {FeatureCollection|Feature<any> } geojson Feature or FeatureCollection
207342 * @returns {FeatureCollection<any> } colorized FeatureCollection
208343 */
209- function colorize ( geojson ) {
210- const results = [ ] ;
344+ function colorize ( geojson : FeatureCollection ) {
345+ const results : Feature [ ] = [ ] ;
211346 featureEach ( geojson , ( feature , index ) => {
212347 const r = index % 2 === 0 ? "F" : "0" ;
213348 const g = "0" ;
0 commit comments