Mongodb Sphere index rejects my object - mongodb

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.

Related

mongodb 2dsphere index fails on MultiPolygon (malformed geometry)

I am struggling to undestand why the following indexing fails:
> db.mycollection.ensureIndex({geometry:"2dsphere"})
{
"err" : "Can't extract geo keys from object, malformed geometry?:{ type: \"MultiPolygon\", coordinates: [ [ [ [ -0.097262623621224, 51.52157883707353 ], [ -0.096489562220012, 51.52027612781475 ], [ -0.095242927823793, 51.52050373756695 ], [ -0.094744446266, 51.5205960952269 ], [ -0.094366848238175, 51.5206711083638 ], [ -0.094478602055745, 51.51976097300852 ], [ -0.095268219478419, 51.51892749907517 ], [ -0.095057671259463, 51.51823866169356 ], [ -0.09590752332937, 51.51675447483272 ], [ -0.094917254103398, 51.51658818255699 ], [ -0.095455170681833, 51.51544167621788 ], [ -0.09577124973446099, 51.51496874589772 ], [ -0.096562908994151, 51.5148429136635 ], [ -0.097957187488107, 51.51553859434178 ], [ -0.097828201440347, 51.51660172221631 ], [ -0.09972288755426, 51.5167693068687 ], [ -0.099643129935966, 51.51745174289048 ], [ -0.099140106492407, 51.51776283078856 ], [ -0.098174109996819, 51.51789906140922 ], [ -0.098017592779996, 51.5175476311896 ], [ -0.097562458108715, 51.51768194540703 ], [ -0.097737151858934, 51.519666041172 ], [ -0.0978402555155, 51.51992164186168 ], [ -0.098548930349501, 51.51973516284531 ], [ -0.098498300867191, 51.52053989289254 ], [ -0.09761991242012499, 51.52069031743765 ], [ -0.097262623621224, 51.52157883707353 ] ] ] ] }",
"code" : 16572,
"n" : 0,
"connectionId" : 61,
"ok" : 1
}
I checked all the related question on SO but couldn't figure out the problem. They all pointed out to the lon,lat order and the format of coordinates. I am using Mongodb 2.4.
one possible explanation can be this but I'm not sure
MongoDB version 2.6 still fails on 2dsphere geoindexing citing inability to extract geokeys and possible malformed geometry
The Multipolygon geometry type is not supported in MongoDB 2.4 (see Geospatial enhancements in MongoDB 2.6).
Please note that as of this writing, MongoDB 2.4 is not a supported version anymore. If this is for a new project, I would recommend using the latest version (currently MongoDB 3.6.2) to ensure that you are not affected by any historical issues that were fixed in newer versions.

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"
}
}

Mongo malformed geo-query with MultiPolygon

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?

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.