5

我想编写一个地理空间视图,该视图在给定纬度和经度的一英里半径内搜索以下文档。我该怎么做呢?

{
   "agree_allowed":true,
   "assigned_by":"",
   "assigned_to":"",
   "comments_allowed":true,
   "location": {
    "coordinates": [
      "-74.168868",
      "40.854655"
    ],
    "type": "Point"
  },
   "subscribed":{
      "user_cfd29b81f0263a380507":true,
      "user_cfd29b81f0263a3805010":true
   },
   "type":"report",
   "user_id":"user_cfd29b81f0263a380507",
   "username":"test17"
}
4

3 回答 3

3

Because you can only use bounding-box queries with Couchbase spatial views, you will have to split your distance query into two steps:

  1. Query Couchbase for coordinates that fall within a bounding box that matches your radius.
  2. Filter out coordinates returned by #1 that are further than the radius you specified.

For the first step, you'll need to write a spatial view as follows:

function(doc, meta)
{
  if (doc.location)
     emit(doc.location, [meta.id, doc.location]);
}

Note: this is the Couchbase 3.0 version of the view, in Couchbase 4 you don't need to emit the meta.id and doc.location in the value anymore.

Now, given a starting point (lat,lon) and radius r, you need to calculate a bounding box [lat1,lon1, lat2,lon2] to query the view for a list of documents whose coordinates potentially fall within the radius you want. The bounding box query in Couchbase specifies the bottom-left and top-right coordinates.

Next, in your application, iterate over all the results and check whether they really do fall within R distance of your starting point.

Depending on how much accuracy you need, you can either assume the Earth is flat and just do the calculations on a 2D plane, which will be inaccurate but not terribly so for a distance of 1 mile. Or alternatively, use the actually accurate formulae to calculate everything, as described in this excellent article: http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates

Or better yet, you can use a geolocation library for the language of your choice to calculate the bounding box and the distances. Here's one for C# and one for Java.

于 2015-07-31T17:02:01.430 回答
1

查看 Couchbase 中的 GeoSpatial 视图文档:http: //docs.couchbase.com/4.0/views/spatial-views.html

您可以采取的一种方法是使用围绕其位置的 1 英里边界框对所有文档进行索引。

然后,您将查询该视图,其中 start_range 和 end_range 是相同的范围,这只是上面文档的位置。这将向您返回该点位于其 1 英里边界框内的所有文档。

您可以将 GeoJSON 用于更精确的边界框,不幸的是它们在规格中没有圆圈,因此根据您制作边界框的先进程度,您可能会得到与查询位置不完全一致的结果。

于 2015-07-31T16:26:56.497 回答
0

With spatial views, you can search only by bounding box.

You may want to search within radius - is hidden in Full-Text Search ... https://docs.couchbase.com/server/current/fts/fts-geospatial-queries.html

于 2020-07-31T17:44:57.230 回答