是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