7

I'm working with the new Entity-Framework 5 using the Spatial data type DbGeography as part of my model for storing in one instance, a POINT and in another instance a POLYGON.

When setting the value of my POLYGON all saves with no error however this is only the case if I draw the Polygon on the map in a clockwise order. If I draw the POLGON in an anti-clockwise direction I get an error the the sql level indicating that the data is an invalid geography type.

Now after doing my own research into the problem, it appears to stem from the geography data type being quite strict in terms of a Polygons ring orientation. The most common solution appears to be creating the Polygon as a geometry and then converting it to a geography type.

I am looking for a solution in C# that can be applied to the data before it gets sent to sql. Basically something that will auto correct the ring orientation of an array of coordinates.

I have tried catching the error and then rebuilding the string by reversing the array. This works in some cases but firstly it's not reliable and secondly catching the error is to big a performance hit.

Thanks Chris

cronline
  • 141
  • 2
  • 4

4 Answers4

3

Hi I had the same problem and solved it by using

var sqlGeography = SqlGeography.STGeomFromText().MakeValid()
var invertedSqlGeography = sqlGeography.ReorientObject();

For more details see here

GerardBeckerleg
  • 813
  • 1
  • 7
  • 13
2

I've had the order issue my self, and I solved it using the MakeValidGeographyFromText function from SQLSpatialTools I used it the following way inside a SP:

SET @ge = dbo.MakeValidGeographyFromText(
    'POLYGON((' + @pois + '))', 4326);

Where @pois is a string containing the coordinates in a valid format, but wrong rotation.

I'm not sure how easy this is to integrate with EF-5, but I see two ways:

  1. Use the function directly from C#, before calling EF with the correct polygon.
  2. Install sqlspatialtools on the SQL server, and do the processing there.
Tomas
  • 3,285
  • 2
  • 18
  • 25
2

The reason this happens is that for a geography type, when the orientation is "wrong" from your perspective, you basically created the whole globe, except the intended area.

First step is to detect if this is the case. This can easily be done with the EnvelopeAngle() method (see Microsoft documentation). When this angle is >90º, your polygon describes more than a hemisphere, meaning the orientation is not what you intended and the order should be reversed.

Reversing can be done with ReorientObject().

So something like this should do the trick

SqlGeography geoPolygon = ....;
if (geoPolygon.EnvelopeAngle() > 90)
{
    geoPolygon = geoPolygon.ReorientObject();
}
H. de Jonge
  • 806
  • 6
  • 21
0

Same problem here, using Bing maps v7 and their shape toolkit for drawing polygons found here: http://bingmapsv7modules.codeplex.com/wikipage?title=Shape%20Toolbox%20Module

  • Hi Adam, this seems like a comment which should be added as a comment to the question, not posted as an answer. Since you're new here, you have to get active to earn a bit more reputation, so you can post a comment. – VKen Oct 09 '12 at 00:27