Turf: Incorrectly getting `null` from `turf.intersect`

Created on 21 Aug 2019  路  7Comments  路  Source: Turfjs/turf

I'm trying to use turf to generate intersections of some polygons but it randomly seems to not work. For example, the polygons in this gist clearly intersect, but turf.intersect returns null

image

> const turf = require('@turf/turf');
> const data = require('./data.json');
> turf.intersect(data.features[0], data.features[1])
null

https://gist.github.com/dschep/92bc5f308b8506a774ecc9482795ccdf

bug

Most helpful comment

The turf.intersect function is just a thin && buggy layer of user-friendly encapsulation of martinez-polygon-clipping, please consider using it directly. I encountered the same issue and got it resolved with that package.

All 7 comments

Can replicate starting with version 5.04

Oh, so it's a newish bug. That's really interesting. Bc turf is so modular, I tried to use @turf/intersect@<5.0.4 but then got some odd outputs too. Since I'm actually not generally using modular turf in this script, I do this and it seems to work:

const turf = require('@turf/turf') # ^5.1.6 in package.json
const intersect = require('@turf/intersect') # <5.0.4 in package.json
let intersection = turf.intersect(poly1, poly2) || intersect(poly1, poly2)

The turf.intersect function is just a thin && buggy layer of user-friendly encapsulation of martinez-polygon-clipping, please consider using it directly. I encountered the same issue and got it resolved with that package.

Thanks for the pointer @tibetty, I'll check that out.

I had to use martinez-polygon-clipping directly as well. Can this be updated in turf?

I have the same issue, function turf.intersect returning null with this code :
turf.intersect({"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[2.269577978076996,48.71995650497067],[2.3007435825130793,48.737428870414234],[2.300675792911607,48.73748135853885],[2.269577978076996,48.71995650497067]]]}},{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[2.2684729099273686,48.724622826193],[2.3092314581589743,48.72685694535847],[2.3092211749806038,48.7269373823835],[2.2684729099273686,48.724622826193]]]}})

But unfortunately the 2 polygons (in blue below) when drawn on a map seem intersect :
image

https://codepen.io/MajorDOOM/pen/dyPmELz

I have a similar case of polygons reporting a null intersection.

https://jsfiddle.net/wlievens/tmj8Ln32/13

var p1 = {"type":"Polygon","coordinates":[[
[41.85587797331998,85.05112877980659],
[37.397392190303705,68.44639338124588],
[21.4340365960655,54.312614791502085],
[8.310282594978935e-9,67.92046278551715],
[8.310282594978935e-9,85.05112877980659],
[41.85587797331998,85.05112877980659]
]]};

var p2 = {"type":"Polygon","coordinates":[[
[10.41346496136495,85.05112778577897],
[10.413464623816678,85.05112794274982],
[10.413464666297507,85.05112810235666],
[10.413465087004509,85.05112825782558],
[10.41346586808238,85.05112840255832],
[10.413466976381255,85.05112853041223],
[10.413468364863629,85.05112863596105],
[10.413469974600678,85.05112871472518],
[10.413471737273266,85.05112876336175],
[10.413473578071496,85.05112877980659],
[79.58652642192847,85.05112877980659],
[79.5865282627267,85.05112876336175],
[79.58653002539928,85.05112871472518],
[79.58653163513635,85.05112863596105],
[79.58653302361871,85.05112853041223],
[79.58653413191759,85.05112840255832],
[79.58653491299546,85.05112825782558],
[79.58653533370246,85.05112810235666],
[79.58653537618329,85.05112794274982],
[79.58653503863503,85.05112778577897],
[45.000008616706545,52.95134261468802],
[45.00000795060929,52.95134162544201],
[45.00000697518646,52.95134073421229],
[45.00000572838777,52.951339975672965],
[45.00000425872103,52.951339379335685],
[45.000002623364914,52.95133896840149],
[45.000000885944466,52.95133875885815],
[44.99999911405554,52.95133875885815],
[44.999997376635086,52.95133896840149],
[44.99999574127899,52.951339379335685],
[44.99999427161223,52.951339975672965],
[44.99999302481355,52.95134073421229],
[44.99999204939071,52.95134162544201],
[44.999991383293455,52.95134261468802],
[10.41346496136495,85.05112778577897]
]]};

console.log(turf.intersect(p1, p2));
Was this page helpful?
0 / 5 - 0 ratings

Related issues

MichelBahl picture MichelBahl  路  4Comments

stebogit picture stebogit  路  5Comments

robhawkes picture robhawkes  路  3Comments

mnvx picture mnvx  路  3Comments

morganherlocker picture morganherlocker  路  5Comments