0

I would like to have a collection with polygons (certain areas on the planet) and considering that I have a point(a single place on the planet), I would like to know which polygons contain this point.

consider the collection like.

{
    "_id" : "xxx",
    "geo" : {
        "type" : "MultiPolygon",
        "coordinates" : [ 
            [ 
                [ 
                    [ 
                        34.5245361328125, 
                        26.4386062678873
                    ], 
                    [ 
                        34.5327758789062, 
                        26.4754901967738
                    ], 
                    [ 
                        34.5272827148438, 
                        26.5099045314139
                    ], 
                    [ 
                        34.508056640625, 
                        26.5688765479507
                    ], 
                    [ 
                        34.5135498046875, 
                        26.6376388866459
                    ], 
                    [ 
                        34.5053100585938, 
                        26.7210803908617
                    ], 
                    [ 
                        34.4943237304688, 
                        26.7872992221561
                    ], 
                    [ 
                        34.5135498046875, 
                        26.8608304092814
                    ], 
                    [ 
                        34.5382690429688, 
                        26.9587976785709
                    ], 
                    [ 
                        34.5767211914062, 
                        27.08113703389
                    ], 
                    [ 
                        34.595947265625, 
                        27.2741611173747
                    ], 
                    [ 
                        34.5492553710938, 
                        27.3766453536396
                    ], 
                    [ 
                        34.12353515625, 
                        27.5448063177539
                    ], 
                    [ 
                        33.59893798828125, 
                        27.94467306257741
                    ], 
                    [ 
                        33.6676025390625, 
                        27.51314343580719
                    ], 
                    [ 
                        33.5577392578125, 
                        27.3912773132324
                    ], 
                    [ 
                        33.4588623046875, 
                        27.3351745605469
                    ], 
                    [ 
                        33.4259033203125, 
                        27.2326526641846
                    ], 
                    [ 
                        33.4121704101562, 
                        27.1300354003906
                    ], 
                    [ 
                        33.4808349609375, 
                        27.0077495574951
                    ], 
                    [ 
                        33.5467529296875, 
                        26.8804302215576
                    ], 
                    [ 
                        33.6044311523438, 
                        26.7382526397705
                    ], 
                    [ 
                        33.6758422851562, 
                        26.5664196014404
                    ], 
                    [ 
                        33.7197875976562, 
                        26.4090900421143
                    ], 
                    [ 
                        33.760986328125, 
                        26.2712516784668
                    ], 
                    [ 
                        33.81591796875, 
                        26.1505069732666
                    ], 
                    [ 
                        33.8818359375, 
                        26.0937881469727
                    ], 
                    [ 
                        33.9669799804688, 
                        26.1381797790527
                    ], 
                    [ 
                        34.0713500976562, 
                        26.2318382263184
                    ], 
                    [ 
                        34.1372680664062, 
                        26.2540092468262
                    ], 
                    [ 
                        34.1867065429688, 
                        26.2663249969482
                    ], 
                    [ 
                        34.2333984375, 
                        26.2761764526367
                    ], 
                    [ 
                        34.27734375, 
                        26.2811031341553
                    ], 
                    [ 
                        34.3927001953125, 
                        26.3081885431537
                    ], 
                    [ 
                        34.4833374023438, 
                        26.3451141501178
                    ], 
                    [ 
                        34.5245361328125, 
                        26.4066306425568
                    ], 
                    [ 
                        34.5245361328125, 
                        26.4386062678873
                    ]
                ],
                ...
            ]
        ]
    }
}

How do I achieve this?

db['countries'].find( { 
  "geo.coordinates" : { 
    $geoIntersects : { 
      $geometry : { 
        type : "Point" ,
        coordinates : [ 
          34.5245361328125, 
          26.4386062678873 
        ]
      } 
    } 
  }
})
oleber
  • 1,089
  • 4
  • 12
  • 25
  • Can you show us a code attempt? There is no built-in function nor operator in MongoDB. There are operators to easily find the points contained in a given polygon, especially in GeoJSON format. You can certainly do something with that. – dgiugg Jul 15 '14 at 07:24
  • My problem is exactly that, I looked to the documentation and I didn't found a solution. – oleber Jul 15 '14 at 07:43
  • 1
    To what extent is your question different from those: http://stackoverflow.com/questions/24578363/mongodb-check-if-point-is-in-polygon, https://stackoverflow.com/questions/15866490/mongodb-check-if-a-point-is-inside-a-stored-polygon? – dgiugg Jul 15 '14 at 07:52
  • What version of MongoDB are you using? I would highly advise you to use GeoJSON rather than legacy points, as in your example above. If you use GeoJSON, then either of the two links above as pointed out by dgiugg will answer your question. – John Powell Jul 16 '14 at 07:40
  • I hop that my editing help you. – oleber Jul 16 '14 at 11:40
  • I am still not clear how this differs from http://stackoverflow.com/questions/24578363/mongodb-check-if-point-is-in-polygon%E2%80%8C%E2%80%8B. The main issue with you query, though, is you should put db.countries.find({geo:{$geoIntersects...... The coordinates is implied, so geo.coordinates is not necessary. If that still doesn't work, update your question to make it clear how it is different, and someone will try and answer. – John Powell Jul 18 '14 at 10:05

0 Answers0