设为首页 加入收藏

TOP

MongoDB中空间数据的存储和操作(四)
2014-11-24 03:23:46 来源: 作者: 【 】 浏览:5
Tags:MongoDB 空间 数据 存储 操作
是2.6.0版本)

具体信息如下(在命令行中操作):

Collection中的坐标

[csharp] view plaincopyprint 01.> db.test.find()
02.{ "_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()
{ "_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 ] ] ] } }

使用的查询语句

[csharp] view plaincopyprint 01.> 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]]] } } } })
> 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]]] } } } })

查询结果:

[csharp] view plaincopyprint 01.{ "_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 ] ] ] } }
{ "_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方法要快

大致思路为:为每一条记录均生成一个最小外接矩形,得到其xmax,xmin,ymax,ymin四个边界值,用数值的形式保存至Collection中,每次进行空间查询时,首先通过最小外接矩形进行一次筛选,判断这些最小外接矩形与查询语句中多边形的最小外接矩形之间的关系,如果相交,那么进行第二步判断,通过Ogr组件判断实际的多边形是否相交,返回最后结果

首先是生成最小外接矩形的代码:

[csharp] view plaincopyprint 01.//获取Collection
02. MongoDatabase db = server.GetDatabase("aa");
03. MongoCollection colsheng= db.GetCollection("sheng");
04. //查询所有记录
05. var cur = colsheng.FindAllAs();
06. long totalCount = cur.Count();
07. //遍历所有记录
08. for (int i = 0; i <= totalCount/1000; i++)
09. {
10. if (i * 1000 >= totalCount) continue;
11. int skip = i * 1000;
12. var cur2 = cur.Clone().SetSkip(skip).SetLimit(1000);
13. var lst = cur2.ToArray();
14. for (int j = 0; j < lst.Count(); j++)
15. {
16. //获取一条记录对应的BsonDocument
17. BsonDocument doc = lst[j];
18. var id = doc["_id"]; //该记录对应的ID
19. BsonDocument geo = doc["geo"].ToBsonDocument();
20. string geostr = geo[1].ToString(); //该记录对应空间信息的Json字符串
21. List coords = GetCoordLstFromString(geostr); //解析Json串,获得所有点的坐标(这里子函数就省略了)
22. double xmin = 181, xmax = -181, ymin = 91, ymax = -91; //四个边界值,由于图层为经纬度,所以初值设为这些值
23. //计算最大最小值
24. for (int k = 0; k < coords.Count; k++)
25. {
26. if (k % 2 == 0)
27. {
28. if (coords[k] < xmin) xmin = coords[k];
29. if (coords[k] > xmax) xmax = coords[k];
30. }
31

首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇如何删除表中的重复记录只保留其.. 下一篇SQLite学习手册(表达式)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·用 C 语言或者限制使 (2025-12-25 08:50:05)
·C++构造shared_ptr为 (2025-12-25 08:50:01)
·既然引用计数在做 GC (2025-12-25 08:49:59)
·Java 编程和 c 语言 (2025-12-25 08:19:48)
·. net内存管理宝典这 (2025-12-25 08:19:46)