mongodb地理信息应用


1. 二维空间索引

     MongoDB支持二维空间索引,这是设计时考虑到基于位置的查询。例如找到离目标位置最近的N条记录并且可以有效地作为附加条件过滤。

     如果需要使用这种索引,应确定对象中存储的字段是子对象或数组,前两个元素为XY坐标。

在文件中,存储的地理位置结构为:

{ loc : [ 50 , 30 ] }  

{ loc : { x : 50 , y : 30 } }  

{ loc : { lat : 40.739037, long: 73.992964 } }  

2d index:

使用2d index 能够将数据作为2维平面上的点存储起来, 在MongoDB 2.2以前 推荐使用2d index索引。
现在MongodDB 2.6了, 推荐使用2dspere index

2dsphere index:

2dsphere index 支持球体的查询和计算,同时它支持数据存储为GeoJSON 和传统坐标。


1.1. 创建二维地理位置索引 

创建二维地理位置索引,代码如下:  

db.places.ensureIndex( { loc : "2d" } )     //应该是固定格式  

     默认的,Mongo假设你索引的是经度/维度,因此配置了一个从-180180的取值范围,如果你想索引更多,可以指定该索引的范围:

db.places.ensureIndex( { loc : "2d" } , { min : -500 , max : 500 } )  

     上面的代码将衡量索引保证存入的值在-500500的范围之内。一般来说geo索引仅限于正方形以内且不包括边界以以外的范围,不能再边界上插入值,比如使用上面的代码,点(-500,-500)是不能被插入的。

每个Collection只能建立一个geospatial索引。

1.2. 索引

loc索引可以被用来精确匹配:

db.places.find( { loc : [50,50] } )  

另一种查询是找到目标点附近的点。

db.places.find( { loc : { $near : [50,50] } } )  

上面的一句将按离目标点(5050)距离最近的100个点(距离倒序排列),如果想指定返回的结果个数,可以使用limit()函数,若不指定,默认是返回100个。

指定返回20个点

每个表Collection只能建立一个geo索引

db.places.find( { loc : { $near : [50,50] } } ).limit(20)  


1.3. 组合索引

      Mongo空间索引可选的支持第二字段索引.如果想用坐标和其他属性同时作为条件查询,把这个属性也一同索引,附带其他属性加入索引中可使查询更快

代码如下:

db.places.ensureIndex( { loc : "2d" , category : 1 } );  

db.places.find( { loc : { $near : [50,50] }, category : 'coffee' } );  

2.距离查询    

2.1. 距离范围内查询

虽然find()语法为查询的首选,Mongo也提供来了 geoNear 命令来执行相似的函数。geoNear命令有一个额外的好处是结果中返回距离目标点的距离,以及一些过滤信息。

示例:

返回10条距离点(5050)最近的记录,loc字段由该collection的空间索引自动检测后决定

> db.runCommand( { geoNear : "places" , near : [50,50], num : 10 } );  
> db.runCommand({geoNear:"asdf", near:[50,50]})  
{  
        "ns" : "test.places",  
        "near" : "1100110000001111110000001111110000001111110000001111",  
        "results" : [  
                {  
                        "dis" : 69.29646421910687,  
                        "obj" : {  
                                "_id" : ObjectId("4b8bd6b93b83c574d8760280"),  
                                "y" : [  
                                        1,  
                                        1  
                                ],  
                                "category" : "Coffee"  
                        }  
                },  
                {  
                        "dis" : 69.29646421910687,  
                        "obj" : {  
                                "_id" : ObjectId("4b8bd6b03b83c574d876027f"),  
                                "y" : [  
                                        1,  
                                        1  
                                ]  
                        }  
                }  
        ],  
        "stats" : {  
                "time" : 0,  
                "btreelocs" : 1,  
                "btreelocs" : 1,  
                "nscanned" : 2,  
                "nscanned" : 2,  
                "objectsLoaded" : 2,  
                "objectsLoaded" : 2,  
                "avgDistance" : 69.29646421910687  
        },  
        "ok" : 1  
}  


2.2. 距离和注释型条件查询     

代码如下:

> db.runCommand( { geoNear : "places" , near : [ 50 , 50 ], num : 10,  

... query : { type : "museum" } } );  

 

2.3. 几何范围内查询

    在v1.3.4版本以后,可以几何边界查询。

     $within 参数可以代替$near来查找一个形状之内结果。同时,也支持$box(矩形)$center(圆环)

    想要查找一个一个矩形之内所有的点,必须制定该矩形的左下角和右上角坐标:

Mongo代码  

> box = [[40, 40], [60, 60]]  

> db.places.find({"loc" : {"$within" : {"$box" : box}}})  

更多信息请参考

http://www.mongodb.org/display/DOCS/Geospatial+Indexing 

 

2.4. 查询操作符

查询操作符查看如下:

Name

Description

$geoWithin

Selects geometries within a bounding GeoJSON geometry.

$geoIntersects

Selects geometries that intersect with a GeoJSON geometry.

$near

Returns geospatial objects in proximity to a point.

$nearSphere

Returns geospatial objects in proximity to a point on a sphere.

Geometry Specifiers

Name

Description

$geometry

Specifies a geometry in GeoJSON format to geospatial query operators.

$maxDistance

Specifies a distance to limit the results of $near and $nearSphere queries.

$center

Specifies a circle using legacy coordinate pairs to $geoWithin queries when using planar geometry.

$centerSphere

Specifies a circle using either legacy coordinate pairs or GeoJSON format

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值