[英]How to query all spatial trajectories that go from one region to another
Consider a mongo db where each document corresponds to a spatial trajectory.考虑一个 mongo db,其中每个文档对应一个空间轨迹。 That is, there is a field that contains an array for each document.
也就是说,有一个字段包含每个文档的数组。 Each array is a sequence of latitude/longitude pairs representing a geographical trajectory.
每个数组是表示地理轨迹的纬度/经度对序列。 Given two geographical regions R1, R2 defined by geojson polygones, find all trajectories that intersect first R1 and second R2.
给定由 geojson 多边形定义的两个地理区域 R1、R2,找到与第一个 R1 和第二个 R2 相交的所有轨迹。 Given the size of the db, computational time is highly important.
考虑到数据库的大小,计算时间非常重要。
My first attempt works but does not consider direction.我的第一次尝试有效,但没有考虑方向。 Moreover, it is extremely slow.
而且,它非常慢。 I use an aggregate framework (regions gjs[i]).
我使用聚合框架(区域 gjs[i])。 My current pipeline contains the following.
我当前的管道包含以下内容。
{"$match":{"location.lonlat":{'$geoIntersects':{'$geometry':gjs[0]}}}}, {"$match":{"location.lonlat":{'$geoIntersects':{'$geometry':gjs[1]}}}}
From the MongoDB manual :来自MongoDB 手册:
$geoIntersects uses spherical geometry. $geoIntersects 使用球面几何。 $geoIntersects does not require a geospatial index.
$geoIntersects 不需要地理空间索引。 However, a geospatial index will improve query performance.
但是,地理空间索引将提高查询性能。 Only the 2dsphere geospatial index supports $geoIntersects.
只有 2dsphere 地理空间索引支持 $geoIntersects。
So you have a problem: your query works because $geoIntersects
does not require an index but it is slow because there is no index.所以你有一个问题:你的查询有效,因为
$geoIntersects
不需要索引,但它很慢,因为没有索引。 To speed it up, you must create a 2dsphere
index because that is the only index (ie NOT a 2d
index) used by $geoIntersects
-- which unfortunately will not work on legacy arrays of coordinate points (your "trajectories").为了加快速度,您必须创建一个
2dsphere
索引,因为这是$geoIntersects
使用的唯一索引(即不是2d
索引)——不幸的是,它不适用于坐标点的旧版 arrays(您的“轨迹”)。
My recommendation is to go full GeoJSON:我的建议是 go 完整的 GeoJSON:
location.lonlat
array of points to GeoJSON LineString
object, eglocation.lonlat
点数组转换为 GeoJSON LineString
object,例如{location: {
"trajectory" : {
"type" : "LineString",
"coordinates" : [ [10,60] , [10,61] , [10,62] ]
}
},
You could do this with a non-destructive update to create a new field trajectory
as a peer to lonlat
in the location
object.您可以通过非破坏性更新来创建一个新的场
trajectory
,作为lonlat
location
的 lonlat 的对等体。 Note the use of the array form of update()
that permits pipelines and thus the $location.lonlat
array can be copied into the new field:请注意使用允许管道的
update()
数组形式,因此可以将$location.lonlat
数组复制到新字段中:
db.foo.update({}, [ {$set: {"location.trajectory": {type: "LineString", coordinates: "$location.lonlat"}}} ], {multi:true});
2dsphere
index on location.trajectory
:location.trajectory
上创建一个2dsphere
索引:db.foo.createIndex({"location.trajectory":"2dsphere"});
Your queries will now be performant.您的查询现在将是有效的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.