Like many people I was excited when the Microsoft Azure team 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);
}
In this example, the query is between two points. However, the fact that the documentation talks about the distance between "geometries" and "point expressions" leads me to think that it may be possible to query by geometries other than points.
I have documents that have a bounding polygon as an attribute. I want to query these documents by their proximity to each other. I have tried the following:
DocumentClient Client = Connection.Client;
var nearbyItems = Client
.CreateDocumentQuery<T>(Collection.DocumentsLink)
.Where(x.BoundingPolygon.Distance(adjacentPolygon) < 1000)
.ToList();
I have tried this with the following adjacent and very simple bounding polygons:
Document Polygon:
"geometry": {
"type": "Polygon",
"coordinates": [[
[-33,18],[-34,18],[-34,19],[-33,19],[-33,18]
]]
}
AdjacentPolygon:
"geometry": {
"type": "Polygon",
"coordinates": [[
[-33,19],[-34,19],[-34,20],[-33,20],[-33,19]
]]
}
But the document is not returned from my query, even when raising the distance to thousands of kilometers.
Is it possible to query a DocumentDB document with a bounding polygon by proximity to another bounding polygon?