当前位置: 首页 > news >正文

湖北省建设银行纪委网站站长工具一区

湖北省建设银行纪委网站,站长工具一区,全国十大物联网平台公司,轻栈小程序官网Mongodb的空间索引 Mongodb数据库大家都非常熟悉,是一个基于分布式文件存储的开源数据库系统,在高负载的情况下,添加更多的节点,可以保证服务器性能,数据结构由键值(key>value)对组成。MongoDB 文档类似于 JSON 对…

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万条数据,每次查询只需零点几秒,搜索附近的位置信息是真的快而方便。

http://www.hkea.cn/news/529157/

相关文章:

  • 电商网站开发工作计划企业网络营销策划
  • 用wps网站栏目做树形结构图网页设计代码案例
  • 多媒体网站设计开发是指什么每日关键词搜索排行
  • 网站 seo正规网络公司关键词排名优化
  • 建立网站赚多少钱seo收录排名
  • 怎么做app网站seo学习网站
  • 广西建设职业技术学院官网免费的seo优化
  • 凡科网电脑版怎么做网站百度知道官网手机版
  • 贵卅省住房和城乡建设厅网站周口seo推广
  • 搭建flv视频网站seo工具查询
  • 企业展示网站 数据库设计模板自助建站
  • 房地产设计师上海seo网络优化
  • wordpress迁移打不开百度seo泛解析代发排名
  • 网站兼容性测试怎么做微信营销软件群发
  • wordpress如何设置内容页seo营销优化
  • 高端大气的网站制作南宁百度seo软件
  • 沙井营销型网站建设成人培训机构
  • 网站没有被百度收录搜索引擎排名优化公司
  • 手机网站转换小程序晋江怎么交换友情链接
  • 专业做网站的公司疫情放开最新消息今天
  • 不用写代码做网站软件长沙优化网站
  • o2o商城网站建设方案广告策划案优秀案例
  • 日照做网站的那家做的好百度网页链接
  • 建设云个人证件查询系统上海seo培训
  • 网站流量提供商杭州seo排名
  • 做装饰工程的在什么网站投标自建站
  • 地球人--一家只做信誉的网站帮忙推广的平台
  • 网站建设外包协议天津网站排名提升
  • 邯郸教育行业网站建设百度推广代理商查询
  • 政府网站有哪些网站seo最新优化方法