我有一个像这样的收藏品:
[
{
"roadname": "foo",
"data": [
{
"val": 50,
"loc": {
"type": "Point",
"coordinates": [3.197033554, 50.64611712]
}
}
},
{
"val": NULL,
"loc": {
"type": "Point",
"coordinates": [3.197740735, 50.6460058]
}
}
}
]
},
{
"roadname": "foo",
"data": [
{
"val": 50,
"loc": {
"type": "Point",
"coordinates": [3.32456512, 50.2744516]
}
}
}
]
},
{
"roadname": "bar",
"data": [
{
"val": 145,
"loc": {
"type": "Point",
"coordinates": [3.198408689, 50.64586985]
}
}
}
]
}
]我在地图上显示了每一个data.loc,这就引出了以下问题:

。(点颜色表示val字段)
EDIT3:来澄清我的数据库结构,这里是精确数据库的表示。每条灰色线表示提供的数据集中的根元素:

我希望“接近的组点(使用data.loc__),并且具有相同的父name”,并聚合它们的val (例如在average中使其简单),以便显示如下内容:

EDIT3: --重要的是要理解,我试图聚合的要点并不共享任何公共属性或祖先。它们的 ONLY 公共分母是它们的空间邻近
我知道near、geonear和group聚合,但我只是找不到解决方案。
我想使用纯mongodb解决方案。如果不可能,我也可以使用turf.js或其他库,但我只是在努力寻找一种可行的、可伸缩的方法来实现这一点。
编辑:集合中的主要根元素表示一条道路,因此道路上的所有点都有相同的父roadname。
EDIT2:数据可以找到这里
发布于 2016-11-30 16:29:53
我想这会对你有用的。我将您的dataset aggregatingpointsdata.json导入到名为roads的mongo集合中。
这就是一个集群在mongo中的样子。因此,每个文档都表示您提供的数据集中的群集或数组中的一个元素。要记住的一件事是,只有当一个点集群中有一些标识符应该组合在一起时,这才能起作用。在我的例子中,它是_id。
> db.roads.findOne()
{
"_id" : ObjectId("583ee50bd7c4d711d45c7757"),
"roadname" : "RD700",
"data" : [
{
"val" : null,
"loc" : {
"type" : "Point",
"coordinates" : [
3.197033554,
50.64611712
]
}
},
{
"val" : null,
"loc" : {
"type" : "Point",
"coordinates" : [
3.197740735,
50.6460058
]
}
},
{
"val" : 145,
"loc" : {
"type" : "Point",
"coordinates" : [
3.198408689,
50.64586985
]
}
},
]
}这是您可以运行的mongo聚合,它将返回每个集群的平均值。我甚至返回相同的GeoJSON格式。
db.roads.aggregate([
//unravel the cluster here
{ $unwind: "$data" },
//project the coordinates to more readable names while preserving roadname and GeoJSON type.
{ $project: {roadname: 1, type: "$data.loc.type", val : "$data.val", lng: { $arrayElemAt: ["$data.loc.coordinates",0]}, lat: { $arrayElemAt: ["$data.loc.coordinates",-1]}}},
//group on cluster id while preserving type and roadname. Take avergae of value, lat and long.
{ $group: { _id : "$_id", roadname: {$first:"$roadname"}, type: {$first: "$type"}, avgLng: { $avg: "$lng" },avgLat: { $avg: "$lat" }, avgVal: { $avg : "$val" }}},
//re-project to fit the similar format of original collection
{ $project: { _id: 1, roadname: 1, data : { val: "$avgVal", loc: {type : "$type", coordinates: ["$avgLng", "$avgLat"]}} }}
])还可以在聚合管道的末尾添加一个额外的$out,以便将所有这些平均质心移动到另一个更易于管理的集合中。
下面是上面所示的aggregration管道调用之后的结果。
[
{
"_id": ObjectId("583ee50bd7c4d711d45c775c"),
"roadname": "RD700",
"data": {
"val": 144.03703703703704,
"loc": {
"type": "Point",
"coordinates": [
3.2232721289257142,
50.67602178708569
]
}
}
},
{
"_id": ObjectId("583ee50bd7c4d711d45c775b"),
"roadname": "RD700",
"data": {
"val": 170.0344827586207,
"loc": {
"type": "Point",
"coordinates": [
3.22367598656322,
50.67626952408046
]
}
}
},
...
]https://stackoverflow.com/questions/40806348
复制相似问题