Mongo malformed geo-query with MultiPolygon - mongodb

I'm using $geoWithin on a large list of geographic regions, defined in geoJSON. Some of these regions are Polygons, and some are MultiPolygons.
MultiPolygons fail, with a non-descript Malformed geo query error. After experimenting with the repl, I found I can't get mongo to take any MultiPolygon.
This is a minimal example:
{
"type" : "MultiPolygon",
"coordinates" : [
[
[
[ 9.560016, 42.152492 ],
[ 8.746009, 42.628122 ],
[ 9.390001, 43.009985 ],
[ 9.560016, 42.152492 ]
]
]
]
}
GEOJSONLint says it's fine, and shows me a triangle in the map, but mongo won't take it.
How is it malformed?

Related

How do I query a GeometryCollection in Mongodb?

I am trying to run a mongoldb query on a GEOjson collection of points and polygons but I'm struggling. I am new to GEOjson and Mongodb and haven't been able to find any examples on how to do this via google search and S.O. search so I'm hoping someone can shed some light on my issue.
I have created a 2DSphere index on geometry.coordinates - not sure if that's correct or not.
My overall objective is to return all points within the current map view and add both the point and the polygon associated to the map. I am using Leaflet.js and understand how to add to the map, but I can't figure out the query on Mongodb to get the results.
Just looking to find out if my GEOjson is correct and maybe an example on how I would perform the search on my GeometryCollection and get the point using $geowithin and then get the associated polygon.
Here's an example of my GEOjson-
{
"_id" : ObjectId("5709722f4044ccc4b8291651"),
"type" : "Feature",
"geometry" : {
"type" : "GeometryCollection",
"geometries" : [
{
"type" : "Point",
"coordinates" : [
-105.960805584616,
39.4671896545181
]
},
{
"type" : "Polygon",
"coordinates" : [
[
[
-105.96110187372227,
39.463449804295806
],
[
-105.96602244670311,
39.462956063632134
],
[
-105.96577627486317,
39.46671592928652
],
[
-105.96553007974012,
39.470475771657675
],
[
-105.96047434857059,
39.47089761421853
],
[
-105.95541854755197,
39.471319247231634
],
[
-105.95579990086372,
39.46763141937862
],
[
-105.95618125417559,
39.463943568242485
],
[
-105.96110187372227,
39.463449804295806
]
]
]
}
]
},
"properties" : {
"processed" : "Yes"
}
}

Geo-query using a circle as area to match at least one of the points of MultiPoint object in MongoDB

I have the following document in the entities collection at Mongo (a 2dsphere index for location.coords is in place):
> db.entities.find({},{location: 1}).pretty()
{
"_id" : {
"id" : "en3",
"type" : "t",
"servicePath" : "/"
},
"location" : {
"attrName" : "position",
"coords" : {
"type" : "MultiPoint",
"coordinates" : [
[
-3.691944,
40.418889
],
[
4.691944,
45.418889
]
]
}
}
}
As far as I have checked, $geoWithin only matches when the geometry includes all the points of the MultiPoint, e.g:
> db.entities.find({"location.coords": { $geoWithin: { $centerSphere: [ [ -3.691944, 40.418889 ], 0.002118976612776644 ] } } })
// Small circle centered at first point, but without covering the second point: it doesn't matchh
> db.entities.find({"location.coords": { $geoWithin: { $centerSphere: [ [ -3.691944, 40.418889 ], 2 ] } } })
// Big circle centered at first point covering also the second point: it matches
However, I would like to have a query to match if at least one point of the MultiPoint matches. I have read about the $geoIntersects operator. I have tried just replace $geoWithin by $geoIntersect in my query, but it doesn't work:
> db.entities.find({"location.coords": { $geoIntersects: { $centerSphere: [ [ -3.691944, 40.418889 ], 0.002118976612776644 ] } } })
error: {
"$err" : "Can't canonicalize query: BadValue bad geo query",
"code" : 17287
}
Reading the $geoIntersects operator, it seems that it can be only used with polygons or multi-polygons, but it doesn't mention circles. I wonder if I'm missing something, because this "asymmetry" between $geoWithin and $geoIntersects seems to be a bit weird...
Thus, is there any way of doing a geo-query using a circle as area to match at least one of the points of MultiPoint object?
I think I have found the answer at the end. It can be done with the $near operator, in the following way:
db.entities.find({"location.coords": { $near: { $geometry: { type: "Point", "coordinates": [ -3.691944, 40.418889 ] }, $maxDistance: 0.5 } }})

Can't extract geo keys' even though the GeoJSON is valid

I have a collection in MongoDB with a 2dsphere index. The object I want to save looks like this:
{
"type" : "Polygon",
"coordinates" : [
[
[
5.052617929724351,
52.64653192570052
],
[
5.051738165167465,
52.64765805672784
],
[
5.054162882116928,
52.64831549553909
],
[
5.054592035559312,
52.64780777138566
],
[
5.055364511755601,
52.64790541110375
],
[
5.056094072607651,
52.64688343792051
],
[
5.054237983969346,
52.64661654927096
],
[
5.052617929724351,
52.64653192570052
]
]
]
}
According to http://geojsonlint.com/ this is perfectly valid GeoJSON. However MongoDB says it can't extract the geo keys because the GeoJSON might be malformed.
Can anyone help me out and spot the mistake?
This is the MongoDB error I get:
insertDocument :: caused by :: 16755 Can't extract geo keys from object, malformed geometry?
The problem is that you are not providing the name of the top level object that the GeoJSON would be assigned to.
You must have created the "2dsphere" index on the "coordinates" field. Instead you want to create it on the field that this entire GeoJSON value will be assigned to.
db.geo.createIndex({"location":"2dsphere"})
db.geo.insert({"location" : {
"type" : "Polygon",
"coordinates" : [
[ <list of your-coord-pairs> ]
]
}})
WriteResult({ "nInserted" : 1 })

Why am I getting values outside polygon with geoIntersect

I am using mongodb with polygons stored with geometry in this format
"geometry" : { "type" : "Polygon", "coordinates" : [ [ [ 2.0315, 85.8546219515 ], [ 2.0315, 86.1673780485 ], [ 1.7185, 86.1673780485 ], [ 1.7185, 85.8546219515 ], [ 2.0315, 85.8546219515 ] ] ] }
I am trying to use geoIntersect and query a rectangle polygon using something like this:
find({geometry:{$geoIntersects:{$geometry:{"type": "Polygon", "coordinates":[ [[12,20], [13,20], [13,18], [12,18], [12,20]]]}}}})
When I do small queries like that, the results are fine, but when I expand to really large boxes:
find({geometry:{$geoIntersects:{$geometry:{"type": "Polygon", "coordinates":[ [[-166.2890625,46.55886030311719], [-166.2890625,47.39834920035926], [12.12890625,47.39834920035926], [12.12890625,46.55886030311719], [-166.2890625,46.55886030311719]]]}}}})
then I start getting really odd results like
"geometry" : { "type" : "Polygon", "coordinates" : [ [ [ 2.9684999999999997, 85.54162195149999 ], [ 2.9684999999999997, 85.8543780485 ], [ 2.6555, 85.8543780485 ], [ 2.6555, 85.54162195149999 ], [ 2.9684999999999997, 85.54162195149999 ] ] ] }
I am only returned a few polygons (4 total) from the db that are actually inside my input polygon:
"geometry":{"type":"Polygon","coordinates":[[[12.343499999999999,47.1416219515],[12.343499999999999,47.454378048500004],[12.0305,47.454378048500004],[12.0305,47.1416219515],[12.343499999999999,47.1416219515]]]}}
"geometry":{"type":"Polygon","coordinates":[[[12.343499999999999,46.829621951499995],[12.343499999999999,47.1423780485],[12.0305,47.1423780485],[12.0305,46.829621951499995],[12.343499999999999,46.829621951499995]]]}}
"geometry":{"type":"Polygon","coordinates":[[[-166.0935,46.5166219515],[-166.0935,46.829378048500004],[-166.4065,46.829378048500004],[-166.4065,46.5166219515],[-166.0935,46.5166219515]]]}}
"geometry":{"type":"Polygon","coordinates":[[[-166.0935,46.829621951499995],[-166.0935,47.1423780485],[-166.4065,47.1423780485],[-166.4065,46.829621951499995],[-166.0935,46.829621951499995]]]}}
I shouldn't have any latitudes in the 80's with my input. I don't know where these are coming from. They make up the majority of what is returned. (847/851 results)
I don't think the polygon fits in one hemisphere. From the $geoIntersects docs,
Any geometry specified with GeoJSON to $geoIntersects queries, must fit within a single hemisphere.

Mongodb Sphere index rejects my object

Hi i have an COllection with an 2d sphere index
wayCollection.createIndex(new BasicDBObject("poly", "2dsphere"));
i when i try to insert a certain object i get an error:
Exception in thread "main" com.mongodb.WriteConcernException: { "serverUsed" : "localhost:27017" , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 16755 Can't extract geo keys from object, malformed geometry?: { _id: ObjectId('53a89dc6a0042baf54050037'), id: 24837452, version: \"64\", changeset: \"13731788\", uid: \"362915\", user: \"micha555\", timestamp: \"2012-11-03T09:11:23Z\", poly: { type: \"Polygon\", coordinates: [ [ [ 9.1244131, 50.0109503 ], [ 9.120464800000001, 50.0099731 ], [ 9.116641, 50.0077
although the multipolygon ring is closed (first and last coordinate is the same)
any ideas?
Here is another polygon which is rejected:
{
"type":"Polygon",
"coordinates":[
[
[
9.0496053,
51.3255584
],
[
9.0492235,
51.32532
],
[
9.0495831,
51.3251321
],
[
9.049917199999999,
51.3253429
],
[
9.0496053,
51.3255584
],
[
9.049984500000001,
51.3258463
],
[
9.0505795,
51.3262127
],
[
9.050045600000001,
51.326574
],
[
9.0496432,
51.3263928
],
[
9.049625300000001,
51.3262806
],
[
9.0495628,
51.3261244
],
[
9.0495316,
51.3259341
],
[
9.0495471,
51.3257127
],
[
9.0496053,
51.3255584
]
]
]
}
when i paste it here it seems to be ok: http://geojsonlint.com/
maybe mongo db does not like when a point is used twice
The polygon is self-intersecting:
MongoDB prohibits such polygons.
First thing I would do is check your polygon at http://geojsonlint.com/ . If it does not pass then you know you have an issue with your data points (which has caused this error for many people previously).
If it does pass then I would check this bug that was recently reported and submitted to MongoDB: https://jira.mongodb.org/browse/SERVER-13735 . Basically a valid MultiPolygon is causing an error with the 2dsphere index on MongoDB 2.6.X. Others have reported similar issues so my bet is that it's a bug, or some undocumented limitation.
I'd vote up the issue on JIRA and add your voice to the comments - the more people let MongoDB know that this bug exists and is impacting them the faster it will get fixed.