none
WKT Point ordering weirdness

    Question

  • Can anyone tell me if there's any rules about the ordering of points in a geography polygon? I couldn't find any mention of rules but I've come across a situation that if the points are ordered one way then they don't work but when they're reversed they work fine. Below is an example of such a polygon. Incidentally if I make it a geometry then ordering the points both ways work.

    DECLARE @g geography;
    --SET @g = geography::STGeomFromText('POLYGON ((-1.412331 50.889745, -1.402288 50.892386, -1.400829 50.88612, -1.40658 50.884618, -1.412331 50.889745))', 4326) -- Broken
    SET @g = geography::STGeomFromText('POLYGON ((-1.412331 50.889745, -1.40658 50.884618, -1.400829 50.88612, -1.402288 50.892386, -1.412331 50.889745))', 4326) -- Works
    SELECT @g
    

    Thursday, August 25, 2011 10:57 PM

Answers

  • The ordering of points in a geography polygon defines the ring orientation. Ring orientation matters because, if you reverse the ring orientation your polygon will be inside-out - you'll exclude the area of the earth's surface you meant to include, and include the area that was meant to be outside the polygon.

    If you're using SQL Server 2008/R2, there is a technical limitation that prevents any geography polygon from encompassing more than one hemisphere of the earth's surface. Although this is a technical limitation in the software, it actually has some beneficial side-effects; namely, it generally prevents you from accidentally creating geography polygons with the incorrect ring orientation. Suppose that you wanted to define e.g. a country, and you accidentally use incorrect ring orientation to define everything on the earth other than that country. That's clearly going to be more than half the surface of the earth and you'll encounter the error you've discovered.

    However, in SQL Server Denali this technical limitation has been lifted. Be careful! That means that, rather than generate an error, your code will work just fine in Denali, but you may not get the results you expect - you'll get the inverse instead.

    The general rule to remember is to use the "left-hand rule" (or "left-foot rule") - if you were to walk along the ring between the points of the polygon, in the order in which they are listed, the interior of the polygon will be on your left-hand side. The exterior is on the right hand side. Therefore, if you are looking down on the polygon from above, the points of exterior rings should be defined in anti-clockwise order while interior rings should be listed in clockwise order.

     


    twitter: @alastaira blog: http://alastaira.wordpress.com/
    • Marked as answer by Andrew A Friday, August 26, 2011 8:09 AM
    Friday, August 26, 2011 6:49 AM
    Answerer

All replies

  • The ordering of points in a geography polygon defines the ring orientation. Ring orientation matters because, if you reverse the ring orientation your polygon will be inside-out - you'll exclude the area of the earth's surface you meant to include, and include the area that was meant to be outside the polygon.

    If you're using SQL Server 2008/R2, there is a technical limitation that prevents any geography polygon from encompassing more than one hemisphere of the earth's surface. Although this is a technical limitation in the software, it actually has some beneficial side-effects; namely, it generally prevents you from accidentally creating geography polygons with the incorrect ring orientation. Suppose that you wanted to define e.g. a country, and you accidentally use incorrect ring orientation to define everything on the earth other than that country. That's clearly going to be more than half the surface of the earth and you'll encounter the error you've discovered.

    However, in SQL Server Denali this technical limitation has been lifted. Be careful! That means that, rather than generate an error, your code will work just fine in Denali, but you may not get the results you expect - you'll get the inverse instead.

    The general rule to remember is to use the "left-hand rule" (or "left-foot rule") - if you were to walk along the ring between the points of the polygon, in the order in which they are listed, the interior of the polygon will be on your left-hand side. The exterior is on the right hand side. Therefore, if you are looking down on the polygon from above, the points of exterior rings should be defined in anti-clockwise order while interior rings should be listed in clockwise order.

     


    twitter: @alastaira blog: http://alastaira.wordpress.com/
    • Marked as answer by Andrew A Friday, August 26, 2011 8:09 AM
    Friday, August 26, 2011 6:49 AM
    Answerer