turf
turf copied to clipboard
booleanContains() fails in the given shapes
yellow geometry is not completely contained by the green geometry but turf.booleanContains(green , yellow )) returns true. turf 6.5.0
@vahid18u please provide a snippet of the code generating the unexpected result; it is impossible to debug/test a picture 😄
OK @stebogit you are right :) , here's the gist file for two other polygons similar to the image: https://gist.github.com/vahid18u/c79c69214fcf1fdb916aebc3c7d4d97e
@vahid18u your gist contains MultiPolygons, which are not supported by booleanContains, however once converted to Polygons the package does indeed return the wrong result:
const yellow = {
'type': 'Feature',
'properties': { 'id': 2, 'fill': 'yellow' },
'geometry': {
'type': 'Polygon',
'coordinates': [
[
[-0.143529411764706, 0.395294117647059],
[-0.115294117647059, -0.117647058823529],
[-0.931764705882353, -0.051764705882353],
[-0.936470588235294, 0.317647058823529],
[-0.143529411764706, 0.395294117647059]
]
]
}
}
const green = {
'type': 'Feature',
'properties': { 'id': 1, 'fill': 'green' },
'geometry': {
'type': 'Polygon',
'coordinates': [
[
[0.087058823529412, 0.376470588235294],
[-0.574117647058823, 0.082352941176471],
[0.27764705882353, -0.089411764705882],
[-0.625882352941176, -0.4],
[-1.390588235294117, 0.202352941176471],
[-0.28, 0.705882352941176],
[0.087058823529412, 0.376470588235294]
]
]
}
}
const isContained = turf.booleanContains(green, yellow)
// true, should be false

However this correctly returns false:
{
"type": "Feature",
"properties": { "id": 2, "fill": "yellow" },
"geometry": {
"type": "Polygon",
"coordinates": [
[
[0.65,0.395294117647059],
[0.65,-0.117647058823529],
[-0.931764705882353,-0.051764705882353],
[-0.936470588235294,0.317647058823529],
[0.65,0.395294117647059]
]
]
}
}

I have a similar situation to this.
var result = turf.booleanContains({
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
113.749824,
23.0188
],
[
113.753772,
23.012914
],
[
113.757119,
23.017891
],
[
113.753815,
23.023776
],
[
113.749824,
23.0188
]
]
]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
113.751905,
23.017121
],
[
113.754394,
23.017259
],
[
113.751787,
23.014613
]
]
}
});
console.log(result) // true, should be false

I had the same issue. It seems that booleanContains() is only checking if all points of a polygon are contained in the outer polygon.