MongoDB中空间数据的存储和操作
本文使用官方C# Driver,实现在MongoDB中存储,查询空间数据(矢量)
- //打开MongoDB的Collection
- MongoDatabase db = server.GetDatabase("aa");
- MongoCollection colSheng = db.GetCollection("sheng");
- //使用Ogr库打开Shapefile文件
- DataSource ds = Ogr.Open(@"c:\temp\sheng.shp", 0);
- Layer lyr = ds.GetLayerByIndex(0);
- //读取要素数量和字段数量
- int feaCount = lyr.GetFeatureCount(0);
- int fieldCount = lyr.GetLayerDefn().GetFieldCount();
- //读取所有字段名
- List<string> fieldNames =new List<string>();
- for (int i = 0; i < fieldCount; i++)
- {
- fieldNames.Add(lyr.GetLayerDefn().GetFieldDefn(i).GetName());
- }
- //循环将所有要素添加到MongoDB中
- for (int i = 0; i < feaCount; i++)
- {
- //使用Ogr库将矢量要素的空间信息转成Json格式
- Feature fea = lyr.GetFeature(i);
- Geometry geo = fea.GetGeometryRef();
- string json = geo.ExportToJson(null);
- BsonDocument doc = new BsonDocument();
- //将Json格式的空间信息存到Collection中
- //BsonValue bs = BsonValue.Create(json); //这种方法是不可以的,添加到库里之后无法使用空间查询语句查询
- BsonValue bs2 = BsonDocument.Parse(json); //这种方法才是正确的
- //doc.Add(new BsonElement("geom", bs));
- doc.Add(new BsonElement("geo",bs2));
- //通过循环将所有字段的属性信息存入Collection中
- for (int j = 0; j < fieldCount; j++)
- {
- string tmpFieldVal = fea.GetFieldAsString(j);
- doc.Add(new BsonElement(fieldNames[j],tmpFieldVal));
- }
- var res = colSheng.Insert<BsonDocument>(doc);
- }
- > db.sheng.find().limit(1)
- { "_id" : ObjectId("5371bf4e1dbba31914224563"), "geo" : { "type" : "Polygon", "coordinates" : [ [ [ 89.8496, 14.093 ], [ 90.3933, 14.004 ], [ 90.2708, 13.4708 ], [ 89.7284, 13.5597 ], [ 89.8496, 14.093 ] ] ] }, "pyname" : "sx", "boxtype" : "inter", "date" : "2012/6/5 12:41:42" }
- //获取Collection
- MongoDatabase db = server.GetDatabase("aa");
- MongoCollection colSheng = db.GetCollection("sheng");
- //定义一个查询框或查询多边形
- var poly = GeoJson.Polygon<GeoJson2DCoordinates>(
- GeoJson.Position(100, 20),
- GeoJson.Position(110, 20),
- GeoJson.Position(110, 40),
- GeoJson.Position(100, 40),
- GeoJson.Position(100, 20));
- //以这个查询多边形为条件定义一条查询语句
- var queryFilter2 = Query.GeoIntersects("geo", poly);
- //进行查询,输出MongoCursor
- cur = colSheng.FindAs<BsonDocument>(queryFilter2).SetFields( "pyname", "date");
- //获取结果
- var res = cur.ToArray();
- for (int i = 0; i < res.Count(); i++)
- {
- BsonDocument tmpDoc = res.ElementAt(i);
- //do something you want
- }
- > db.test.find()
- { "_id" : ObjectId("535884771dbba31858ad2101"), "geo" : { "type" : "Polygon", "coordinates" : [ [ [ 96.722, 38.755 ], [ 97.3482, 38.6922 ], [ 97.1674, 38.0752 ], [ 96.5474, 38.1383 ], [ 96.722, 38.755 ] ] ] } }
使用的查询语句
- > db.test.find({ "geo" : { "$geoIntersects" : { "$geometry" : { "type" : "Polygon", "coordinates" : [[[91.0, 33.0], [102.0, 33.0], [102.0, 38.0], [91.0, 38.0], [91.0, 33.0]]] } } } })
查询结果:
- { "_id" : ObjectId("535884771dbba31858ad2101"), "geo" : { "type" : "Polygon", "coordinates" : [ [ [ 96.722, 38.755 ], [ 97.3482, 38.6922 ], [ 97.1674, 38.0752 ], [ 96.5474, 38.1383 ], [ 96.722, 38.755 ] ] ] } }
但可以看到,collection中只有一条记录,且该记录所有点的Y坐标均大于38.0,为什么查询结果里,这条记录与语句中的Box相交呢。。。很奇怪
因为有这样的问题,所以还不放心直接将空间查询用于实际应用,而是通过一种变通的方法进行简单的空间查询,测试后发现,可能是由于空间索引的问题,这种方式查询比自带的GeoIntersects方法要快
- //获取Collection
- MongoDatabase db = server.GetDatabase("aa");
- MongoCollection colsheng= db.GetCollection("sheng");
- //查询所有记录
- var cur = colsheng.FindAllAs<BsonDocument>();
- long totalCount = cur.Count();
- //遍历所有记录
- for (int i = 0; i <= totalCount/1000; i++)
- {
- if (i * 1000 >= totalCount) continue;
- int skip = i * 1000;
- var cur2 = cur.Clone<BsonDocument>().SetSkip(skip).SetLimit(1000);
- var lst = cur2.ToArray();
- for (int j = 0; j < lst.Count(); j++)
- {
- //获取一条记录对应的BsonDocument
- BsonDocument doc = lst[j];
- var id = doc["_id"]; //该记录对应的ID
- BsonDocument geo = doc["geo"].ToBsonDocument();
- string geostr = geo[1].ToString(); //该记录对应空间信息的Json字符串
- List<double> coords = GetCoordLstFromString(geostr); //解析Json串,获得所有点的坐标(这里子函数就省略了)
- double xmin = 181, xmax = -181, ymin = 91, ymax = -91; //四个边界值,由于图层为经纬度,所以初值设为这些值
- //计算最大最小值
- for (int k = 0; k < coords.Count; k++)
- {
- if (k % 2 == 0)
- {
- if (coords[k] < xmin) xmin = coords[k];
- if (coords[k] > xmax) xmax = coords[k];
- }
- else
- {
- if (coords[k] < ymin) ymin = coords[k];
- if (coords[k] > ymax) ymax = coords[k];
- }
- }
- //将最大最小值写入Collection
- var tmpQuery = Query.EQ("_id", id);
- var tmpUpdate = MongoDB.Driver.Builders.Update.Set("xmax", xmax);
- var tmpres = col02c.Update(tmpQuery, tmpUpdate);
- tmpUpdate = MongoDB.Driver.Builders.Update.Set("xmin", xmin);
- tmpres = col02c.Update(tmpQuery, tmpUpdate);
- tmpUpdate = MongoDB.Driver.Builders.Update.Set("ymax", ymax);
- tmpres = col02c.Update(tmpQuery, tmpUpdate);
- tmpUpdate = MongoDB.Driver.Builders.Update.Set("ymin", ymin);
- tmpres = col02c.Update(tmpQuery, tmpUpdate);
- }
- }
- //获取Collection
- MongoDatabase db = server.GetDatabase("aa");
- MongoCollection colSheng = db.GetCollection("zy02c");
- //第一步,通过四边界筛选,
- var query = Query.And(Query.GT("xmax", 91.0), Query.LT("xmin", 102.0), Query.GT("ymax", 33.0), Query.LT("ymin", 38.0));
- var cur = colSheng.FindAs<BsonDocument>(query);
- //定义第二空间运算时的条件多边形(Ogr格式的定义)
- Geometry queryGeoLR = new Geometry(wkbGeometryType.wkbLinearRing);
- queryGeoLR.AddPoint(91.0, 33.0,0);
- queryGeoLR.AddPoint(102.0, 33.0,0);
- queryGeoLR.AddPoint(102.0, 38.0,0);
- queryGeoLR.AddPoint(91.0, 38.0,0);
- queryGeoLR.AddPoint(91.0, 33.0,0);
- Geometry queryGeo = new Geometry(wkbGeometryType.wkbPolygon);
- queryGeo.AddGeometry(queryGeoLR);
- //循环查询到的结果
- var lst = cur.ToArray();
- for (int i = lst.Length-1; i >=0; i--)
- {
- //获取当前记录对应的BsonDocument
- BsonDocument doc = lst[i];
- var id = doc["_id"]; //当前记录的ID
- BsonDocument geo = doc["geo"].ToBsonDocument();
- string geostr = geo[1].ToString(); //当前记录对应空间信息的Json字符串
- //通过Json串获取坐标值,并生成对应的Geometry对象
- List<double> coords = GetCoordLstFromString(geostr);
- Geometry resGeoLR = new Geometry(wkbGeometryType.wkbLinearRing);
- for (int j = 0; j < coords.Count / 2; j++)
- {
- resGeoLR.AddPoint_2D(coords[j], coords[j + 1]);
- }
- resGeoLR.AddPoint_2D(coords[0], coords[1]);
- Geometry resGeo = new Geometry(wkbGeometryType.wkbPolygon);
- resGeo.AddGeometry(resGeoLR);
- //判断是该Geometry与条件多边形是否相交
- if (resGeo.Intersects(queryGeo))
- {
- //do something
- }
- }
MongoDB中空间数据的存储和操作的更多相关文章
- MongoDB中数组类型相关的操作
概述 在MongoDB的模式中,我们经常将一些数据存储到数组类型中,即我们常见的嵌套模式设计的一种实现方式.数组的这种设计实现方式在关系数据库中是没有或者说不常见的.所以,通过本文我们来梳理一下Mon ...
- 利用“海底捞算法”在MongoDB中优雅地存储一棵树
目前常见的树形结构数据库存储方案有以下四种,但是在处理无限深度.海量数据的树结构时,都存在一些问题: 1)Adjacency List(邻接表):每个节点仅记录父节点主键.优点是简单,缺点是访问子树需 ...
- MongoDB地理空间数据存储及检索
目录 1.存入地理数据 GeoJSON数据存入 1.Ponit 点数据 2.LineString 线数据(多段线) 3. Polygon 多边形数据 4.MultiPoint多点.MultiLineS ...
- 在MongoDB中使用JOIN操作
SQL与NoSQL最大的不同之一就是不支持JOIN,在传统的数据库中,SQL JOIN子句允许你使用普通的字段,在两个或者是更多表中的组合表中的每行数据.例如,如果你有表books和publisher ...
- php操作mongodb中的ISODate格式日期
mongodb 中数据记录的日期格式为"dateCreated" : ISODate("2011-12-20T07:22:50.836Z")经过翻阅php官网中 ...
- 【转载】MongoDB中的MapReduce 高级操作介绍
转载自残缺的孤独 1.概述 MongoDB中的MapReduce相当于关系数据库中的group by.使用MapReduce要实现两个函数Map和Reduce函数.Map函数调用emit(key,va ...
- MongoDB中的映射,限制记录和记录拼排序 文档的插入查询更新删除操作
映射 在 MongoDB 中,映射(Projection)指的是只选择文档中的必要数据,而非全部数据.如果文档有 5 个字段,而你只需要显示 3 个,则只需选择 3 个字段即可. find() 方法 ...
- Java POJO类直接存储在MongoDB中
记录Java POJO类直接存储在MongoDB中的策略. maven: <dependency> <groupId>org.mongodb</groupId> & ...
- 爬取豆瓣电影TOP 250的电影存储到mongodb中
爬取豆瓣电影TOP 250的电影存储到mongodb中 1.创建项目sp1 PS D:\scrapy> scrapy.exe startproject douban 2.创建一个爬虫 PS D: ...
随机推荐
- 使用EF操作Oracle数据库小计
1.建表 CREATE TABLE item.ORDERS( ORDERID ) CONSTRAINT PK_ORDERS PRIMARY KEY, ORDERNO ), STOREID ), STO ...
- Windows下安装配置爬虫工具Scrapy及爬虫环境
爬虫工具Scrapy在Mac和Linux环境下都相对好装,但是在Windows上总会碰到各种莫名其妙的问题.本文记录下Scrapy在Window上的安装过程. 本文是基于Python2.7及Windo ...
- 01_python_初始python
一.初始python python是一门解释型语言,弱类型语言 / python解释器最为常用的是cpython(官方) 弱类型语言: a = 1 a = 'alex' #说明变量a既可以是整 ...
- Power Designer将表字段注释转换为模型
选择工具——Execute Commands——Edit /Run Script 将代码粘贴到此处,然后执行.即成功加入注释 Option Explicit ValidationMode = True ...
- Python小白学习之路(二十三)—【生成器补充】
生成器的一些补充 接着下鸡蛋和吃包子! 补充一:生成器只能遍历一次 (总是把生成器比喻成母鸡下鸡蛋,需要一个下一个,首先是下出来的鸡蛋不能塞回母鸡肚子里,其次是一个母鸡一生只能下一定数量的鸡蛋,下完了 ...
- C# SaveFileDialog的用法
#region 保存对话框 private void ShowSaveFileDialog() { //string localFilePath, fileNameExt, newFileName, ...
- 服务器返回的http状态码
状态码 响应类别 原因短语 1XX 信息性状态码(Informational) 服务器正在处理请求 2XX 成功状态码(Success) 请求已正常处理完毕 3XX 重定向状态码(Redirectio ...
- 利用Warensoft Stock Service编写高频交易软件--客户端驱动接口说明
Warensoft Stock Service Api客户端接口说明 Warensoft Stock Service Api Client Reference 本项目客户端驱动源码已经发布到GitHu ...
- Install vsftpd on centos
安装vsftpd程序. sudo yum -y install vsftpd 启动ftp服务. sudo service vsftp start 添加ftp用户,并设置密码. sudo useradd ...
- setAttribute()、getAttribute()与ele[attr]与自定义属性
一.自定义属性设置 1.setAttrbute() var q=document.getElementById("q"); q.setAttribute("index&q ...