Error parsing GeoJSON polygon in query in 2.4rc3

930 views
Skip to first unread message

Matt X

unread,
Mar 17, 2013, 12:32:46 PM3/17/13
to mongod...@googlegroups.com
I'm having trouble running this query against a field indexed as 2dspherical using mongodb 2.4rc3


db.contact2.find({geo_2: { $within:
                             { $geometry: { "type": "Polygon",
                                            "coordinates": [[ 
                                [-88.352051,30.977609],
                                [-81.320801,30.694612],
                                [-79.431152,25.383735],
                                [-81.013184,24.487149],
                                [-82.990723,27.741885],
                                [-83.540039,29.401320],
                                [-88.308105,30.126124] 
                                ]]
                             } 
                             }
                             } });

I have tried using both $within and $geoIntersects (not sure that one is what I want) and both of these give me a parsing error

error: {
"$err" : "can't parse query (2dsphere): { $within: { $geometry: { type: \"Polygon\", coordinates: [ [ [ -88.352051, 30.977609 ], [ -81.320801, 30.694612 ], [ -79.431152, 25.383735 ], [ -81.013184, 24.487149 ], [ -82.990723, 27.741885 ], [ -83.54003899999999, 29.40132 ], [ -88.308105, 30.126124 ] ] ] } } }",
"code" : 16535
}

I used http://geojsonlint.com/ to validate the Polygon JSON and it says it's correct so I don't know why the query is rejected.  I can successfully do similar queries against 2d indexes using $within+$polygon but I was hoping for better count performance with the 2dspherical index.

Asya Kamsky

unread,
Mar 17, 2013, 8:16:30 PM3/17/13
to mongod...@googlegroups.com
Do you have too many [ ] square brackets?  (after coordinates you seem to have three [ where I would expect two.

Ronald Stalder

unread,
Mar 17, 2013, 8:32:04 PM3/17/13
to mongod...@googlegroups.com
no, it's okay, I've checked this with the geoJSON spec:

a polygon is an array of points in an array of circular lines (here only one of them), which are an array of lines

-Ronald
 

Matt X

unread,
Mar 17, 2013, 9:22:47 PM3/17/13
to mongod...@googlegroups.com
I started with a single pair but then I noticed both the mongodb and the geojson specification use double square brackets.  I thing the spec allows you to specify a polygon as an array of multiple shapes.


http://geojson.org/geojson-spec.html#id18

--
--
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.
 
 

Asya Kamsky

unread,
Mar 18, 2013, 12:54:25 PM3/18/13
to mongod...@googlegroups.com
Okay I found what it doesn't like - truncate off the decimals and the syntax becomes acceptable to the query.

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+unsubscribe@googlegroups.com.

Ronald Stalder

unread,
Mar 18, 2013, 1:29:04 PM3/18/13
to mongod...@googlegroups.com

Em 18-03-2013 13:54, Asya Kamsky escreveu:
> Okay I found what it doesn't like - truncate off the decimals and the
> syntax becomes acceptable to the query.
>

Yes, this works, but that's pretty useless, if you want to find out, for
example, in what city/neighborhood you are....

Ronald Stalder

unread,
Mar 18, 2013, 1:36:45 PM3/18/13
to mongod...@googlegroups.com
Just one of these funny little things:

it works with decimals!

the point is that you have to close  the polygon (and by removing the decimals, the polygon got accidentally closed: last item = first item)

cheers
Ronald

mattx

unread,
Mar 18, 2013, 2:09:58 PM3/18/13
to mongod...@googlegroups.com
Thanks!  This fact should be in the doc.  It's a big gotcha... Now the query works.  I'll follow up on performance issues on the performance/indexing thread.

Hari Khalsa

unread,
Mar 18, 2013, 2:12:21 PM3/18/13
to mongod...@googlegroups.com
This is correct.  The spec mentions:

"For type "Polygon", the "coordinates" member must be an array of LinearRing coordinate arrays. For Polygons with multiple rings, the first must be the exterior ring and any others must be interior rings or holes."

and

"A LinearRing is closed LineString with 4 or more positions. The first and last positions are equivalent (they represent equivalent points). Though a LinearRing is not explicitly represented as a GeoJSON geometry type, it is referred to in the Polygon geometry type definition."


On Monday, March 18, 2013 1:36:45 PM UTC-4, Ronald Stalder wrote:
Reply all
Reply to author
Forward
0 new messages