Search code examples
azure-cognitive-search

Azure Search: geo.intersects returning incorrect results?


I'm using the following query for Azure Search:

$filter=geo.intersects(coordinate, geography'POLYGON((1.136 44.733, 1.316 44.733, 1.316 44.553, 1.136 44.553, 1.136 44.733))')

This should return all points inside this polygon, which is a small area in south-western France. Unfortunately Azure Search returns results from outside the polygon.

Examples of results that seem incorrect to me (corrected, see comments):

  • lon=5.299151, lat=44.695285,
  • lon=0.397723, lat=44.668628,

The points in the polygon are entered clockwise (which was mentioned in a related question), but still results are incorrect.

Any suggestions on fixing the query?

I'm using the following code for feeding the index:

public class Geometry

{
    public Geometry(Coordinate c)
    {
        List<double> GeoList = new List<double>();         
        GeoList.Add((double)c.Longitude);
        GeoList.Add((double)c.Latitude);
        type = "Point";
        coordinates = GeoList;
    }

    public string type { get; set; }
    public IList<double> coordinates { get; set; }
}

Solution

  • TL;DR Put points in counterclockwise order when defining a polygon.


    A more nuanced answer is:

    Azure Search uses OData which uses the Well-known text format to define geometric objects. As per the WKT spec polygon points are in counterclockwise order if you want to include the area inside the polygon as seen from the top. This gets tricky to think about since we are dealing with non euclidean geometry.

    The [polygon] defines the “top” of the surface which is the side of the surface from which the exterior boundary appears to traverse the boundary in a counter clockwise direction.

    To solve your issue try putting your points in counter-clockwise order instead

    $filter=geo.intersects(coordinate,geography'POLYGON((1.136 44.733, 1.136 44.553, 1.316 44.553, 1.316 44.733, 1.136 44.733))')