Mongodb:我如何检查点是否包含在多边形中? [英] Mongodb: How can I check if a point is contained in a polygon?
问题描述
我有一系列点(纬度,经度)中的点的列表。我已经对这些数组做了一个索引,现在我想知道是否有一个点位于该多边形的内部。
MongoDB有可能吗?
我已经尝试过使用这些命令,但没有运气:
> polygonA = [[48.780809,2.307129],[48.781809,2.300129],[48.770809,2.317129]]
> db.contours.find({rings.ring:{$ within:{$ polygon:polygonA}}})
和
> db.runCommand({geoNear:contour,in:[2.307129,48.780809,],num:10});
我的数据结构是:
> db.contours.findOne({},{'rings':0})
{
_id:ObjectId(50364617d591ac166000c196),
foundfieldname:Name,
geometrytype:geometryPolygon,
attributes:{
Shape:Polygon,
Name:France,
:国家,
国家:法国,
区域:1162358716567.45
},
国家:法国,
戒指:{
戒指:[[12.32,43.54],...],
...
}
谢谢
一个点,它有一个地理位置索引定义,是这种情况吗?
你的问题意味着这实际上是一个多点标准(多键索引),这是不会工作。
http://www.mongodb.org/display/DOCS/Geospatial+Indexing/#GeospatialIndexing-BoundsQueries
正如你可以看到的那样,当你在一个多边形内搜索loc时,loc字段就是这样的(参见上面的链接以获得其他有效的例子):
{loc:[50,30]}
使用如下索引:
>也就是说,一个字段代表一个点,并且在其上定义了一个地理索引。如果你使用这样的字段 - 那么你的测试是否可以正常工作?
I've got a list of points of a area in an array of points (latitude, longitude). I 've made an index on these arrays and now I want to know if one point is inside that polygon.
Is it possible with MongoDB? I already tried with these commands but no luck:
> polygonA = [ [ 48.780809,2.307129],[ 48.781809,2.300129],[ 48.770809,2.317129]]
> db.contours.find({ "rings.ring" : { "$within" : { "$polygon" : polygonA } } })
and
> db.runCommand( { geoNear : "contours" , within : [2.307129,48.780809,], num : 10 } );
My data structure is:
> db.contours.findOne({},{'rings':0})
{
"_id" : ObjectId("50364617d591ac166000c196"),
"foundfieldname" : "Name",
"geometrytype" : "geometryPolygon",
"attributes" : {
"Shape" : "Polygon",
"Name" : "France",
"Type" : "Country",
"Country" : "France",
"Area" : "1162358716567.45"
},
"country" : "France",
"rings":{
"ring":[[12.32,43.54],...],
...
}
Thanks
This requires that rings.ring be a points and that it have a geo index defined on it, is that the case here?
Your question implies that this is in fact a list of multiple points with a standard index on it (multikey index), which is not going to work.
http://www.mongodb.org/display/DOCS/Geospatial+Indexing/#GeospatialIndexing-BoundsQueries
As you can see there, when you search for "loc" as a point inside a polygon, the "loc" field is something like this (see the link above for other valid examples):
{ loc : [ 50 , 30 ] }
With an index something like this:
db.places.ensureIndex( { loc : "2d" }
That is, a field representing a single point and with a geo index defined on it. If you use a field like that - does your testing then work?
这篇关于Mongodb:我如何检查点是否包含在多边形中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!