'$geoWithin return document located outside the polygon queried

I've recently came across this behaviour that I cannot explain. $geowithin return documents outside the polygon queried.

To illustrate my problem, I've made a mongodb playground with 3 documents with geometries all located outside the polygon. The polygon is rather big and I've read the reco for big polygons.

Here the $geoWithin query with the polygon built anti-clockwise that return 1 document.

db.collection.aggregate([
  {
    $match: {
      "featureOfInterest.samplingFeature.geometry": {
        $geoWithin: {
          $geometry: {
            type: "Polygon",
            coordinates: [
              [
                [
                  -131.484375,
                  -58.447733
                ],
                [
                  67.5,
                  -58.447733
                ],
                [
                  67.5,
                  24.20689
                ],
                [
                  -131.484375,
                  24.20689
                ],
                [
                  -131.484375,
                  -58.447733
                ]
              ]
            ],
            crs: {
              type: "name",
              properties: {
                name: "urn:x-mongodb:crs:strictwinding:EPSG:4326"
              }
            }
          }
        }
      }
    }
  }
])

I also tried to build the polygon clockwise and the query return the two documents that were not returned by the previous polygon.

[
  [
    [
      -131.484375,
      -58.447733
    ],
    [
      -131.484375,
      24.20689
    ],
    [
      67.5,
      24.20689
    ],
    [
      67.5,
      -58.447733
    ],
    [
      -131.484375,
      -58.447733
    ]
  ]
]

Can someone spot what I'm missing here?



Solution 1:[1]

all located outside the polygon

Except they are not - two of them are outside and one is inside.

It's easier to see if you draw the other polygon (as you did) and see two of the three points inside and the third one (correctly) outside.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Asya Kamsky