多边形mongodb中的点

时间:2014-04-14 07:47:49

标签: mongodb geospatial mongodb-query

我已经从oracle将多边形导出到mongodb中。现在我试图实现这样的事情(以下查询在oracle空间中工作)。

select * from geoloc g 
where  SDO_CONTAINS(
    g.geoloc,
   SDO_GEOMETRY('POINT(-77.44848485115146  38.94291436764032)',8307)) = 'TRUE' 

这样我就可以获得包含所提供点的所有多边形。

多边形如下:

{
    "_id" : "4c103c0f-4dc6-4bfa-88f4-712bb498ce39",
    "loc" : {
            "type" : "Polygon",
            "coordinates" : [
                    [
                            [
                                    31.9203654668208,
                                    77.36639
                            ],
                            [
                                    31.9077732910309,
                                    77.4294610599561
                            ],
                            [
                                    31.8700925977222,
                                    77.4920521110627
                            ],
                            [
                                    31.8076101597232,
                                    77.5536867976271
                            ],
                            [
                                    31.7208015061148,
                                    77.6138960424688
                            ],
                            [
                                    31.6103273031675,
                                    77.672221616879
                            ],
                            [
                                    31.477028326272,
                                    77.7282196280186
                            ],
                            [
                                    31.3219190611305,
                                    77.7814638972103
                            ],
                            [
                                    31.1461799829082,
                                    77.8315492034157
                            ],
                            [
                                    30.9511485721047,
                                    77.8780943672123
                            ],
                            [
                                    30.7383091355193,
                                    77.9207451517989
                            ],
                            [
                                    30.5092815097803,
                                    77.9591769589515
                            ],
                            [
                                    30.2658087334104,
                                    77.9930972994119
                            ],
                            [
                                    30.0097437812505,
                                    78.0222480189076
                            ],
                            [
                                    29.7430354622029,
                                    78.0464072628615
                            ],
                            [
                                    29.4677135876184,
                                    78.0653911648394
                            ],
                            [
                                    29.1858735232065,
                                    78.0790552458845
                            ],
                            [
                                    28.899660242037,
                                    78.0872955140892
                            ],
                            [
                                    28.611252,
                                    78.0900492560372
                            ],
                            [
                                    28.3228437579629,
                                    78.0872955140892
                            ],
                            [
                                    28.0366304767935,
                                    78.0790552458845
                            ],
                            [
                                    27.7547904123815,
                                    78.0653911648394
                            ],
                            [
                                    27.479468537797,
                                    78.0464072628615
                            ],
                            [
                                    27.2127602187494,
                                    78.0222480189076
                            ],
                            [
                                    26.9566952665896,
                                    77.9930972994119
                            ],
                            [
                                    26.7132224902197,
                                    77.9591769589515
                            ],
                            [
                                    26.4841948644807,
                                    77.9207451517989
                            ],
                            [
                                    26.2713554278953,
                                    77.8780943672123
                            ],
                            [
                                    26.0763240170917,
                                    77.8315492034157
                            ],
                            [
                                    25.9005849388695,
                                    77.7814638972102
                            ],
                            [
                                    25.745475673728,
                                    77.7282196280186
                            ],
                            [
                                    25.6121766968325,
                                    77.672221616879
                            ],
                            [
                                    25.5017024938852,
                                    77.6138960424688
                            ],
                            [
                                    25.4148938402768,
                                    77.5536867976271
                            ],
                            [
                                    25.3524114022778,
                                    77.4920521110627
                            ],
                            [
                                    25.3147307089691,
                                    77.4294610599561
                            ],
                            [
                                    25.3021385331792,
                                    77.36639
                            ],
                            [
                                    25.3147307089691,
                                    77.3033189400439
                            ],
                            [
                                    25.3524114022778,
                                    77.2407278889373
                            ],
                            [
                                    25.4148938402768,
                                    77.1790932023729
                            ],
                            [
                                    25.5017024938852,
                                    77.1188839575312
                            ],
                            [
                                    25.6121766968325,
                                    77.060558383121
                            ],
                            [
                                    25.745475673728,
                                    77.0045603719814
                            ],
                            [
                                    25.9005849388695,
                                    76.9513161027897
                            ],
                            [
                                    26.0763240170917,
                                    76.9012307965843
                            ],
                            [
                                    26.2713554278953,
                                    76.8546856327877
                            ],
                            [
                                    26.4841948644807,
                                    76.8120348482011
                            ],
                            [
                                    26.7132224902197,
                                    76.7736030410485
                            ],
                            [
                                    26.9566952665896,
                                    76.739682700588
                            ],
                            [
                                    27.2127602187495,
                                    76.7105319810923
                            ],
                            [
                                    27.4794685377971,
                                    76.6863727371384
                            ],
                            [
                                    27.7547904123816,
                                    76.6673888351606
                            ],
                            [
                                    28.0366304767935,
                                    76.6537247541155
                            ],
                            [
                                    28.322843757963,
                                    76.6454844859107
                            ],
                            [
                                    28.611252,
                                    76.6427307439628
                            ],
                            [
                                    28.899660242037,
                                    76.6454844859107
                            ],
                            [
                                    29.1858735232065,
                                    76.6537247541155
                            ],
                            [
                                    29.4677135876185,
                                    76.6673888351606
                            ],
                            [
                                    29.743035462203,
                                    76.6863727371384
                            ],
                            [
                                    30.0097437812505,
                                    76.7105319810923
                            ],
                            [
                                    30.2658087334104,
                                    76.739682700588
                            ],
                            [
                                    30.5092815097803,
                                    76.7736030410485
                            ],
                            [
                                    30.7383091355193,
                                    76.8120348482011
                            ],
                            [
                                    30.9511485721047,
                                    76.8546856327877
                            ],
                            [
                                    31.1461799829083,
                                    76.9012307965843
                            ],
                            [
                                    31.3219190611305,
                                    76.9513161027897
                            ],
                            [
                                    31.477028326272,
                                    77.0045603719814
                            ],
                            [
                                    31.6103273031675,
                                    77.060558383121
                            ],
                            [
                                    31.7208015061148,
                                    77.1188839575312
                            ],
                            [
                                    31.8076101597232,
                                    77.1790932023729
                            ],
                            [
                                    31.8700925977222,
                                    77.2407278889373
                            ],
                            [
                                    31.9077732910309,
                                    77.3033189400439
                            ],
                            [
                                    31.9203654668208,
                                    77.36639
                            ]
                    ]
            ]
    }
}

谢谢..

1 个答案:

答案 0 :(得分:1)

根据外观,您需要geoIntersects operator

考虑以下

//Add setup data
db.polygon.insert({ "_id" : "Poly1", "shape" : { "type" : "Polygon", "coordinates" : [  [  [  0, 0 ],  [  3,  0 ],  [  3,  3 ],  [  0, 3 ],  [  0,  0 ] ] ] } })
db.polygon.insert({ "_id" : "Poly2", "shape" : { "type" : "Polygon", "coordinates" : [  [  [  3, 0 ],  [  6,  0 ],  [  6, 3 ],  [  3,  3 ],  [  3,  0 ] ] ] } })
//Create a 2dSphere index
db.polygon.ensureIndex({shape:"2dsphere"})

然后我可以运行以下查询:

db.polygon.find( { shape : { $geoIntersects : { $geometry : { type : "Point", coordinates: [ 3, 1 ] } } } } )

这将返回包含点3,1的所有多边形,包括仅边缘匹配的多边形(在这种情况下,两个文档都具有边缘运行3,0< - > 3,3)