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

查看:38
本文介绍了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:

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:

无法规范化查询:BadValue 错误地理查询"错误.

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天全站免登陆