title: C# SQLGeography: diagnosing invalid geometries
body: <p>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 <code>SqlGeography.STIsValid()</code>.  </p>
  
  <p>For most or all of these cases, the .NET method <code>SqlGeography.MakeValid()</code> 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 <code>MakeValid</code> makes without understanding the reasons for the geography being considered invalid and what <code>MakeValid</code> does to correct them.</p>
  
  <p>By calling <code>IsValidDetailed</code>, we get a cryptic error message that is not well documented.  For many of the cases, the string returned by <code>IsValidDetailed</code> looks something like this:</p>
  
  <blockquote>
    <p>24404: Not valid because polygon ring (1) intersects itself or some other ring. The problem occurs in entry (19) in a geometry collection</p>
  </blockquote>
  
  <p>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. </p>
  
  <p>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?</p>