hisoka
hisoka

Reputation: 43

mongodb error: 'longitude/latitude is out of bounds

when I run the mongoDB query, I get error which is 'longitude/latitude is out of bounds.

Here is my query is created:

DBObject query = new BasicDBObject("loc",
            new BasicDBObject("$geoWithin",
                    new BasicDBObject("$geometry",
                            new BasicDBObject("type","Polygon").append("coordinates", polygon))));

Here is my mongoDB query:

db.collection_name.find(
   { "loc" : 
        { "$geoWithin" : 
            { "$geometry" : 
                { "type" : "Polygon" , "coordinates" : [ [ [ 44.67047 , 136.01075] , [ 77.05318 , 33.59619] , [ 13.04135 , -22.31323]]]

                }
            }
        }
   } 
)

I get this error output:

{
"code" : {
    "_bsontype" : "Int32",
    "value" : 2
},
"codeName" : "BadValue",
"errmsg" : "longitude/latitude is out of bounds, lng: 44.6705 lat: 136.011",
"message" : "longitude/latitude is out of bounds, lng: 44.6705 lat: 136.011",
"name" : "MongoError",
"ok" : {
    "_bsontype" : "Double",
    "value" : 0
},

How can I solve this problem ? Thank you for your answers.

Upvotes: 2

Views: 3170

Answers (1)

Wan B.
Wan B.

Reputation: 18845

"longitude/latitude is out of bounds, lng: 44.6705 lat: 136.011"

You need to specify in the order of longitude then latitude. The valid range of latitude in degrees is -90 and +90. Your value of latitude of 136.011 is out of range.

"Loop is not closed"

To specify geospatial query for $geometry you need to close the GeoJSON Polygon loop in order to create a polygon shape.

For example:

{
  type: "Polygon",
  coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0  ] ] ]
}

Note that the first and last coordinates are matching, in order to close the polygon shape.

The above is assuming that you're referring to GeoJSON polygons with a single ring.

Upvotes: 1

Related Questions