简体   繁体   English

如何查询 go 从一个区域到另一个区域的所有空间轨迹

[英]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:

  1. Convert your location.lonlat array of points to GeoJSON LineString object, eg将您的location.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});

  1. Create a 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.

相关问题 如何在 Robo3T 中将结果从一个查询传递到另一个查询 - How to pass results from one query to another in Robo3T 如何查询以基于一个字段作为用户名获取引用到另一个集合的所有文档 - How to query to get all documents referenced to another collection based on one field as username MongoDB:如何使用 Java(使用或不使用 Spring)将带有别名的一个查询的特定字段添加到另一个查询的结果? - MongoDB: how to add the specific fields from one query with alias to result of another query using Java (with or without Spring)? 如何一次性返回mongoDB查询和gridFS数据的结果? - how to return the result of mongoDB query and gridFS data in a one go? 一个mongoid模型如何查询另一个模型? - How can one mongoid model query another? 如何从猫鼬查询中获取数据从一个js文件到另一个 - How to get the data from mongoose query from one js file to another 如何从另一个模型的引用字段中查询一个模型的文档? - How to query documents of one model, from referenced field from another model? Spring Data / MongoDB:一次性将一个文档从一个集合复制到另一个 - Spring Data / MongoDB: Copy a single document from one collection to another in one go MongoDB多边形的空间查询 - MongoDB spatial query for Polygons 如何将所有数据从一个 Mongodb 云项目复制到另一个? - How do I copy all data from one Mongodb Cloud Project to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM