- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个看起来像这样的集合:
[
{
"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]
}
}
}
]
}
]
我在 map 上显示每个data.loc
,这导致我发现: 。 (点颜色代表 val
字段)
EDIT3:为了阐明我的数据库结构,这里是确切数据库的表示。每条灰线代表所提供数据集中的一个根元素:
我想“对附近的点进行分组(使用data.loc
),并且具有相同的父名称
”,并聚合它们的val
(为了简单起见,用平均值表示),以便显示如下内容:
编辑3:重要的是要了解我试图聚合的点不共享任何共同的属性(property)或祖先。它们的唯一共同点是空间接近
我知道 near
、geonear
和 group
聚合,但我只是找不到解决方案来执行此操作。
我想使用纯 mongodb 解决方案。如果不可能,我也可以使用 turf.js 或其他库,但我只是在努力寻找一种可行且可扩展的方法来做到这一点。
<小时/>编辑:集合上的主要根元素代表一条道路,因此道路上的所有点都具有相同的父roadname
。
EDIT2:可以找到数据 here
最佳答案
我认为这对你有用。我导入了你的数据集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
,以将所有这些平均质心移动到另一个更易于管理的集合。
这是上面所示的聚合管道调用后的结果。
[
{
"_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
]
}
}
},
...
]
关于node.js - 按 $geonear 聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40806348/
我有一个看起来像这样的集合: { "_id" : ObjectId("5783051796a90cd8098fc3e0"), "name" : "Org 1", "locati
我有一个看起来像这样的集合: { "_id" : ObjectId("5783051796a90cd8098fc3e0"), "name" : "Org 1", "locati
我在 Java 中使用 mongodb 的地理功能,我在运行命令“geoNear”时遇到以下问题: 给定: 名为“GEOENTITIES”的集合,包含 GeoJSON 对象 此集合使用 Java 代码
我有一个名为 test 的集合,我想使用 geoNear 进行查询。 2d 索引设置在字段 loc 上。 我在这里正确使用 db.runCommand 吗? db.open (err, client)
我正在使用 MongoDB 2.6.3 查询大量地理空间数据。具体来说,我正在查询中心位置几公里范围内所有 ping 的数据集,然后按用户标识符折叠它们以计算每个用户有多少 ping。 当然,我为此使
我想根据它们与 2D 空间中多个点的接近程度对我的结果进行排序。 我假设这将通过查询第一点然后根据第二点重新查询/检查这些结果来完成? 也许下面的代码可以更好地解释我想要实现的目标? db.runCo
我有以下数据: { "_id" : ObjectId("55a8c1ba3996c909184d7a22"), "uid" : "1db82e8a-2038-4818-b805-76a
在mongodb中使用geoNear时是否可以添加更多的过滤器?例如,假设我的记录如下所示: { _id: {}, cid: 1, latlon: [ -74.07096147537231, 4
我在记录集合 Mongodb (3.4) 中有文档,如下所示:- { "_id" : ObjectId("592d0c78555a7436b0883960"), "us
我有这个查询: db.places.aggregate([ { "$geoNear" : { "near" : { "type" : "Po
我正在使用 mongoDB 在 Java 应用程序中存储和获取位置相关信息。 我在使用“geoNear”获取数据时遇到异常... 这是存储数据的代码: Document doc = new Docum
我正在 MongoDB 中对我的用户模型执行查询,并希望首先按搜索结果的距离(以公里为单位)(使用 $geoNear 运算符),然后按上次登录时间对搜索结果进行排序。问题是,距离属性以浮点变量中的米形
我的收藏有如下字段,存储用户签到(地理坐标) { "_id" : ObjectId("5333c3063b15ea390b3c986a"), "userID" : "5332cad33
我正在使用 NodeJs 和 Mongoose 并构建一个功能来列出附近的交易。 Deal.db.db.command({ "geoNear": Deal.collection.name
我一直在网上搜索与此相关的内容,但找不到。 我有这个聚合 Place.aggregate( [ { "$geoNear": { "near": {
我在集合 offers 中有以下示例 mongodb 文档。 集合:优惠 { "countryCode" : "LU", "geoLocation" : [ {
我有一个简单的 MongoDo 聚合查询: { "$geoNear": { "near": { "type": "Point",
我在 MongoDB 中有一些地理空间数据,我想找到靠近用户位置并符合特定条件的地点列表。 这是我的文档结构: { "_id" : { "$oid" : "528af043d1a2760efcd2fd
我的文档结构如下: { agency_key : '', route_id: '', direction_id: '', stops: [ {stop_id:15,
我正在开发一个 PHP 应用程序,我们需要在其中检索特定边界内的结果,但按结果的创建日期而不是距离排序。我认为 MongoDB 的 geoNear 命令对此非常有用,因为它负责计算每个结果的距离。但是
我是一名优秀的程序员,十分优秀!