当前位置: 首页 > 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.zhongyajixie.com/news/15982.html

相关文章:

  • 有没有什么网站专门帮人做问卷ciliba磁力搜索引擎
  • 店铺销售系统软件哪个好seo网站监测
  • 做个外贸网站设计app拉新推广平台
  • 网站系统建设汇报搜索引擎优化seo培训
  • 盐城网站优化价格搜索引擎seo关键词优化方法
  • 青岛注册公司在哪个网站申请宁波网站推广网站优化
  • 网站租用服务器费用深圳市网络品牌推广
  • 昆明建设招投标网站网络营销的现状及问题
  • 中英文网站开发公司seo百度网站排名研究中心关键词首页优化
  • 微网站建设第一步是进行什么的设置seo优化网站的注意事项
  • 网站开发 估算 excel营销策划方案ppt模板
  • 你去湖北省住房城乡建设厅网站查sem推广
  • 域名查询网站黄页
  • 崇州市城乡建设局网站sem营销推广
  • 软件制作过程优化营商环境建议
  • 百度建设网站优化网站页面
  • 国内设计网站公司网站四川游戏seo整站优化
  • 邻水建设局网站上海百度推广优化公司
  • 做网站哪里好网页设计模板html代码
  • 软件工程考研方向优化工具箱下载
  • 长沙人才市场招聘网最新招聘会seo搜索是什么意思
  • 网站对图片优化谷歌google play下载
  • 具有品牌的上海网站建设竞价推广代运营企业
  • 北京网站设计公司youx成都柚米科技15北京搜索引擎推广服务
  • 安徽做网站找谁公众号怎么引流推广
  • 建设网站 教学反思营销模式方案
  • 遵义网上制作网站全网营销的公司
  • 做网站点击率赚钱吗网络媒体推广方案
  • 网站meta优化企业推广网
  • 做网站建设哪家效益快站长工具精品