[SERVER-24251] $geoIntersects does not find the (right) polygon Created: 23/May/16  Updated: 20/Jun/16  Resolved: 20/Jun/16

Status: Closed
Project: Core Server
Component/s: Geo
Affects Version/s: 3.2.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Marc Bourqui [X] Assignee: Kelsey Schubert
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

Create a collection containing those two documents:

{ "_id" : 1, "geometry" : { "type" : "Polygon", "coordinates" : [ [ [ 2.119381, 41.33061 ], [ -174.006166, -90 ], [ -180, -90 ], [ -180, -8.695196 ], [ -2.19644, 40.196647 ], [ 2.119957, 41.335644 ], [ 2.120312, 41.333592 ], [ 2.119381, 41.33061 ] ] ] }, "type" : "Feature", "properties" : {  } }
{ "_id" : 2, "geometry" : { "type" : "Polygon", "coordinates" : [ [ [ 2.12758, 41.323953 ], [ 2.110699, 41.113954 ], [ -12.960389, -90 ], [ -174.006166, -90 ], [ 2.119381, 41.33061 ], [ 2.12758, 41.323953 ] ] ] }, "type" : "Feature", "properties" : {  } }

The following query should return doc 2, but instead returns nothing:

db.collection.find({"geometry":{$geoIntersects:{$geometry:{"type":"Point", "coordinates":[2.12127,41.32236]}}}})

The following query should return doc 2, but instead returns doc 1:

db.collection.find({"geometry":{$geoIntersects:{$geometry:{"type":"Point", "coordinates":[2.10154,41.29387]}}}})

Participants:

 Description   

For some points and polygons, $geoIntersects() returns either no result or a neighboring polygon instead of the expected polygon.



 Comments   
Comment by Kelsey Schubert [ 20/Jun/16 ]

Hi Kibour,

The second polygon is not valid as it self-intersects. If you attempt to create a 2dsphere index using this data the resulting error provides additional information.

> db.collection.insert({ "_id" : 2, "geometry" : { "type" : "Polygon", "coordinates" : [ [ [ 2.12758, 41.323953 ], [ 2.110699, 41.113954 ], [ -12.960389, -90 ], [ -174.006166, -90 ], [ 2.119381, 41.33061 ], [ 2.12758, 41.323953 ] ] ] }, "type" : "Feature", "properties" : {  } } )
WriteResult({ "nInserted" : 1 })
> db.collection.createIndex({geometry:'2dsphere'})
{
	"ok" : 0,
	"errmsg" : "Can't extract geo keys: { _id: 2.0, geometry: { type: \"Polygon\", coordinates: [ [ [ 2.12758, 41.323953 ], [ 2.110699, 41.113954 ], [ -12.960389, -90.0 ], [ -174.006166, -90.0 ], [ 2.119381, 41.33061 ], [ 2.12758, 41.323953 ] ] ] }, type: \"Feature\", properties: {} }  Loop is not valid: [ [ 2.12758, 41.323953 ], [ 2.110699, 41.113954 ], [ -12.960389, -90.0 ], [ -174.006166, -90.0 ], [ 2.119381, 41.33061 ], [ 2.12758, 41.323953 ] ] Edges 0 and 3 cross. Edge locations in degrees: [2.1275800, 41.3239530]-[2.1106990, 41.1139540] and [-174.0061660, -90.0000000]-[2.1193810, 41.3306100]",
	"code" : 16755
}

If you need to query against this shape, I would suggest converting into a MultiPolygon to resolve the ambiguity inherent to self-intersecting polygons.

Please note that SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For MongoDB-related support discussion please post on the mongodb-users group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-users group.

Kind regards,
Thomas

Comment by Marc Bourqui [X] [ 23/May/16 ]

Indeed, I was checking using geojsonlint.com, and my polygons looked good there.
I checked them on the link you provided, and the shapes are different.

  • Especially, for the second case, it is now clear that the point belongs to the doc 1.
  • The shape of the polygon from doc 2 looks weird. According to the geodndmap, the point lies in the polygon when $geoIntersects returns nothing.
Comment by Ramon Fernandez Marina [ 23/May/16 ]

Kibour, those polygon definitions look a bit strange. Please note that using something like geojsonlint.com may not be the best way to test geo coordinates – can you please check your geometries at https://s3.amazonaws.com/geodndmap/index.html ? This site is a hosted version of geodndmap, and provides accurate representation of geo coordinates.

Generated at Thu Feb 08 04:05:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.