C # SQLGeography: diagnosing invalid geometry

We have a lot of geography stored in MapInfo tables, which we would now like to store in SQL Server. We wrote C # utilities to read geography from MapInfo files and import them into a SQL server. For the most part, this works well. However, after many cleaning checks using the tools that we developed in the house, we are still left with a significant number of geographical regions that are considered invalid SqlGeography.STIsValid() .

For most or all of these cases, the .NET SqlGeography.MakeValid() method can create a valid geography instance. However, the documentation on this issue is rather poor, and we are not satisfied with just accepting the changes that MakeValid makes, without understanding the reasons why the geography is considered invalid and what MakeValid does to correct them.

By calling IsValidDetailed , we get an error message that is poorly documented. For many cases, the line returned by IsValidDetailed looks something like this:

24404: Not valid because the polygonal ring (1) intersects itself or some other ring. The problem arises when entering (19) in the geometric collection

All geography objects that we are trying to import are multi-polygons. Having analyzed this error message, we tried to identify the problems of polygons and rings inside these polygons. However, we find that the indices do not seem to actually match the actual problematic polygons / rings. In many cases, indices go beyond arrays of input geometries.

Is there a better way to determine the specific reason why a geometry / geography object is considered invalid and determine which polygons, rings or points are problematic?

+5
source share
1 answer

I have come across this error several times. This is usually due to the fact that the SQL server requires that the points be specified in counterclockwise order. When the points are in clockwise order, it throws an exception that you see.

Here is a great visual: http://danielwertheim.se/sqlgeography-in-sql-server-2012-polygon-must-start-on-correct-position-no/

... and for a practical solution, check out this blog post: http://blogs.msdn.com/b/edkatibah/archive/2008/08/19/working-with-invalid-data-and-the-sql- server-2008-geography-data-type-part-1b.aspx

0
source

All Articles