20

I'm new to the mongodb geolocation features.

I stored some polygons that represent the country borders in a database along with the country name. Now what i would like to do is checking which country a point is in. For example if i give my own geolocation i would like to get the country where i am.

Is there a way to do it with mongodb? Maybe with geoWithin?

Thank you

SkinyMonkey
  • 259
  • 1
  • 3
  • 7
  • Please check this answers: http://stackoverflow.com/questions/12095965/mongodb-how-can-i-check-if-a-point-is-contained-in-a-polygon and http://stackoverflow.com/questions/10774427/how-to-find-if-a-point-exists-in-which-polygon. It seems that both are relevant and have solution. – Rasim Apr 07 '13 at 19:17
  • 2
    I need the same. @brotherofken: the question is exactly reverse: I need to search wich polygon (stored on mongodb) contains a parameter point. I can't found any example in this direction. – g.annunziata Sep 12 '13 at 09:15
  • I need to find if a point lies inside a closed circle. Any leads on how would I achieve that? – Saras Arya Feb 18 '16 at 07:44

4 Answers4

9

You must store your location data like this schema:

{"loc":
     {"coordinates":[
       [
         [1.0,1.0],
         [1.0,10.0],
         [10.0,10.0],
         [10.0,1.0],
         [1.0,1.0]
       ]
      ],
     "type":"Polygon"
   }
}

and then send $geoIntersects queries

db.polygons.find({"loc":{"$geoIntersects":{"$geometry":{"type":"Point", "coordinates":[x, y]}}}}
David Chaava
  • 184
  • 1
  • 6
  • Any reason why there is an array within an array for coordinates? What would adding another set of coordinates do for us? e.g [[[1,1],...],[[2,2]...]] – Stan Bondi Sep 15 '14 at 08:45
  • perhaps one polygon can be located inside the other – David Chaava Nov 17 '14 at 10:04
  • Yup, I found out a while ago that the next array elements specify polygons which "cut out" the outer one. https://developers.google.com/maps/documentation/javascript/examples/layer-data-simple – Stan Bondi Nov 18 '14 at 10:55
7

You have to use $geoIntersects for that.

db.features.find({mystoredpolygon:{$geoIntersects:{$geometry:{type:'Point', coordinates:[22,38]}}}})
gtsouk
  • 5,208
  • 1
  • 28
  • 35
  • Yes, $geoIntersects works. Here's the reference link: [MongoDB $geoIntersects](http://docs.mongodb.org/manual/reference/operator/query/geoIntersects/) – Adam Feb 04 '15 at 21:24
4

Yes, you can use $geoWithin for the check, provided you define the countries as polygons using GeoJSON. For that, try this GitHub repo.

Sim
  • 13,147
  • 9
  • 66
  • 95
  • 1
    Thank you for the polygons but i got a more precise set, i don't remember where i got it though. Do you think that i'll have to iterate over all these polygons to find in which the point is or does mongodb geoWithin helps me to avoid this kind of backtracking? – SkinyMonkey Apr 08 '13 at 12:14
  • 1
    geoWithin returns all features *within* a polygon. geoIntersects returns the feature that *intersect* with a feature (a point, in this case). Therefore, in this case, geoIntersects gives you exactly what you want in one query, whereas you'd have to run geoWithin with every polygon you have. – Cody Django Feb 13 '16 at 21:39
0

Your scenario seems to be identical with the MongoDB Geo-spatial Tutorial example which uses $geoIntersects instead of $geoWithin.

As in the example shown in the MongoDB documentation you can use $geoIntersects Geo-spatial query feature of MongoDB assuming that you have a polygon representing countries stored in your database and a point is your current location, you can use following query.

db.neighborhoods.findOne({
     geometry: { 
        $geoIntersects: {
             $geometry: { 
                type: "Point", 
                coordinates: [ -73.93414657, 40.82302903 ] 
            }
        } 
    } 
})

where, neighborhood is your collection which stores in your case country's polygon. geometry in second line of query is your actual polygon stored in DB (which is Geo-Json format). And "Point" inside $geometry is your current location that you want to fine country of.

dips
  • 21
  • 7