odiszapc
odiszapc

Reputation: 4109

MongoDB $geoWithin statement doesn't work for wide ranges

The following doc:

{
    "_id" : ObjectId("56dd4fb755eb122ee622e17d"),
    "geo_segments" : {
        "type" : "MultiPoint",
        "coordinates" : [ 
            [ 
                90, 
                0
            ]
        ]
    }
}

Can be found using:

db.collection.find({
    "geo_segments": {
      $geoWithin: {
        $geometry: {
          type : "Polygon" ,
          coordinates: [ [
            [-131, -90], [-131, 90],
            [131, 90], [131, -90],
            [-131, -90]
          ] ]
        }
      }
    }
});

But nothing is found when I change all 131 occurences to 132:

db.collection.find({
    "geo_segments": {
      $geoWithin: {
        $geometry: {
          type : "Polygon" ,
          coordinates: [ [
            [-132, -90], [-132, 90], [132, 90], [132, -90], [-132, -90]
          ] ]
        }
      }
    }
});

Why this won't work? CRS feature also doesn't seem to work:

db.collection.find({
    "geo_segments": {
      $geoWithin: {
        $geometry: {
          type : "Polygon" ,
          coordinates: [ [
            [-132, -90], [-132, 90], [132, 90], [132, -90], [-132, -90]
          ] ],
          crs: {
            type: "name",
            properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
          }
        }
      }
    }
});

Field is indexed with 2dsphere, Mongo is 3.3.1

UPD:

Tried to set points in reversed order to treat polygon as "include" area, with no results:

db.geo_series.find({
    "geo_segments": {
      $geoWithin: {
        $geometry: {
          type : "Polygon" ,
          coordinates: [ [
            [-132, -90], [132, -90], [132, 90], [-132, 90], [-132, -90]
          ] ]
        }
      }
    }
});

Upvotes: 2

Views: 640

Answers (1)

Blakes Seven
Blakes Seven

Reputation: 50426

Like the documentation says, "single ring Polygon in counter-clockwise winding "(sic)

db.geo_series.find({
    "geo_segments": {
      "$geoWithin": {
        "$geometry": {
          "type" : "Polygon" ,
          "coordinates": [ [
            [-132, -90],[132, -90], [132, 90], [-132, 90], [-132, -90]
          ] ],
          "crs": {
            "type": "name",
            "properties": { "name": "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
          }
        }
      }
    }
});

So the direction the polygon is defined in is important, as is the "crs" since your polygon does extend beyond a single hemisphere.

FYI, your polygon is basically the same as the reference Polygon in the documentation in that is crosses hemispheres and reverse orders of longitude and latitude, but just over a slightly larger area.

Upvotes: 1

Related Questions