我可以通过附近的多边形查询具有边界多边形的 DocumentDB 文档吗?

Can I query a DocumentDB document with a bounding polygon, by a nearby polygon?

像许多人一样,当 Microsoft Azure 团队出现时我很兴奋 announced support for spatial queries in DocumentDB two months ago. In particular, DocumentDB supports the ST_DISTANCE query which allows you to query documents by their distance from another geometry. An example of this using LINQ is found on the Azure documentation:

foreach (UserProfile user in client.CreateDocumentQuery<UserProfile>(collection.SelfLink)
    .Where(u => u.ProfileType == "Public" && a.Location.Distance(new Point(32.33, -4.66)) < 30000))
{
    Console.WriteLine("\t" + user);
}

在这个例子中,查询是在两点之间。但是,文档讨论 "geometries" 和 "point expressions" 之间的距离这一事实让我认为可以通过点以外的几何图形进行查询。

我的文档将边界多边形作为属性。我想通过它们彼此接近来查询这些文档。我尝试了以下方法:

DocumentClient Client = Connection.Client;

var nearbyItems = Client
    .CreateDocumentQuery<T>(Collection.DocumentsLink)
    .Where(x.BoundingPolygon.Distance(adjacentPolygon) < 1000)
    .ToList();

我已经尝试使用以下相邻且非常简单的边界多边形:

文档多边形:

"geometry": {
  "type": "Polygon",
  "coordinates": [[
    [-33,18],[-34,18],[-34,19],[-33,19],[-33,18]
    ]]
  }

相邻多边形:

"geometry": {
  "type": "Polygon",
  "coordinates": [[
    [-33,19],[-34,19],[-34,20],[-33,20],[-33,19]
    ]]
  }

但是我的查询没有返回文档,即使将距离提高到数千公里也是如此。

是否可以通过接近另一个边界多边形来查询具有边界多边形的 DocumentDB 文档?

没有。 ST_DISTANCE 旨在仅适用于两点表达式。 ST_WITHIN 将使用点表达式和多边形。

要找到两个多边形之间的最小距离,您必须比较一个多边形上每个点与另一个多边形上每个点的点距,但我发现您通常不需要那么精确并且即使你这样做了,在你做详细计算之前先得到一个大概的答案也是有意义的。

作为近似答案的示例,您可以将一个多边形的中心与其他多边形的中心进行比较。 GeoLib 是具有 getCenter() 功能的 javascript 库。从那里提取代码并将其放入存储过程中,每当您编写多边形时都会调用该存储过程(您必须为现有多边形编写迁移)。在该存储过程中,调用 getCenter() 函数并将结果存储在与多边形相同的文档中的另一个字段中。然后对查询中的这些中心点使用 ST_DISTANCE 函数来查找附近的所有其他多边形。