如何获取包含Point的所有圈子? [英] How to get all circles that a Point is contained in?

查看:85
本文介绍了如何获取包含Point的所有圈子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以找出特定点位于哪些多边形(特别是圆形)中?

Is there a way to find out what polygons (specifically circles) a specific Point lies in?

在这种情况下,我将存储一个包含圆的文档,如下所示,我将在一个点的纬度和经度中传递,并想获取该点在给定圆内的所有文档.

In this case I would have stored a documents containing circles, like below, I would pass in a latitude and longitude for a point, and would like to get back all documents where the point is within the given circle.

{
  "_id" : ObjectId("53e3e85ce4b0c2e8227a1dad"),
  "name" : "Menlo College",
  "location" : [-122.1928, 37.45632],
  "radius" : NumberLong(215),
},
{
  "_id" : ObjectId("53e43d19e4b0aeabcb3d3f9d"),
  "name" : "West Valley College",
  "location" : [-122.01021194458008, 37.263226547586207],
  "radius" : NumberLong(604),
}

如果这不可能,那么至少对于其他GeoJSON形状是否可行?到目前为止,我发现的所有内容都表明逆是可能的(找到像一个圆内的所有点),但在这种情况下什么也没有.

If this is not possible, then is it at least possible with other GeoJSON shapes? Everything I've found so far indicates that the inverse is possible (find all points which like inside a circle), but nothing for this scenario.

谢谢

推荐答案

可以使用MongoDB的$geoIntersects地理空间查询运算符.

It is possible using MongoDB's $geoIntersects Geospatial query operator.

因此,如果您有一个GeoJson多边形集合,并且想找出与给定点相交的所有多边形,则需要运行以下命令:

So, if you have a collection of GeoJson polygons and you want to find out all the polygons that intersect with your given point, then you need to run the following:

db.places.find( { <locationFieldOfYourDocuments> :
                  { $geoIntersects :
                    { $geometry :
                      { type : "Point" ,
                        coordinates: [long, lat]
                } } } } )

在上面的命令中,loc是每个文档的属性,其中包含GeoJson多边形的坐标.另外,请确保您在<locationFieldOfYourDocuments>上具有2dsphere索引.

In the command above, loc is that attribute of each document that contains the coordinates for GeoJson polygon. Also, make sure that you have 2dsphere index over <locationFieldOfYourDocuments>.

现在,要解决您的原始问题,我将使用一些javascript.可能有更好的解决方案,但据我所知.

Now, to get your original problem solved I will use a little bit of javascript. There may be better solutions but not in my knowledge.

假设您的所有圈子都存储在Circles集合中.我将查询该集合并逐个获取每个圆,然后与另一个包含单个点的集合进行相交,该点将是您要查询的与圆是否相交的点.因此,将点存储在SinglePoint集合中.

Let's say all your circles are stored in Circles collection. I would query that collection and fetch each circle one by one and then perform an intersect with another collection that would contain a single point which would be the one you wanted to query if it intersects with the circles or not. So let the point be stored in SinglePoint collection.

脚本看起来像...

db.Intersections.remove({}); // emptying the output collection
var circleCursor = db.Circles.find();
while (circleCursor.hasNext()) {
    var circle = circleCursor.next();
    var coord = circle.location;
    var radiusInRadians = circle.radius * conversionFactorForRadius;
    var intersect = db.SinglePoint.find({loc :
                                         { $geoWithin :
                                           {$centerSphere : [coord], radiusInRadians}
                                         }});
    if (intersect.hasNext()) {db.Intersections.add(circle)} // this will add all intersecting circles to Intersections collection
}

所有您需要做的就是将该脚本保存在文件(myScript.js)中并进行调用:

All you have to do is save this script in a file (myScript.js) and make a call:

mongo DBName pathTomyScript.js

这会将与输入点相交的所有圆存储在相交"集合中.以上所有集合都应该在DBName数据库中.

This will store all the circles that intersect with your input point in the Intersects collection. All the above collections should be in DBName database.

这篇关于如何获取包含Point的所有圈子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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