mongodb检查点是否在多边形中 [英] mongodb check if point is in polygon

查看:107
本文介绍了mongodb检查点是否在多边形中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mongo 2.6

我有一些存储的多边形.我有一点.我不知道该点是否适合任何存储的多边形

I have some amount of stored polygons. And I have a point. I what to know if this point fits any of stored polygons

文档示例

{ ..., "polygons" : [ [ 17.60083012593064, 78.18557739257812 ], [ 17.16834652544664, 78.19381713867188 ], [ 17.17490690610013, 78.739013671875 ], [ 17.613919673106714, 78.73489379882812 ] ], ... }

Mongodb中已经存在几乎相同的问题:检查点是否在存储的多边形内. 但这对我不起作用-此查询必须至少给出一个结果(示例中为一个)-但事实并非如此.

There is nearly the same question already Mongodb : Check if a point is inside a stored polygon. But it is not working for me - this query has to give at least one result(the one in example) - but it does not.

db.areas.find( { polygons : { $geoIntersects : { $geometry : {type:"Point",coordinates:[17.3734, 78.4738]} } } } )

实际上,如果我在任何多边形的边界上选择了一个点-确实如此.

Actually if I chose a point on a border of any polygon - it does.

$geoWithin方法必须按照mondodb文档所述进行工作.

$geoWithin method has to do the work as mondodb documentation says.

但是这些查询中的任何一个都不起作用

but any of these queries do not work

db.areas.find( { polygons : { $geoWithin : { $geometry : {type:"Point",coordinates:[17.3734, 78.4738]} } } } ) - not supported with provided geometry

db.tradeareas.find( { polygons : { $geoWithin : { $geometry : {type:"Polygon",coordinates: inside_polygon} } } } ) - BadValue bad geo query

似乎我错过了一些东西,但无法理解在什么地方.

It seems I miss something but cant understand what and where.

感谢您的帮助.

推荐答案

似乎与顺序有关.如果使用$ geoWithin并尝试在多边形内查找点,则其中的内容就是您要搜索的字段.但是,geoIntersects可以在任一方向上工作,因此您可以在多边形内或包含点的多边形中搜索点,例如,

It seems to be to do with the order. If you are using $geoWithin and you are trying to find points inside a polygon, the thing that is within is the field you are searching on. However, geoIntersects works in either direction, so you can search for points inside polygons, or polygons containing points, eg,

db.geom.insert({"polygons":
                      {"type":"Polygon",
                         coordinates:
                          [[[ 17.60083012593064, 78.18557739257812], 
                            [ 17.16834652544664, 78.19381713867188], 
                            [ 17.17490690610013, 78.739013671875], 
                            [ 17.613919673106714, 78.73489379882812],
                            [ 17.60083012593064, 78.18557739257812]
                          ]]
                      }
                 }); 

db.geom.find({polygons:
                 {$geoIntersects:
                     {$geometry:{ "type" : "Point",
                          "coordinates" : [ 17.3734, 78.4738 ] }
                      }
                  }
             });

此外,请注意,您需要在末端重复多边形的第一个点.如果删除最后一对,则会出现"$ err":无法规范化查询:BadValue错误的地理查询"错误.

Also, note that, you need to repeat the first point of the polygon at the end. If you remove the final pair, you will get a "$err" : "Can't canonicalize query: BadValue bad geo query" error.

似乎MongoDB允许您插入无效的几何图形,并且仅在您尝试添加2dsphere索引或执行相交/内/近查询时才抱怨,我想这是合理的,因为GeoJSON可以是有效的JSON,而不会成为有效的几何图形.

It seems that MongoDB allows you to insert invalid geometries and only complains when you try and add a 2dsphere index or do an intersects/within/near query, which, I suppose is reasonable, as GeoJSON can be valid JSON without being a valid geometry.

这篇关于mongodb检查点是否在多边形中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆