Valid geojson won't index in 2dsphere

Hi team - I’m getting some surprising-to-me index failures in Mongo 5.0.4; facts:

  • Every document in my collection has a geolocation property which is assigned a MultiPolygon.
  • I’m creating a ‘2dsphere’ index on this property. It works great for all but 2 of the 40k documents in the collection.
  • both the failures claim that two edges of one of the loops intersect, when they clearly do not.
  • in both cases, the MultiPolygon validates as good geojson both by python shapely, and by https://geojsonlint.com/.

Example one geolocation:

{
  "type": "MultiPolygon",
  "coordinates": [
    [
      [
        [
          123.125,
          -63
        ],
        [
          124.375,
          -63
        ],
        [
          124.375,
          -62
        ],
        [
          123.125,
          -62
        ],
        [
          123.125,
          -61.5
        ],
        [
          122.5,
          -61.5
        ],
        [
          122.5,
          -60.5
        ],
        [
          121.875,
          -60.5
        ],
        [
          121.875,
          -60
        ],
        [
          121.25,
          -60
        ],
        [
          121.25,
          -59
        ],
        [
          120.625,
          -59
        ],
        [
          120.625,
          -58
        ],
        [
          120,
          -58
        ],
        [
          120,
          -53
        ],
        [
          119.375,
          -53
        ],
        [
          119.375,
          -52
        ],
        [
          118.75,
          -52
        ],
        [
          118.75,
          -51.5
        ],
        [
          118.125,
          -51.5
        ],
        [
          118.125,
          -50.5
        ],
        [
          117.5,
          -50.5
        ],
        [
          117.5,
          -48.5
        ],
        [
          116.875,
          -48.5
        ],
        [
          116.875,
          -46.5
        ],
        [
          116.25,
          -46.5
        ],
        [
          116.25,
          -49.5
        ],
        [
          116.875,
          -49.5
        ],
        [
          116.875,
          -52
        ],
        [
          117.5,
          -52
        ],
        [
          117.5,
          -56.5
        ],
        [
          118.125,
          -56.5
        ],
        [
          118.125,
          -59
        ],
        [
          118.75,
          -59
        ],
        [
          118.75,
          -60
        ],
        [
          119.375,
          -60
        ],
        [
          119.375,
          -60.5
        ],
        [
          120,
          -60.5
        ],
        [
          120,
          -61
        ],
        [
          120.625,
          -61
        ],
        [
          120.625,
          -61.5
        ],
        [
          121.25,
          -61.5
        ],
        [
          121.25,
          -62
        ],
        [
          122.5,
          -62
        ],
        [
          122.5,
          -62.5
        ],
        [
          123.125,
          -62.5
        ],
        [
          123.125,
          -63
        ]
      ]
    ],
    [
      [
        [
          115.625,
          -46.5
        ],
        [
          116.25,
          -46.5
        ],
        [
          116.25,
          -37
        ],
        [
          115.625,
          -37
        ],
        [
          115.625,
          -36
        ],
        [
          115,
          -36
        ],
        [
          115,
          -35.5
        ],
        [
          114.375,
          -35.5
        ],
        [
          114.375,
          -35
        ],
        [
          113.75,
          -35
        ],
        [
          113.75,
          -34.5
        ],
        [
          114.375,
          -34.5
        ],
        [
          114.375,
          -34
        ],
        [
          113.75,
          -34
        ],
        [
          113.75,
          -33
        ],
        [
          113.125,
          -33
        ],
        [
          113.125,
          -32.5
        ],
        [
          112.5,
          -32.5
        ],
        [
          112.5,
          -32
        ],
        [
          111.25,
          -32
        ],
        [
          111.25,
          -31.5
        ],
        [
          110.625,
          -31.5
        ],
        [
          110.625,
          -31
        ],
        [
          109.375,
          -31
        ],
        [
          109.375,
          -30.5
        ],
        [
          108.125,
          -30.5
        ],
        [
          108.125,
          -30
        ],
        [
          111.25,
          -30
        ],
        [
          111.25,
          -29.5
        ],
        [
          111.875,
          -29.5
        ],
        [
          111.875,
          -28.5
        ],
        [
          112.5,
          -28.5
        ],
        [
          112.5,
          -28
        ],
        [
          111.875,
          -28
        ],
        [
          111.875,
          -27.5
        ],
        [
          110,
          -27.5
        ],
        [
          110,
          -27
        ],
        [
          108.125,
          -27
        ],
        [
          108.125,
          -26.5
        ],
        [
          105.625,
          -26.5
        ],
        [
          105.625,
          -27
        ],
        [
          105,
          -27
        ],
        [
          105,
          -27.5
        ],
        [
          104.375,
          -27.5
        ],
        [
          104.375,
          -28.5
        ],
        [
          105.625,
          -28.5
        ],
        [
          105.625,
          -29
        ],
        [
          106.875,
          -29
        ],
        [
          106.875,
          -29.5
        ],
        [
          107.5,
          -29.5
        ],
        [
          107.5,
          -30
        ],
        [
          105.625,
          -30
        ],
        [
          105.625,
          -30.5
        ],
        [
          106.25,
          -30.5
        ],
        [
          106.25,
          -31.5
        ],
        [
          107.5,
          -31.5
        ],
        [
          107.5,
          -32.5
        ],
        [
          108.125,
          -32.5
        ],
        [
          108.125,
          -33
        ],
        [
          108.75,
          -33
        ],
        [
          108.75,
          -34
        ],
        [
          109.375,
          -34
        ],
        [
          109.375,
          -34.5
        ],
        [
          110,
          -34.5
        ],
        [
          110,
          -35
        ],
        [
          110.625,
          -35
        ],
        [
          110.625,
          -36
        ],
        [
          111.875,
          -36
        ],
        [
          111.875,
          -36.5
        ],
        [
          112.5,
          -36.5
        ],
        [
          112.5,
          -37.5
        ],
        [
          113.125,
          -37.5
        ],
        [
          113.125,
          -38.5
        ],
        [
          113.75,
          -38.5
        ],
        [
          113.75,
          -40
        ],
        [
          114.375,
          -40
        ],
        [
          114.375,
          -41.5
        ],
        [
          115,
          -41.5
        ],
        [
          115,
          -43.5
        ],
        [
          115.625,
          -43.5
        ],
        [
          115.625,
          -46.5
        ]
      ]
    ]
  ]
}

example one error:

> db.blobs.createIndex({geolocation:'2dsphere'})
{
	"ok" : 0,
	"errmsg" : "Index build failed: 821317aa-a32a-4c7a-bd71-857716cc7626: Collection argo.blobs ( 69cdf012-53f3-4e51-bbf0-62985ef04e41 ) :: caused by :: Can't extract geo keys: [long error document suppressed] Edges 15 and 35 cross. Edge locations in degrees: [-53.0000000, 119.3750000]-[-52.0000000, 119.3750000] and [-60.0000000, 119.3750000]-[-60.5000000, 119.3750000]",
	"code" : 16755,
	"codeName" : "Location16755"

example two geometry:

{
  "type": "MultiPolygon",
  "coordinates": [
    [
      [
        [
          -146.25,
          -46
        ],
        [
          -143.75,
          -46
        ],
        [
          -143.75,
          -45.5
        ],
        [
          -142.5,
          -45.5
        ],
        [
          -142.5,
          -45
        ],
        [
          -141.25,
          -45
        ],
        [
          -141.25,
          -43
        ],
        [
          -136.875,
          -43
        ],
        [
          -136.875,
          -42.5
        ],
        [
          -134.375,
          -42.5
        ],
        [
          -134.375,
          -43
        ],
        [
          -133.75,
          -43
        ],
        [
          -133.75,
          -41
        ],
        [
          -135.625,
          -41
        ],
        [
          -135.625,
          -40.5
        ],
        [
          -136.875,
          -40.5
        ],
        [
          -136.875,
          -39.5
        ],
        [
          -137.5,
          -39.5
        ],
        [
          -137.5,
          -39
        ],
        [
          -138.75,
          -39
        ],
        [
          -138.75,
          -38.5
        ],
        [
          -141.25,
          -38.5
        ],
        [
          -141.25,
          -39
        ],
        [
          -141.875,
          -39
        ],
        [
          -141.875,
          -38.5
        ],
        [
          -142.5,
          -38.5
        ],
        [
          -142.5,
          -38
        ],
        [
          -143.125,
          -38
        ],
        [
          -143.125,
          -37.5
        ],
        [
          -144.375,
          -37.5
        ],
        [
          -144.375,
          -37
        ],
        [
          -145,
          -37
        ],
        [
          -145,
          -36.5
        ],
        [
          -145.625,
          -36.5
        ],
        [
          -145.625,
          -35.5
        ],
        [
          -146.25,
          -35.5
        ],
        [
          -146.25,
          -34.5
        ],
        [
          -146.875,
          -34.5
        ],
        [
          -146.875,
          -33.5
        ],
        [
          -147.5,
          -33.5
        ],
        [
          -147.5,
          -33
        ],
        [
          -148.125,
          -33
        ],
        [
          -148.125,
          -32.5
        ],
        [
          -148.75,
          -32.5
        ],
        [
          -148.75,
          -32
        ],
        [
          -149.375,
          -32
        ],
        [
          -149.375,
          -31.5
        ],
        [
          -150.625,
          -31.5
        ],
        [
          -150.625,
          -31
        ],
        [
          -151.875,
          -31
        ],
        [
          -151.875,
          -32
        ],
        [
          -152.5,
          -32
        ],
        [
          -152.5,
          -33.5
        ],
        [
          -151.875,
          -33.5
        ],
        [
          -151.875,
          -34
        ],
        [
          -151.25,
          -34
        ],
        [
          -151.25,
          -35
        ],
        [
          -150.625,
          -35
        ],
        [
          -150.625,
          -35.5
        ],
        [
          -150,
          -35.5
        ],
        [
          -150,
          -36.5
        ],
        [
          -149.375,
          -36.5
        ],
        [
          -149.375,
          -37.5
        ],
        [
          -148.75,
          -37.5
        ],
        [
          -148.75,
          -38.5
        ],
        [
          -148.125,
          -38.5
        ],
        [
          -148.125,
          -40
        ],
        [
          -147.5,
          -40
        ],
        [
          -147.5,
          -42
        ],
        [
          -146.875,
          -42
        ],
        [
          -146.875,
          -43.5
        ],
        [
          -146.25,
          -43.5
        ],
        [
          -146.25,
          -46
        ]
      ]
    ]
  ]
}

example two error:

> db.blobs.createIndex({geolocation:'2dsphere'})
{
	"ok" : 0,
	"errmsg" : "Index build failed: 6e707a5e-59b0-4760-8269-97d9e07d4a54: Collection argo.blobs ( 69cdf012-53f3-4e51-bbf0-62985ef04e41 ) :: caused by :: Can't extract geo keys: [long error document suppressed] Edges 5 and 21 cross. Edge locations in degrees: [-45.0000000, -141.2500000]-[-43.0000000, -141.2500000] and [-38.5000000, -141.2500000]-[-39.0000000, -141.2500000]",
	"code" : 16755,
	"codeName" : "Location16755"
}

In both cases the offending line segments are colinear, but clearly separated by several degrees. Why does mongo not like these shapes? Thanks!

It looks like this is a known bug, currently stuck in MongoDB’s backlog: https://jira.mongodb.org/browse/SERVER-52928

This is a pretty serious problem for us, I can’t fudge coordinates to make them artificially non-colinear in my use case to avoid this problem. We can close out here while that ticket remains open, but scientific applications need the above bug fixed when feasible.