How to correct Polygon Ring Orientation using C# Entity Framework 5 DbGeography Spatial Data

谁都会走 提交于 2019-12-03 14:16:32

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

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.

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();
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!