Search code examples
sql-serversqlgeography

Why simple sql geography polygon fill whole Earth?


I'm trying to create a geography polygon in sqlserver:

declare @poly geography = geography::STGeomFromText('POLYGON ((37.1834472346309 55.9627364130318, 37.1843162703518 55.9615689125515, 37.1847990679744 55.9603833217405, 37.1861294436458 55.9590893612134, 37.1858183074 55.9583972249632, 37.1860757994655 55.9580361054639, 37.1851960349086 55.9576990575455, 37.1834901499751 55.9592217684712, 37.1819451975825 55.9611416225876, 37.1812692809108 55.9626581795365, 37.1834472346309 55.9627364130318))', 4326)

select @poly

And result looks like this:

screenshot

Why my polygon fill whole Earth?

I have an another polygon, and it's looks ok:

declare @poly2 geography = geography::STGeomFromText('POLYGON ((37.1880241973734 55.964234021191, 37.1895369632579 55.9640354359623, 37.1900519473887 55.9640655246991, 37.1912964923716 55.9645830472949, 37.1928736312724 55.9647996825904, 37.193699751649 55.9648598588536, 37.1936568363047 55.965563914075, 37.1913823230601 55.9656120541331, 37.1905669315196 55.966273973827, 37.1896227939463 55.9666891721997, 37.1887323005534 55.9674172628857, 37.1882924182749 55.9681333056029, 37.1881851299143 55.9689095049396, 37.1880027397013 55.9692163701344, 37.1877452476359 55.9693788271893, 37.1874341113901 55.9694570470084, 37.1870371444559 55.9694690808126, 37.186693821702 55.9694510301049, 37.1863504989481 55.9693246749138, 37.1861144645548 55.9691682345792, 37.1860179050303 55.9689696747783, 37.185974989686 55.9687711139541, 37.1862324817515 55.9675737103232, 37.186693821702 55.9673631078402, 37.1868332965708 55.9667192588449, 37.1874555690622 55.9663883042339, 37.1876165016031 55.9657384214668, 37.1881529434061 55.9652269319451, 37.1880134685373 55.964577029591, 37.1880241973734 55.964234021191))', 4326)

select @poly2

screenshot 2

I trying this query in different versions of SqlServer (SqlExpress 2017 and SqlServer Professional 2016)- the results are same.


Solution

  • There are 2 ways to interpret polygons and it seems the order of the points you are supplying SQL Server with is not the one that it expects, so it's picking the area of the "outside" rather than the "inside".

    This is the right to left rule, and the simple fix is to call native function ReorientObject() before selecting the polygon.

    declare @poly geography = geography::STGeomFromText('POLYGON ((37.1834472346309 55.9627364130318, 37.1843162703518 55.9615689125515, 37.1847990679744 55.9603833217405, 37.1861294436458 55.9590893612134, 37.1858183074 55.9583972249632, 37.1860757994655 55.9580361054639, 37.1851960349086 55.9576990575455, 37.1834901499751 55.9592217684712, 37.1819451975825 55.9611416225876, 37.1812692809108 55.9626581795365, 37.1834472346309 55.9627364130318))', 4326)
    
    select @poly.ReorientObject()
    

    enter image description here