3

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?

08Dc91wk
  • 4,254
  • 8
  • 34
  • 67

1 Answers1

1

No. ST_DISTANCE is designed to only work on two point expressions. ST_WITHIN will work with a point expression and a polygon.

To find the minimal distance between two polygons, you would have to compare the point distance of each point on one polygon to each point on the other, but I've found that you often don't need that precision and even when you do, it makes sense to get an approximate answer first before you do the detail calculation.

As an example of an approximate answer, you might compare the center of one polygon with the center of the others. GeoLib is a javascript library with a getCenter() function. Extract the code from there and put it in a stored procedure that you call whenever you write your polygons (you'll have to write a migration for existing polygons). In that stored procedure, call the getCenter() function and store the result in another field in the same document as the polygon. Then use the ST_DISTANCE function against those center points in a query to find all the other polygons that are nearby.

Larry Maccherone
  • 9,393
  • 3
  • 27
  • 43
  • Hi Larry. Thanks for your answer. I realise there are algorithms to determine the minimum distance but I want to do this with a DocumentDB spatial query to avoid having to load my database into memory. Note that comparing all vertex to vertex distances as you suggest does not give you the minimum distance in the case that the minimum distance is from a vertex to an edge. I may have to approximate polygons as circles and then load these into memory for further processing with a minimum distance algorithm. – 08Dc91wk Oct 09 '15 at 05:50
  • Algorithms are fun, yes? :-) If you found the two closest vertices on one side, then you could draw a line perpendicular to the line between those two points through the closest point on the other side. You could then compare that to the same calculation on the other side as well as to the vertex to vertex minimum. I can imagine geometries that would defeat even that algorithm, but imagine they are rare in true GIS. – Larry Maccherone Oct 09 '15 at 12:59
  • Note, what I'm suggesting wrt to comparing the centers as an approximate answer is equivalent to your suggestion to approximate polygons as circles. When you say, "load these into memory", I'm suggesting that you load the approximate answer (using an index capable `ST_DISTANCE` clause) into the memory of a stored procedure, where you do your detailed calculations. – Larry Maccherone Oct 09 '15 at 13:01
  • 1
    To be clear, the answer to your explicit question, "Is it possible to query a DocumentDB document with a bounding polygon by proximity to another bounding polygon?" is "No". – Larry Maccherone Oct 09 '15 at 13:03