【问题标题】:MongoDB find polygon coordinates using single point and radiusMongoDB使用单点和半径查找多边形坐标
【发布时间】:2014-06-16 12:49:46
【问题描述】:

我有一个包含许多多边形坐标的集合,它们代表不同的区域。 我的目标是我将在 mongodb 查询中发送纬度、经度和半径,该查询应该返回落在圆形区域内的多边形坐标。

多边形坐标:

{
    "_id" : "300",
    "name" : "MeKesler",
    "centroid" : [ 
        0, 
        0
    ],
    "type" : "cnbd_id",
    "coords" : [ 
        [ 
            39.8620784017, 
            -86.14614844330004
        ], 
        [ 
            39.8625395793, 
            -86.15442037579999
        ], 
        [ 
            39.8593030353, 
            -86.156373024
        ], 
        [ 
            39.8586935926, 
            -86.15669488909998
        ], 
        [ 
            39.8534225112, 
            -86.15854024890001
        ], 
        [ 
            39.850391456, 
            -86.1589050293
        ], 
        [ 
            39.8511657057, 
            -86.1479830742
        ], 
        [ 
            39.8511986523, 
            -86.14598751070002
        ], 
        [ 
            39.856881704, 
            -86.14605188370001
        ], 
        [ 
            39.8575241063, 
            -86.14605188370001
        ], 
        [ 
            39.8620784017, 
            -86.14614844330004
        ]
    ]
}

我的查询:

db.collection.find({
    "coords" : {
        "$within" : { 
            "$center" : [ 
                [ 39.863110, -86.168456], 
                2/69.1
            ]
        }
    }
}) 

有人可以帮忙吗?

【问题讨论】:

  • 我不认识您的多边形定义的语法。它应该是geojson吗? “cnbd_id”是什么意思?
  • 这是我的整个文档,它是一个 geoJson。 “cnbd_id”是其中一种类型。 “coords”是一个包含所有多边形坐标的数组。

标签: mongodb


【解决方案1】:

我认为问题在于您的文档不是有效的 geojson。您的多边形的有效语法是

{
    "_id": "300",
    "name": "MeKesler",
    "centroid": {
        type: "Point",
        coordinates: [0, 0]
    },
    "type": "cnbd_id",

    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [39.8620784017, -86.14614844330004],
                [39.8625395793, -86.15442037579999],
                [39.8593030353, -86.156373024],
                [39.8586935926, -86.15669488909998],
                [39.8534225112, -86.15854024890001],
                [39.850391456, -86.1589050293],
                [39.8511657057, -86.1479830742],
                [39.8511986523, -86.14598751070002],
                [39.856881704, -86.14605188370001],
                [39.8575241063, -86.14605188370001],
                [39.8620784017, -86.14614844330004]
            ]
        ]
    }
}

另外,您应该考虑在几何字段上创建2dsphere index

最后,地理查询应使用$geoWithin 运算符,并针对多边形而不是其坐标运行。

db.collection.find({
    "geometry" : {
        "$geoWithin" : { 
            "$center" : [ 
                [ 39.863110, -86.168456], 
                2/69.1
            ]
        }
    }
}) 

【讨论】:

  • 我发布的文档是我开发的。谢谢你的好意回复。我会试试你的脚本并回复你。
  • 如果您将其视为旧坐标对,也许您仍然可以使用您拥有的数据。您仍然需要在 coord 字段上构建 2d 索引(而不是 2dsphere 索引)。见Geospatial Query Compatibility
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 2018-11-02
  • 2019-12-18
  • 2011-05-31
相关资源
最近更新 更多