MonogDB -索引 (三) GIS

來源:互聯網
上載者:User

一 2d Index如的平面左邊系,在其中有四個點A,B,C,D 

1.使用mongo插入四個點

C:\dev\bin\mongodb-2.0.2\bin]]>mongoMongoDB shell version: 2.0.2connecting to: test
> db.createCollection("location"){ "ok" : 1 }
> db.location.save( {_id: "A", position: [0.001, -0.002]} )
> db.location.save( {_id: "B", position: [1.0, 1.0]} )
> db.location.save( {_id: "C", position: [0.5, 0.5]} )
> db.location.save( {_id: "D", position: [-0.5, -0.5]} )
2.建立2d索引> db.location.ensureIndex( {position: "2d"} )

3.執行查詢
查詢距離圓心0.75範圍內的點(毗鄰關係)
> db.location.find( {position: 
                        { $near: [0,0], $maxDistance: 0.75  } 
                     } )
{ "_id" : "A", "position" : [ 0.001, -0.002 ] }
{ "_id" : "D", "position" : [ -0.5, -0.5 ] }
{ "_id" : "C", "position" : [ 0.5, 0.5 ] }

查詢位於矩形[0.25,0.25],[1.0,1.0]為頂點的矩形內的點(內含項目關聯性)
> db.location.find( {position:
                       { $within: 
                          { $box: [ [0.25, 0.25], [1.0,1.0] ] } 
                       } 
                     } ) 
{ "_id" : "C", "position" : [ 0.5, 0.5 ] }
{ "_id" : "B", "position" : [ 1, 1 ] }

二  2sphere Index
1.準備資料 資料格式為GeoJSON 對象 
> db.baidugis.find(){ "_id" : ObjectId("528ecd42b8ff14e242ac3129"), "id" : 1, "loc" : { "type" : "Point", "coordinates" : [  73.5,  39.31 ] }, "xoffset" : 0.009962485610003569, "yoffset" : 0.005924861040000451 }{ "_id" : ObjectId("528ecd42b8ff14e242ac312a"), "id" : 2, "loc" : { "type" : "Point", "coordinates" : [  73.5,  39.32 ] }, "xoffset" : 0.009956379369995716, "yoffset" : 0.005926044680002462 }{ "_id" : ObjectId("528ecd42b8ff14e242ac312b"), "id" : 3, "loc" : { "type" : "Point", "coordinates" : [  73.5,  39.33 ] }, "xoffset" : 0.009954659179996384, "yoffset" : 0.005929157739998914 }{ "_id" : ObjectId("528ecd42b8ff14e242ac312c"), "id" : 4, "loc" : { "type" : "Point", "coordinates" : [  73.5,  39.34 ] }, "xoffset" : 0.009957397069996432, "yoffset" : 0.00593507193999443 }
2.建立2sphere Index> db.baidugis.ensureIndex({loc:”2dsphere"})注意:2dphere索引可以是一個複合索引,而且也不要求位置欄位為第一索引 

3.查看索引> db.baidugis.getIndexes()[     {          "v" : 1,          "key" : {               "_id" : 1          },          "ns" : "GISNew.baidugis",          "name" : "_id_"     },     {          "v" : 1,          "key" : {               "loc" : "2dsphere"          },          "ns" : "GISNew.baidugis",          "name" : "loc_2dsphere"     }] 

4.查詢操作在糾偏資料庫中分別查詢以經緯度[116.86,40.40]為中心,最大距離為20公裡、200公裡和2000公裡的點的個數> db.baidugis.find( {loc:                                {$near:                                          {$geometry:                                                  {type:"Point", coordinates:  [116.86,40.40]},                                                                           $maxDistance:20000                                          }                                 }                              }).count() 1325
> db.baidugis.find({loc:{$near:{$geometry:{type:"Point", coordinates:[116.86,40.40]},$maxDistance:200000}}}).count() 133158
> db.baidugis.find({loc:{$near:{$geometry:{type:"Point", coordinates:[116.86,40.40]},$maxDistance:2000000}}}).count() 6239311 


上面使用的$near的文法:

將查詢出的經緯度點在地圖上顯示的

三 總結          通過在實際項目中使用MongoDB的地理位置索引發現其效果還是很不錯的,將查詢出的結果顯示在地圖上後基本是一個圓形,例如的顯示效果,說明其計算是精確的。        其查詢效能還是可以接受的,在大概擁有一千萬個點的糾偏資料庫中,單機情況下,查詢20公裡範圍內的點的速度大概是1-2秒,查詢200公裡的點大概需要6-10秒,查詢2000公裡(小半個中國)的點大概需要二十分鐘。

 參考:     http://docs.mongodb.org/manual/tutorial/query-a-2dsphere-index/  GIS查詢方法     http://geojson.org/geojson-spec.html#id2     GeoJSON對象介紹

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.