设为首页 加入收藏

TOP

MonogDB-索引(三)GIS
2014-11-24 00:04:55 来源: 作者: 【 】 浏览:9
Tags:MonogDB 索引 GIS

一 2d Index

如下图的平面左边系,在其中有四个点A,B,C,D

\



1.使用mongo插入四个点
C:\dev\bin\mongodb-2.0.2\bin]]
>
mongo
MongoDB shell version: 2.0.2
connecting 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的语法: \

将查询出的经纬度点在地图上显示的效果图 n   快逗' k  糙 ( m5 4 6  岫   ' [⒏   _砗      ^    囤   ⒏  # 欹   http://www.2cto.com/kf/qianduan/css/html#id2 GeoJSON对象介绍
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hadoop的HDFS文件系统 下一篇C3P0多数据源的死锁问题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: