Re: GeoJSON geometry query not working

333 views
Skip to first unread message

Dan Pasette

unread,
Apr 13, 2013, 1:47:16 PM4/13/13
to mongod...@googlegroups.com
Hi Billy,
Your syntax is correct, however you're bumping into a limit of 2dsphere indexes.  The bounding polygon must be < a hemisphere.  If it's greater than a hemisphere, it won't return anything.  I checked, and this info is missing from the documentation, but generally speaking, most users will want to limit their queries to less than the whole sphere, and if they do want everything, adding the geo query predicate would not be useful in any case.  The docs and error messages should definitely be more clear in this case though.

Dan

On Friday, April 12, 2013 5:24:34 PM UTC-4, Billy Newman wrote:
I have stored some GeoJSON points in my db and I cannot query them using the new geoWithin (2.4.1).

Here is the indexes for my collection:

    > db.features1.getIndexes();
    [
        {
            "v" : 1,
            "key" : {
                "_id" : 1
            },
            "ns" : "sagedb.features1",
            "name" : "_id_"
        },
        {
            "v" : 1,
            "key" : {
                "geometry" : "2dsphere"
            },
            "ns" : "sagedb.features1",
            "name" : "geometry_2dsphere"
        }
    ]

Here is the one document I have in my db

    > db.features1.find();
    {
      "_id" : ObjectId("51672e677a62bb0000000003"),
      "geometry" : {
        "type" : "Point",
        "coordinates" : [ -106.8610919, 49.665165099999996 ] },
      "__v" : 0 }

I am trying to run this query:

    > db.features1.find( {
      geometry: {
        $geoWithin: {
          $geometry: {
            type: "Polygon",
            coordinates: [ [ [-180,-90], [-180,90], [180,90], [180,-90], [-180,-90] ] ] } } } });

But I get nothing back.  Any ideas what I am doing wrong?

Billy Newman

unread,
Apr 14, 2013, 10:01:38 AM4/14/13
to mongod...@googlegroups.com
Agreed the docs need to be updated.  In the end that is pretty limiting as I cannot control the bounds of my users queries, if they want the entire world I would like to be able to handle that request.  I am also storing points, lines and polygons and would like to return any geometries that match a geoWithin/geoIntersects so I am stuck with 2dsphere.

I think my only option at this point is to break apart queries on the timeline, form two queries and merge the results.

Any idea if this is going to be patched/fixed to work?


Thanks for the help, this was driving me nuts!
Billy

Dan Pasette

unread,
Apr 14, 2013, 6:52:09 PM4/14/13
to mongod...@googlegroups.com
To support >= hemisphere requires coordinates to have "handed-ness" and GeoJSON doesn't have that.  Even to error out in a sane way requires having orientations on polygons.  How else do you tell a slightly-larger-than-hemi from a slightly-smaller-than-hemi polygon?  Without having any idea of which "side" of the polygon the user is trying to describe.  Will have to look into how other systems handle this, but for now you'll have to find a client-side solution as there are no imminent plans on removing this constraint.

Billy Newman

unread,
Apr 15, 2013, 1:25:55 PM4/15/13
to mongod...@googlegroups.com
Dan,

Agreed, good point.  I did not realize that GeoJSON did not assume a particular handedness.

It is a little disconcerting that the you have no plans to fix this in the future and plan to push this problem onto all clients that use mongo.  In the end it seems like a little 'convention over specification' is in order.  I.E. just because GeoJSON does not specify a handedness does not mean that mongo cannot provide some convention on handedness when uses submit queries over 180 degrees in longitude. 

Not sure if its possible to also provide a bounding box (lower left, upper right) query for the 2dsphere index, at least then you know which direction to 'draw' the polygon.  This would of course not solve the arbitrary large polygon ( < 180 degrees), but most map clients query without bounding boxes rather than polygons.  Similar to the bbox query for '2d' indexes that is in place.

Lastly agreed that a query over the entire earth is not limiting records coming back from the DB and probably not a good idea.  However when providing users with an API that they can query I have to cover all basis, they can query for data over any area they want.

Thanks for all your help.  I truly appreciate mongo and the time you spend here helping is not lost on me.

Billy

Dan Pasette

unread,
Apr 16, 2013, 3:06:13 PM4/16/13
to mongod...@googlegroups.com
Hi Billy,
We're adding to the docs shortly (see: https://jira.mongodb.org/browse/DOCS-1391) and the discussion on this issue in terms of adding this functionality continues here: https://jira.mongodb.org/browse/SERVER-9346.  There is a case to be made for 'convention over specification', but there are some nuances to consider.  I believe making this sort of change would be backward breaking, so we will have to be very careful in the case this approach is adopted.  Stay tuned.

Dan


--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages