How to fix polygon ring orientation using C # Entity Framework 5 DbGeoography Spatial Data

I am working with the new Entity-Framework 5, using the Spatial data type DbGeography as part of my model to store in one instance, POINT and in another POLYGON instance.

When setting the value of my POLYGON everything saves without errors, however this is only if I draw a polygon on the map clockwise. If I draw POLGON counterclockwise, I get an error, sql level, indicating that the data is an invalid type of geography.

Now, after my own research into the problem, it seems that the geography data type is pretty strict in terms of the orientation of the Polygons ring. The most common solution is to create a polygon as a geometry, and then convert it to a geography type.

I am looking for a solution in C # that can be applied to data before it is sent to sql. Basically something that will automatically fix the annular orientation of the coordinate array.

I tried to catch the error and then rebuilt the line by changing the array. This works in some cases, but, firstly, it is not reliable, and secondly, catching an error is a big hit in performance.

Thank you, Chris

+6
source share
4 answers

Hi, I had the same problem and decided to use it with

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

See here for more details.

+3
source

I had a problem with the order, and I solved it with the MakeValidGeographyFromText function from SQLSpatialTools I used it in SP:

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

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

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

  • Use the function directly from C # before calling EF with the correct polygon.
  • Install sqlspatialtools on the SQL server and do the processing there.
+2
source

The same problem here, using v7 Bing cards and their tools for drawing polygons found here: http://bingmapsv7modules.codeplex.com/wikipage?title=Shape%20Toolbox%20Module

0
source

The reason this happens is because for the type of geography, when the orientation is β€œwrong” from your point of view, you basically created the whole globe, except for the intended area.

The first step is to determine if this is so. This is easily done using the EnvelopeAngle() method (see Microsoft Documentation ). When this angle is> 90 Β°, your polygon describes more than a hemisphere, that is, the orientation is not the one you assumed, and the order should be reversed.

You can ReorientObject() using ReorientObject() .

So something like this should do the trick

 SqlGeography geoPolygon = ....; if (geoPolygon.EnvelopeAngle() > 90) { geoPolygon = geoPolygon.ReorientObject(); } 
0
source

Source: https://habr.com/ru/post/925204/


All Articles