MonogDB -索引 (三) GIS

来源:互联网 发布:淘宝确认收货后退运费 编辑:程序博客网 时间:2024/05/21 10:40

一 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对象介绍
0 0
原创粉丝点击