We have a large amount of geography stored in MapInfo tables that we would now like to store in SQL Server. We have written C# utilities to read the geography from the MapInfo files and import them into SQL server. For the most part, this is working well. However, after many cleanup passes using tools we have developed in house, we are still left with a significant number of geographies that are considered invalid by SqlGeography.STIsValid()
.
For most or all of these cases, the .NET method SqlGeography.MakeValid()
is able to create a valid geography instance. However, the documentation on this is pretty poor, and we aren't content with simply accepting the modifications that MakeValid
makes without understanding the reasons for the geography being considered invalid and what MakeValid
does to correct them.
By calling IsValidDetailed
, we get a cryptic error message that is not well documented. For many of the cases, the string returned by IsValidDetailed
looks something like this:
24404: Not valid because polygon ring (1) intersects itself or some other ring. The problem occurs in entry (19) in a geometry collection
All of the geography objects we are trying to import are multipolygons. By parsing this error message, we have tried to identify the problem polygons and rings within those polygons. However, we are finding that the indexes do not seem to actually match the actual problematic polygons/rings. In many cases, the indexes are beyond the bounds of the arrays of the input geometries.
Is there a better way to pinpoint the specific reason for a geometry/geography object being considered invalid and to pinpoint which polygons, rings, or points are problematic?