2

Creating a DbGeography from text using DbGeography.FromText(polygonAsText, 4326); generates the quoted exception. The box dimensions have been provided by a Google geocoding result as a suggested viewport dimension. Are the coordinates dud? Are the order used in the construction of the polygon wrong?

The latitude/longitude ordering has provided accurate results with other coordinates, except this one.

North East Longitude: 180.0 North East Latitude: 90.0

South West Longitude: -180.0 South West Latitude: -90.0

POLYGON((180 90, -180 90, -180 -90, 180 -90, 180 90))

The exception:

24206: The specified input cannot be accepted because it contains an edge with antipodal points.

Why don't these coordinates result in a valid polygon?

simbolo
  • 7,279
  • 6
  • 56
  • 96
  • Have you tried calling ReorientObject and/or MakeValid on this, which will reorder your polygon's orientation so that the node order is correct -- yours appear to be in reverse order, so that the whole globe is outside your polygon. DbGeography and, by extension, SQL Server 2012 now support whole globe objects, so you should be able to do what you are attempting. You can also try adding points between you antipodal points, so you don't go from one pole directly to another, eg, 180 -90, 180 0, 180 90. I can't test as I only have older C#/SQL Server 2008, but I have read a fair bit about this. – John Powell Sep 11 '14 at 17:49

1 Answers1

5

There is a nice easy way to address the problem (assuming you do have SQL Server 2012 or onwards - which as John Barca points out is absolutely required to build a spatial object covering more than half a hemisphere).

Try the following:

// Let's use the nice and simple "FULLGLOBE" statement to create a polygon covering the earth
SqlGeography geography = SqlGeography.STGeomFromText("FULLGLOBE", 4326);
DbGeography dbGeography = DbGeography.FromText(geography.ToString(), 4326);

The conversion (second line) is taken from a previous answer of mine on SO here.

Community
  • 1
  • 1
Jon Bellamy
  • 3,333
  • 20
  • 23
  • 1
    I gather that the actual error message re antipodal points can be avoided by adding a point on the equator, eg, -180 90, -180 0, -180 -90, but the FULLGLOBE construct covers this anyway. Sadly, I lack access to SQL Server 2012. – John Powell Sep 13 '14 at 11:53
  • I'm not sure without testing John but would suggest that instead you reverse the coordinates and create a SqlGeometry instance covering the earth then convert it to SqlGeography using a method like I have above. It works just as well and the framework does a great job or reversing the coordinates automatically to cover the earth! – Jon Bellamy Sep 13 '14 at 16:40
  • @JohnBarça For fullness I tested your theory in SQL 2014. Adding the additional two points required a MakeValid() on the created instance. I then dragged it back out as WKT and got *LINESTRING (0 90, 180 0, 0 -90)*. I also tried adding further points at Longitude 0 but this had no further effect. I also further tested my SqlGeometry suggestion but once you convert it to SqlGeography the same reported error occurs. It appears the only way is via the FULLGLOBE method. – Jon Bellamy Sep 14 '14 at 20:32
  • Thanks for update. Shame the OP has lost interest, interesting question really. – John Powell Sep 15 '14 at 20:28