Mongodb空间索引的使用以及与Django的对接

Mongodb的空间索引

Mongodb数据库大家都非常熟悉,是一个基于分布式文件存储的开源数据库系统,在高负载的情况下,添加更多的节点,可以保证服务器性能,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。对与Mongodb还有一个非常重要的功能那就是它的空间索引,一般存储每个地点的经纬度的坐标, 如果要查询附近的场所,则需要建立索引来提升查询效率。 Mongodb专门针对这种查询建立了地理空间索引:2d和2dsphere索引

1.首先安装Mongodb数据库,在此不再赘述

在开始教程之前呢,先介绍一下Mongodb空间索引的查询器以及查询参数,如下所示:

#查询器
$geoWithin       Selects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d 	  					indexes support $geoWithin. replaces $within which is deprecated.

$geoIntersects      Selects geometries that intersect with a GeoJSON geometry. The 2dsphere index 				     supports $geoIntersects.

$near          Returns geospatial objects in proximity to a point. Requires a geospatial 						index. The 2dsphere and 2d indexes support $near.

$nearSphere        Returns geospatial objects in proximity to a point on a sphere. Requires a 						geospatial index. The 2dsphere and 2d indexes support $nearSphere.
#查询参数
$geometry	    Specifies a geometry in GeoJSON format to geospatial query operators.

$minDistance	Specifies a minimum distance to limit the results of $near and $nearSphere 		 				queries. For use with 2dsphere index only.

$maxDistance	 Specifies a maximum distance to limit the results of $near and $nearSphere 					queries. The 2dsphereand 2d indexes support $maxDistance.

$center			Specifies a circle using legacy coordinate pairs to $geoWithin queries when using 				  planar geometry. The 2d index supports $center.

$centerSphere	 Specifies a circle using either legacy coordinate pairs or GeoJSON format for
				$geoWithin queries when using spherical geometry. The 2dsphere and 2d indexes 					support$centerSphere.
    
$box	         Specifies a rectangular box using legacy coordinate pairs for $geoWithin queries. 				   The 2d index supports $box.

$polygon	     Specifies a polygon to using legacy coordinate pairs for $geoWithin queries. The 				  2d index supports $center.

$uniqueDocs	     Deprecated. Modifies a $geoWithin and $near queries to ensure that even if a         			  document matches the query multiple times, the query returns the document once.

不知道什么意思,没关系,下面开始讲解!

2.2dsphere索引

2dsphere索引是MongoDB最常用的地理空间索引之一,用于地球表面类型的地图。允许使用GeoJSON格式指定点、线、多边形。 点可以用形如[longitude,latitude]([经度,纬度])的两个元素的数组表示("locations"字段的名字可以是任意的,但是其中的子对象是有GeoJSON指定的,不能改变),存储的数据格式如下:

#点状数据
{"coorname" : "蘑菇石", 
    "locations" : {
        "type" : "Point", 
        "coordinates" : [
            108.693809,27.912161
        ]
    }, 
    "types" : "标志性建筑物
    }

#线状数据可以由点组成的数组来表示
{
    "name":"changjiang",
    "locations":{
        "type":"Line",
        "coordinates":[[108.693809,27.912161],[108.693809,27.912161],[108.693809,27.912161]]
    },
    "types" : "标志性建筑物
}

#同样多边形也时用点数组表示,不同的是type的类型
{
    "name":"changjiang",
    "locations":{
        "type":"Polygon",
        "coordinates":[[108.693809,27.912161],[108.693809,27.912161],[108.693809,27.912161]]
    },
    "types" : "标志性建筑物
}

#注意:locations字段里面的key是固定的,不要修改,否则空间索引无法添加

数据添加好之后,就要建立空间索引了:

#1.使用Mongodb命令添加
db.Periphery_basic.ensureIndex({"locations":"2dsphere"})

#2.使用django的ORM添加索引
#在setting中配置mongodb数据库
from mongoengine import connect
CONN = connect('globalmap').geo_example
#创建表
class Periphery_basic(mongoengine.Document):
    coorname = mongoengine.StringField()
    locations = mongoengine.DictField()
    types = mongoengine.StringField()
#添加完数据创建索引
Periphery_basic.create_index([("locations","2dsphere")])

在Mongodb数据库中产看添加成功没有

#查看索引
db.getCollection('Periphery_basic').getIndexes()

#删除集合所有索引
db.getCollection('Periphery_basic').dropIndexes()

#删除集合指定索引
db.getCollection('Periphery_basic').dropIndex('索引名')

3.2D索引

2d索引也是MongoDB最常用的地理空间索引之一,用于游戏地图。2d索引用于扁平表面,而不是球体表面。如果用在球体表面上,在极点附近会出现大量的扭曲变形(一句话就是说2D索引是平面的,2dsphere索引是球面的)

依然用上面的数据格式,添加完之后创建索引

db.Periphery_basic.ensureIndex({"locations.coordinates":"2d"})  #2d索引是要精确到经纬度字段的

#django内创建
Periphery_basic.create_index([("locations.coordinates","2d")])

4.查询

geoWithIn查询, 查询多边形范围内的点 (适用于两种索引)

#命令查询
db.Periphery_basic.find({locations: {$geoWithin: {$geometry: {type : "Polygon" ,coordinates: [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ]]]}}}})

#Django查询
Periphery_basic.objects(locations={"$geoWithin": {"$geometry": {"type": "Polygon", "coordinates": [[[0, 0], [3, 6], [6, 1], [0, 0]]]}}})

#大于单个半球的查询, 需要加入crs
db.Periphery_basic.find({locations: {$geoWithin: {$geometry: {type : "Polygon" ,coordinates: [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ]]],crs: {type: "name",properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326"}}}}}})

Periphery_basic.objects(locations={"$geoWithin": {"$geometry": {"type": "Polygon", "coordinates": [[[0, 0], [3, 6], [6, 1], [0, 0]]],crs: {"type": "name",properties: {"name": "urn:x-mongodb:crs:strictwinding:EPSG:4326"}}}}})

geoIntersects, 图形查询, 交集 (2dsphere索引支持)

#命令查询
db.Periphery_basic.find({locations: {$geoIntersects: {$geometry: {type : "Polygon" ,coordinates: [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ]]]}}}})
#django查询
Periphery_basic.objects(locations={"$geoIntersects": {"$geometry": {"type": "Polygon", "coordinates": [[[0, 0], [3, 6], [6, 1], [0, 0]]]}}})

#大于单个半球的查询, 需要加入crs
db.Periphery_basic.find({locations: {$geoIntersects: {$geometry: {type : "Polygon" ,coordinates: [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ]]],crs: {type: "name",properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326"}}}}}})

Periphery_basic.objects(locations={"$geoIntersects": {"$geometry": {"type": "Polygon", "coordinates": [[[0, 0], [3, 6], [6, 1], [0, 0]]],crs: {"type": "name","properties": {"name": "urn:x-mongodb:crs:strictwinding:EPSG:4326"}}}}})

$near, 由近道原返回文档的点, 经纬度罗列方式为 [ lng, lat ] (两种索引都支持)

#命令查询
db.Periphery_basic.find({locations:{$near:{$geometry: {type: "Point", coordinates: [120.665283,31.317678]},$minDistance: 1000,$maxDistance: 5000}}})
#django查询
Periphery_basic.objects(locations={"$near":{"$geometry": {"type": "Point", "coordinates": [120.665283,31.317678]},"$minDistance": 1000,"$maxDistance": 5000}})

#传统坐标查询
db.Periphery_basic.find(
   { location : { $near : [120.665283,31.317678], $maxDistance: 10 } }
)

$nearSphere, 空间距离查询 (两种索引都支持)

#命令查询
db.Periphery_basic.find({locations:{$nearSphere:{$geometry: {type: "Point", coordinates: [120.665283,31.317678]},$minDistance: 1000,$maxDistance: 5000}}})
#django查询
Periphery_basic.objects(locations={"$nearSphere":{"$geometry": {"type": "Point", "coordinates": [120.665283,31.317678]},"$minDistance": 1000,"$maxDistance": 5000}})

最大距离内查询 (两种索引都支持)

db.Periphery_basic.find({
   locations: {$nearSphere: [120.665283,31.317678],$maxDistance: 10 }
} )

$center查询, 圆形查询 (2d索引支持)

#平面10公里
db.Periphery_basic.find(
   {locations: { $geoWithin: { $center: [ [120.665283,31.317678], 10 ] } } }
)
#django查询
Periphery_basic.objects(
   locations={"$geoWithin": {"$center": [ [120.665283,31.317678], 10 ] } } 
)

$centerSphere 查询, 球形查询 (两种索引都支持)

#需要把查询的半径转化为弧度
#命令行查询
db.Periphery_basic.find( {
  locations: { $geoWithin: {$centerSphere: [ [ 120.665283,31.317678 ], 3/3963.2 ] } }
} )
#django查询
Village_basic.objects(locations={"$geoWithin": {"$centerSphere": [[ 120.665283,31.317678 ], 3 / 3963.2]}})

$box查询, 先精度后纬度, first lower then upper (2d索引支持)

db.Periphery_basic.find({
   locations: { $geoWithin: {$box:  [ [ 0, 0 ], [120.665283,31.317678]]}}})

$polygon, 多边形查询 (两种索引都支持)

db.Periphery_basic.find({locations: {$geoWithin: { $polygon: [[120.665284,31.317675], [120.665245,31.317612],[120.665265,31.317631]]}}})

小结:

数据量越来越多的情况下,要想找到合适的坐标并不容易,建立空间索引之后,数据库自动会按照地理标准进行检索,速度上是非常快的,目前库中20万条数据,每次查询只需零点几秒,搜索附近的位置信息是真的快而方便。

你可能感兴趣的:(GIS,mongodb,django,数据库,python)