turf icon indicating copy to clipboard operation
turf copied to clipboard

booleanContains() fails in the given shapes

Open vahid18u opened this issue 3 years ago • 6 comments
trafficstars

Capture

yellow geometry is not completely contained by the green geometry but turf.booleanContains(green , yellow )) returns true. turf 6.5.0

vahid18u avatar Aug 01 '22 07:08 vahid18u

@vahid18u please provide a snippet of the code generating the unexpected result; it is impossible to debug/test a picture 😄

stebogit avatar Aug 01 '22 08:08 stebogit

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 avatar Aug 02 '22 07:08 vahid18u

@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

Screen Shot 2022-08-05 at 12 13 55 AM

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]
      ]
    ]
  }
}

Screen Shot 2022-08-05 at 12 30 12 AM

stebogit avatar Aug 05 '22 07:08 stebogit

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

image

linzhongxian avatar Sep 21 '22 08:09 linzhongxian

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

mauricio-custodio avatar Dec 27 '23 16:12 mauricio-custodio